Java使用AJ-Captcha滑动验证码
文档地址: 文档地址
1、验证码类型包括两种
滑块验证码
文字验证码
2、项目引入AJ-Captcha
2.1 添加依赖
<dependency>
<groupId>com.anji-plus</groupId>
<artifactId>captcha</artifactId>
<version>1.3.0</version>
</dependency>
后端SpringBoot
@Resource
private CaptchaService captchaService;
@PostMapping("/get") // 获取图片
public ResponseModel get(@RequestBody CaptchaVO data, HttpServletRequest request) {
assert request.getRemoteHost()!=null;
data.setBrowserInfo(getRemoteId(request));
return captchaService.get(data);
}
@PostMapping("/check") // 进行校验
public ResponseModel check(@RequestBody CaptchaVO data, HttpServletRequest request) {
data.setBrowserInfo(getRemoteId(request));
return captchaService.check(data);
}
@PostMapping("/verify") // 二次校验
public ResponseModel verify(@RequestBody CaptchaVO data, HttpServletRequest request) {
return captchaService.verification(data);
}
public static final String getRemoteId(HttpServletRequest request) {
String xfwd = request.getHeader("X-Forwarded-For");
String ip = getRemoteIpFromXfwd(xfwd);
String ua = request.getHeader("user-agent");
if (StringUtils.isNotBlank(ip)) {
return ip + ua;
}
return request.getRemoteAddr() + ua;
}
private static String getRemoteIpFromXfwd(String xfwd) {
if (StringUtils.isNotBlank(xfwd)) {
String[] ipList = xfwd.split(",");
return StringUtils.trim(ipList[0]);
}
return null;
}
1. 获取验证码接口:http://:/captcha/get
入参:
{
"captchaType": "blockPuzzle", //验证码类型 clickWord
"clientUid": "唯一标识" //客户端UI组件id,组件初始化时设置一次,UUID(非必传参数)
}
回参:
{
"repCode": "0000",
"repData": {
"originalImageBase64": "底图base64",
"point": { //默认不返回的,校验的就是该坐标信息,允许误差范围
"x": 205,
"y": 5
},
"jigsawImageBase64": "滑块图base64",
"token": "71dd26999e314f9abb0c635336976635", //一次校验唯一标识
"secretKey": "16位随机字符串", //aes秘钥,开关控制,前端根据此值决定是否加密
"result": false,
"opAdmin": false
},
"success": true,
"error": false
}
2. 核对验证码接口接口:http://:/captcha/check
入参:
{
"captchaType": "blockPuzzle",
"pointJson": "QxIVdlJoWUi04iM+65hTow==", //aes加密坐标信息
"token": "71dd26999e314f9abb0c635336976635" //get请求返回的token
}
回参:
{
"repCode": "0000",
"repData": {
"captchaType": "blockPuzzle",
"token": "71dd26999e314f9abb0c635336976635",
"result": true,
"opAdmin": false
},
"success": true,
"error": false
}
使用自定义Redis
@Service
@Slf4j
public class CaptchaCacheServiceRedisImpl implements CaptchaCacheService {
private static final String REDIS = "redis";
@Autowired
private RedisService redisService;
@Override
public void set(String key, String value, long expiresInSeconds) {
redisService.set(CacheKeyConstants.USER_IMG_VERIFICATION_CODE_PREFIX + key, value, expiresInSeconds);
}
@Override
public boolean exists(String key) {
return redisService.hasKey(CacheKeyConstants.USER_IMG_VERIFICATION_CODE_PREFIX + key);
}
@Override
public void delete(String key) {
redisService.delete(CacheKeyConstants.USER_IMG_VERIFICATION_CODE_PREFIX + key);
}
@Override
public String get(String key) {
return redisService.get(CacheKeyConstants.USER_IMG_VERIFICATION_CODE_PREFIX + key);
}
@Override
public String type() {
return REDIS;
}
}
配置文件位置:
THE END
0
二维码
打赏
海报
Java使用AJ-Captcha滑动验证码
文档地址: 文档地址
1、验证码类型包括两种
滑块验证码
文字验证码
2、项目引入AJ-Captcha
2.1 添加依赖
<dependency>
<groupId>com.anji-p……
共有 0 条评论