① 配置终端不校验验证码

  • 后端不校验验证码 ```yaml nacos pig-gateway-dev.yml

不校验验证码终端

gateway: ignore-clients:

  1. - test
  2. - XXX
  1. <a name="odjy9"></a>
  2. ### ② 配置前端关闭验证码
  3. - **前端不显示验证码,website.js中配置validateCode关闭验证码**
  4. ```javascript
  5. validateCode: false //是否开启验证码校验

验证码原理

① webflux 生成验证码原理

验证码直接网关异步生成,基于webflux

  1. public class ImageCodeHandler implements HandlerFunction<ServerResponse> {
  2. private final Producer producer;
  3. private final RedisTemplate redisTemplate;
  4. @Override
  5. public Mono<ServerResponse> handle(ServerRequest serverRequest) {
  6. //生成验证码
  7. String text = producer.createText();
  8. BufferedImage image = producer.createImage(text);
  9. //保存验证码信息
  10. String randomStr = serverRequest.queryParam("randomStr").get();
  11. redisTemplate.opsForValue().set(CommonConstants.DEFAULT_CODE_KEY + randomStr, text, 60, TimeUnit.SECONDS);
  12. // 转换流信息写出
  13. FastByteArrayOutputStream os = new FastByteArrayOutputStream();
  14. try {
  15. ImageIO.write(image, "jpeg", os);
  16. } catch (IOException e) {
  17. log.error("ImageIO write err", e);
  18. return Mono.error(e);
  19. }
  20. return ServerResponse
  21. .status(HttpStatus.OK)
  22. .contentType(MediaType.IMAGE_JPEG)
  23. .body(BodyInserters.fromResource(new ByteArrayResource(os.toByteArray())));
  24. }
  25. }

② webflux 请求处理入口

  1. public class RouterFunctionConfiguration {
  2. private final HystrixFallbackHandler hystrixFallbackHandler;
  3. private final ImageCodeHandler imageCodeHandler;
  4. @Bean
  5. public RouterFunction routerFunction() {
  6. return RouterFunctions.route(RequestPredicates.GET("/code")
  7. .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), imageCodeHandler);
  8. }
  9. }

③ 校验逻辑,通过oauth2 终端的client-id 来确定是否校验验证码

  1. public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory {
  2. @Override
  3. public GatewayFilter apply(Object config) {
  4. return (exchange, chain) -> {
  5. ServerHttpRequest request = exchange.getRequest();
  6. // 终端设置不校验, 直接向下执行
  7. String[] clientInfos = WebUtils.getClientId(request);
  8. if (filterIgnorePropertiesConfig.getClients().contains(clientInfos[0])) {
  9. return chain.filter(exchange);
  10. }
  11. //校验验证码
  12. checkCode(request);
  13. return chain.filter(exchange);
  14. };
  15. }
  16. }

❤ 问题咨询

手势点击蓝字求关注简约风动态引导关注__2022-09-07+23_18_38.gif