1.pom文件中引用easy-captcha:
<!-- 新验证码 -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
2.网关给前端调用获取验证码的路由:
package com.lt.gateway.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import com.lt.gateway.handler.ValidateCodeHandler;
/**
* 路由配置信息
*
* @author lt
*/
@Configuration
public class RouterFunctionConfiguration
{
@Autowired
private ValidateCodeHandler validateCodeHandler;
@SuppressWarnings("rawtypes")
@Bean
public RouterFunction routerFunction()
{
return RouterFunctions.route(
RequestPredicates.GET("/code").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)),
validateCodeHandler);
}
}
3.获取验证码(当个控制层):
package com.lt.gateway.handler;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import com.lt.common.core.exception.CaptchaException;
import com.lt.common.core.web.domain.AjaxResult;
import com.lt.gateway.service.ValidateCodeService;
import reactor.core.publisher.Mono;
/**
* 验证码获取
*
* @author lt
*/
@Component
public class ValidateCodeHandler implements HandlerFunction<ServerResponse>
{
@Autowired
private ValidateCodeService validateCodeService;
@Override
public Mono<ServerResponse> handle(ServerRequest serverRequest)
{
AjaxResult ajax;
try
{
ajax = validateCodeService.createCapcha();
}
catch (CaptchaException | IOException e)
{
return Mono.error(e);
}
return ServerResponse.status(HttpStatus.OK).body(BodyInserters.fromValue(ajax));
}
}
4.获取与校验验证码接口:
package com.lt.gateway.service;
import java.io.IOException;
import com.lt.common.core.exception.CaptchaException;
import com.lt.common.core.web.domain.AjaxResult;
/**
* 验证码处理
*
* @author lt
*/
public interface ValidateCodeService
{
/**
* 生成验证码
*/
public AjaxResult createCapcha() throws IOException, CaptchaException;
/**
* 校验验证码
*/
public void checkCapcha(String key, String value) throws CaptchaException;
}
5.生成验证码与检查验证码是否验证通过方法实现 获取时丢到redis中
package com.lt.gateway.service.impl;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import com.wf.captcha.ArithmeticCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FastByteArrayOutputStream;
import com.google.code.kaptcha.Producer;
import com.lt.common.core.constant.Constants;
import com.lt.common.core.exception.CaptchaException;
import com.lt.common.core.utils.IdUtils;
import com.lt.common.core.utils.StringUtils;
import com.lt.common.core.utils.sign.Base64;
import com.lt.common.core.web.domain.AjaxResult;
import com.lt.common.redis.service.RedisService;
import com.lt.gateway.config.properties.CaptchaProperties;
import com.lt.gateway.service.ValidateCodeService;
/**
* 验证码实现处理
*
* @author lt
*/
@Service
public class ValidateCodeServiceImpl implements ValidateCodeService
{
@Resource(name = "captchaProducer")
private Producer captchaProducer;
@Resource(name = "captchaProducerMath")
private Producer captchaProducerMath;
@Autowired
private RedisService redisService;
@Autowired
private CaptchaProperties captchaProperties;
/**
* 生成验证码
*/
@Override
public AjaxResult createCapcha() throws IOException, CaptchaException
{
AjaxResult ajax = AjaxResult.success();
boolean captchaOnOff = captchaProperties.getEnabled();
ajax.put("captchaOnOff", captchaOnOff);
if (!captchaOnOff) {
return ajax;
}
// 保存验证码信息
String uuid = IdUtils.simpleUUID();
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
ArithmeticCaptcha captcha =new ArithmeticCaptcha(150,40,2);
//图片数据 丢redis存
redisService.setCacheObject(verifyKey, captcha.text().toLowerCase(), Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
ajax.put("uuid", uuid);
ajax.put("img",captcha.toBase64());
//ajax.put("img", Base64.encode(os.toByteArray()));
return ajax;
}
/**
* 校验验证码
*/
@Override
public void checkCapcha(String code, String uuid) throws CaptchaException
{
if (StringUtils.isEmpty(code)) {
throw new CaptchaException("验证码不能为空");
}
if (StringUtils.isEmpty(uuid)) {
throw new CaptchaException("验证码已失效");
}
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
String captcha = redisService.getCacheObject(verifyKey);
redisService.deleteObject(verifyKey);
if (!code.equalsIgnoreCase(captcha)) {
throw new CaptchaException("验证码错误");
}
}
}
其中com.lt相关的包 是我这边内网相关的核心包 根据自己代码情况 自行修改;
前端:
重点是红框这句 captchaOnOff这个是自定义的 判断是否开启验证码的 没有去掉即可
?????什么只想要正数结果的验证码???
生成验证码的具体实现 ValidateCodeServiceImpl 中生成时验证一下
如果结果小于0 再重新生成就好了。