From 6a28df8884fe3ec95a33abc20b774b885cda8511 Mon Sep 17 00:00:00 2001 From: lengleng Date: Tue, 19 May 2020 13:27:46 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Introducing=20new=20features.=20#1?= =?UTF-8?q?245=20=E5=8D=87=E7=BA=A7=E9=AA=8C=E8=AF=81=E7=A0=81=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E6=94=AF=E6=8C=81=20=E8=A1=8C=E4=B8=BA=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/core/constant/CommonConstants.java | 5 ++ pigx-gateway/pom.xml | 4 +- .../config/CaptchaCacheServiceProvider.java | 42 ++++++++++++ .../config/RouterFunctionConfiguration.java | 12 ++-- .../filter/ValidateCodeGatewayFilter.java | 20 +++++- .../handler/ImageCodeCheckHandler.java | 66 +++++++++++++++++++ ...ndler.java => ImageCodeCreateHandler.java} | 51 ++++++-------- pom.xml | 2 +- 8 files changed, 160 insertions(+), 42 deletions(-) create mode 100644 pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/CaptchaCacheServiceProvider.java create mode 100644 pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeCheckHandler.java rename pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/{ImageCodeHandler.java => ImageCodeCreateHandler.java} (52%) diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CommonConstants.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CommonConstants.java index 8c6c24b35..2fff0f48d 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CommonConstants.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/CommonConstants.java @@ -91,4 +91,9 @@ public interface CommonConstants { * 默认存储bucket */ String BUCKET_NAME = "lengleng"; + + /** + * 滑块验证码 + */ + String IMAGE_CODE_TYPE = "blockPuzzle"; } diff --git a/pigx-gateway/pom.xml b/pigx-gateway/pom.xml index 80f58f299..fb6e4ba36 100644 --- a/pigx-gateway/pom.xml +++ b/pigx-gateway/pom.xml @@ -59,8 +59,8 @@ - com.github.whvcse - easy-captcha + com.github.anji-plus + captcha ${kaptcha.version} diff --git a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/CaptchaCacheServiceProvider.java b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/CaptchaCacheServiceProvider.java new file mode 100644 index 000000000..398fd4f5d --- /dev/null +++ b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/CaptchaCacheServiceProvider.java @@ -0,0 +1,42 @@ +package com.pig4cloud.pigx.gateway.config; + +import com.anji.captcha.service.CaptchaCacheService; +import lombok.AllArgsConstructor; +import org.springframework.context.annotation.Primary; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Component; + +import java.util.concurrent.TimeUnit; + +/** + * @author lengleng + * @date 2020/5/19 + *

+ * 验证码 缓存提供支持集群 + */ +@Primary +@Component +@AllArgsConstructor +public class CaptchaCacheServiceProvider implements CaptchaCacheService { + private final StringRedisTemplate stringRedisTemplate; + + @Override + public void set(String key, String value, long expiresInSeconds) { + stringRedisTemplate.opsForValue().set(key, value, expiresInSeconds, TimeUnit.SECONDS); + } + + @Override + public boolean exists(String key) { + return stringRedisTemplate.hasKey(key); + } + + @Override + public void delete(String key) { + stringRedisTemplate.delete(key); + } + + @Override + public String get(String key) { + return stringRedisTemplate.opsForValue().get(key); + } +} diff --git a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/RouterFunctionConfiguration.java b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/RouterFunctionConfiguration.java index 2b46c6c55..a8aceb874 100644 --- a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/RouterFunctionConfiguration.java +++ b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/config/RouterFunctionConfiguration.java @@ -17,10 +17,7 @@ package com.pig4cloud.pigx.gateway.config; -import com.pig4cloud.pigx.gateway.handler.ImageCodeHandler; -import com.pig4cloud.pigx.gateway.handler.SwaggerResourceHandler; -import com.pig4cloud.pigx.gateway.handler.SwaggerSecurityHandler; -import com.pig4cloud.pigx.gateway.handler.SwaggerUiHandler; +import com.pig4cloud.pigx.gateway.handler.*; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; @@ -39,16 +36,19 @@ import org.springframework.web.reactive.function.server.RouterFunctions; @Configuration @AllArgsConstructor public class RouterFunctionConfiguration { + private final ImageCodeCheckHandler imageCodeCheckHandler; + private final ImageCodeCreateHandler imageCodeCreateHandler; private final SwaggerResourceHandler swaggerResourceHandler; private final SwaggerSecurityHandler swaggerSecurityHandler; private final SwaggerUiHandler swaggerUiHandler; - private final ImageCodeHandler imageCodeHandler; @Bean public RouterFunction routerFunction() { return RouterFunctions.route( RequestPredicates.path("/code") - .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), imageCodeHandler) + .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), imageCodeCreateHandler) + .andRoute(RequestPredicates.POST("/check") + .and(RequestPredicates.accept(MediaType.ALL)), imageCodeCheckHandler) .andRoute(RequestPredicates.GET("/swagger-resources") .and(RequestPredicates.accept(MediaType.ALL)), swaggerResourceHandler) .andRoute(RequestPredicates.GET("/swagger-resources/configuration/ui") diff --git a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/filter/ValidateCodeGatewayFilter.java b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/filter/ValidateCodeGatewayFilter.java index 020b63618..37a756528 100644 --- a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/filter/ValidateCodeGatewayFilter.java +++ b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/filter/ValidateCodeGatewayFilter.java @@ -18,13 +18,17 @@ package com.pig4cloud.pigx.gateway.filter; import cn.hutool.core.util.StrUtil; +import com.anji.captcha.model.vo.CaptchaVO; +import com.anji.captcha.service.CaptchaService; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.pig4cloud.pigx.common.core.constant.CacheConstants; +import com.pig4cloud.pigx.common.core.constant.CommonConstants; import com.pig4cloud.pigx.common.core.constant.SecurityConstants; import com.pig4cloud.pigx.common.core.constant.enums.LoginTypeEnum; import com.pig4cloud.pigx.common.core.exception.ValidateCodeException; import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.core.util.SpringContextHolder; import com.pig4cloud.pigx.common.core.util.WebUtils; import com.pig4cloud.pigx.gateway.config.FilterIgnorePropertiesConfig; import lombok.AllArgsConstructor; @@ -44,8 +48,8 @@ import reactor.core.publisher.Mono; /** * @author lengleng - * @date 2018/7/4 - * 验证码处理 + * @date 2020/5/19 + * 登录逻辑验证码处理 */ @Slf4j @Component @@ -125,6 +129,18 @@ public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory { String randomStr = request.getQueryParams().getFirst("randomStr"); + // 若是滑块登录 + if (CommonConstants.IMAGE_CODE_TYPE.equalsIgnoreCase(randomStr)) { + CaptchaService captchaService = SpringContextHolder.getBean(CaptchaService.class); + CaptchaVO vo = new CaptchaVO(); + vo.setCaptchaVerification(code); + if (!captchaService.verification(vo).isSuccess()) { + throw new ValidateCodeException("验证码不能为空"); + } + + return; + } + //https://gitee.com/log4j/pig/issues/IWA0D String mobile = request.getQueryParams().getFirst("mobile"); if (StrUtil.isNotBlank(mobile)) { diff --git a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeCheckHandler.java b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeCheckHandler.java new file mode 100644 index 000000000..4de706ce3 --- /dev/null +++ b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeCheckHandler.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.gateway.handler; + +import com.anji.captcha.model.common.ResponseModel; +import com.anji.captcha.model.vo.CaptchaVO; +import com.anji.captcha.service.CaptchaService; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.pig4cloud.pigx.common.core.constant.CommonConstants; +import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.core.util.SpringContextHolder; +import lombok.AllArgsConstructor; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +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 reactor.core.publisher.Mono; + +/** + * @author lengleng + * @date 2020/5/19 + * 验证码生成逻辑处理类 + */ +@Slf4j +@Component +@AllArgsConstructor +public class ImageCodeCheckHandler implements HandlerFunction { + private final ObjectMapper objectMapper; + + + @Override + @SneakyThrows + public Mono handle(ServerRequest request) { + CaptchaVO vo = new CaptchaVO(); + vo.setPointJson(request.queryParam("pointJson").get()); + vo.setToken(request.queryParam("token").get()); + vo.setCaptchaType(CommonConstants.IMAGE_CODE_TYPE); + + CaptchaService captchaService = SpringContextHolder.getBean(CaptchaService.class); + ResponseModel responseModel = captchaService.check(vo); + + return ServerResponse.status(HttpStatus.OK) + .contentType(MediaType.APPLICATION_JSON) + .body(BodyInserters.fromValue(objectMapper.writeValueAsString(R.ok(responseModel)))); + } +} diff --git a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeHandler.java b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeCreateHandler.java similarity index 52% rename from pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeHandler.java rename to pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeCreateHandler.java index 08bf1f210..6840c48e0 100644 --- a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeHandler.java +++ b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeCreateHandler.java @@ -17,58 +17,47 @@ package com.pig4cloud.pigx.gateway.handler; -import com.pig4cloud.pigx.common.core.constant.CacheConstants; -import com.pig4cloud.pigx.common.core.constant.SecurityConstants; -import com.wf.captcha.ArithmeticCaptcha; +import com.anji.captcha.model.common.ResponseModel; +import com.anji.captcha.model.vo.CaptchaVO; +import com.anji.captcha.service.CaptchaService; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.pig4cloud.pigx.common.core.constant.CommonConstants; +import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.core.util.SpringContextHolder; import lombok.AllArgsConstructor; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; -import org.springframework.core.io.ByteArrayResource; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; -import org.springframework.util.FastByteArrayOutputStream; 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 reactor.core.publisher.Mono; -import java.util.concurrent.TimeUnit; - /** * @author lengleng - * @date 2018/7/5 + * @date 2020/5/19 * 验证码生成逻辑处理类 */ @Slf4j @Component @AllArgsConstructor -public class ImageCodeHandler implements HandlerFunction { - private static final Integer DEFAULT_IMAGE_WIDTH = 100; - private static final Integer DEFAULT_IMAGE_HEIGHT = 40; - private final RedisTemplate redisTemplate; +public class ImageCodeCreateHandler implements HandlerFunction { + private final ObjectMapper objectMapper; + @Override + @SneakyThrows public Mono handle(ServerRequest serverRequest) { - ArithmeticCaptcha captcha = new ArithmeticCaptcha(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT); + CaptchaVO vo = new CaptchaVO(); + vo.setCaptchaType(CommonConstants.IMAGE_CODE_TYPE); + CaptchaService captchaService = SpringContextHolder.getBean(CaptchaService.class); + ResponseModel responseModel = captchaService.get(vo); - String result = captcha.text(); - - //保存验证码信息 - String randomStr = serverRequest.queryParam("randomStr").get(); - redisTemplate.setKeySerializer(new StringRedisSerializer()); - redisTemplate.opsForValue().set(CacheConstants.DEFAULT_CODE_KEY + randomStr, result - , SecurityConstants.CODE_TIME, TimeUnit.SECONDS); - - // 转换流信息写出 - FastByteArrayOutputStream os = new FastByteArrayOutputStream(); - captcha.out(os); - - return ServerResponse - .status(HttpStatus.OK) - .contentType(MediaType.IMAGE_JPEG) - .body(BodyInserters.fromResource(new ByteArrayResource(os.toByteArray()))); + return ServerResponse.status(HttpStatus.OK) + .contentType(MediaType.APPLICATION_JSON) + .body(BodyInserters.fromValue(objectMapper.writeValueAsString(R.ok(responseModel)))); } } diff --git a/pom.xml b/pom.xml index aa1992bc9..e43adf248 100644 --- a/pom.xml +++ b/pom.xml @@ -41,7 +41,7 @@ 1.8 2.2.3 5.3.3 - 1.6.2 + 1.1.8 2.9.2 2.0.2 1.7