您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容介绍了“Java如何实现OAuth2.0授权系统的验证码功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
1.集成EasyCaptcha:
<dependencies>
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
2.生成验证码并保存到Redis中:
/**
* 验证码
*
* @return
*/
@GetMapping("/captcha")
public Result captcha() {
String captchaKey = "captcha_" + UUID.randomUUID();
// 三个参数分别为宽、高、位数
SpecCaptcha captcha = new SpecCaptcha(130, 60, 4);
// 设置字体 有默认字体,可以不用设置
captcha.setFont(new Font("Verdana", Font.PLAIN, 32));
// 设置类型,纯数字、纯字母、字母数字混合
captcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
log.info("key: [{}] ,code: [{}]", captchaKey, captcha.text());
// 存入Redis ,默认两分钟
redisBaseUtil.set(captchaKey, captcha.text(), 2, TimeUnit.MINUTES);
Map<String, Object> map = new HashMap<>(4);
map.put("captchaKey", captchaKey);
map.put("image", captcha.toBase64());
return Result.success(map);
}
3. 校验验证码的Filter:
package com.hanxiaozhang.filter;
import com.hanxiaozhang.constant.Constant;
import com.hanxiaozhang.redis.util.RedisUtil;
import com.hanxiaozhang.result.ResultCode;
import com.hanxiaozhang.result.Result;
import com.hanxiaozhang.util.JsonUtil;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* 〈一句话功能简述〉<br>
* 〈验证码过滤器〉
*
* @author hanxinghua
* @create 2021/4/4
* @since 1.0.0
*/
@Slf4j
@Component
public class CaptchaFilter extends ZuulFilter {
@Autowired
private RedisUtil redisBaseUtil;
@Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest serverHttpRequest = currentContext.getRequest();
String uri = serverHttpRequest.getRequestURI();
if (uri.contains("/oauth/token")) {
String method = serverHttpRequest.getMethod();
// 处理跨域Post发送两次请求
if (Constant.OPTIONS.equals(method)) {
return null;
}
Map<String, String[]> parameterMap = serverHttpRequest.getParameterMap();
String[] captchaKeys = null, captchaCodes = null;
if (!parameterMap.isEmpty()
&& (captchaKeys = parameterMap.get("captcha_key")) != null
&& (captchaCodes = parameterMap.get("captcha_code")) != null) {
String captchaKey = captchaKeys[0];
String captchaCode = captchaCodes[0];
log.info("Request Captcha Parameters: key: [{}] ,code: [{}]", captchaKey, captchaCode);
String redisCaptchaCode = redisBaseUtil.get(captchaKey);
String responseBody = null;
if (redisCaptchaCode == null) {
responseBody = JsonUtil.beanToJson(Result.error(ResultCode.LOGIN_CAPTCHA_EXPIRE));
} else if (!captchaCode.trim().equalsIgnoreCase(redisCaptchaCode)) {
responseBody = JsonUtil.beanToJson(Result.error(ResultCode.LOGIN_CAPTCHA_ERROR));
}
if (responseBody != null) {
currentContext.setSendZuulResponse(false);
currentContext.setResponseStatusCode(200);
currentContext.getResponse().setContentType(Constant.APP_JSON_UTF_8);
log.info("Response Parameters:
[{}]", responseBody);
currentContext.setResponseBody(responseBody);
}
}
}
return null;
}
}
4.使用,这里使用《Idea中HTTP Client请求测试工具》:
4.1 获取验证码:
GET http://localhost/api/system/captcha
4.2 校验验证码:
POST http://localhost/api/system/oauth/token?username={{username}}&password={{password}}&grant_type=password&scope={{scope}}&client_id={{client_id}}&client_secret={{client_secret}}&captcha_key=captcha_23cacfe5-2751-44af-a34d-5e795caeb46a&captcha_code=5594
“Java如何实现OAuth2.0授权系统的验证码功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。