diff --git a/pigx-gateway/pom.xml b/pigx-gateway/pom.xml index 50a576a03..a74579e6e 100644 --- a/pigx-gateway/pom.xml +++ b/pigx-gateway/pom.xml @@ -72,6 +72,12 @@ captcha-spring-boot-starter ${captcha.version} + + + io.springboot.plugin + captcha-core + ${captcha.image.version} + org.springframework.boot 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 f74edccc1..863ca4cb5 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 @@ -19,6 +19,7 @@ package com.pig4cloud.pigx.gateway.config; import com.pig4cloud.pigx.gateway.handler.ImageCodeCheckHandler; import com.pig4cloud.pigx.gateway.handler.ImageCodeCreateHandler; +import com.pig4cloud.pigx.gateway.handler.ImageCodeMathHandler; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; @@ -41,13 +42,17 @@ public class RouterFunctionConfiguration { private final ImageCodeCreateHandler imageCodeCreateHandler; + private final ImageCodeMathHandler imageCodeMathHandler; + @Bean public RouterFunction routerFunction() { return RouterFunctions - .route(RequestPredicates.path("/code/create").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), - imageCodeCreateHandler) - .andRoute(RequestPredicates.POST("/code/check").and(RequestPredicates.accept(MediaType.ALL)), - imageCodeCheckHandler); + .route(RequestPredicates.path("/code/create").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), + imageCodeCreateHandler) + .andRoute(RequestPredicates.path("/code/image").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), + imageCodeMathHandler) + .andRoute(RequestPredicates.POST("/code/check").and(RequestPredicates.accept(MediaType.ALL)), + imageCodeCheckHandler); } diff --git a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeMathHandler.java b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeMathHandler.java new file mode 100755 index 000000000..48c0c7c15 --- /dev/null +++ b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/handler/ImageCodeMathHandler.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.pig4cloud.pigx.gateway.handler; + +import com.pig4cloud.pigx.common.core.constant.CacheConstants; +import com.pig4cloud.pigx.common.core.constant.SecurityConstants; +import io.springboot.captcha.ArithmeticCaptcha; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.data.redis.core.RedisTemplate; +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.Optional; +import java.util.concurrent.TimeUnit; + +/** + * @author lengleng + * @date 2023-09-29 数据验证码实现 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class ImageCodeMathHandler implements HandlerFunction { + + private static final Integer DEFAULT_IMAGE_WIDTH = 100; + + private static final Integer DEFAULT_IMAGE_HEIGHT = 40; + + private final RedisTemplate redisTemplate; + + @Override + public Mono handle(ServerRequest serverRequest) { + ArithmeticCaptcha captcha = new ArithmeticCaptcha(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT); + + String result = captcha.text(); + + // 保存验证码信息 + Optional randomStr = serverRequest.queryParam("randomStr"); + randomStr.ifPresent(s -> redisTemplate.opsForValue() + .set(CacheConstants.DEFAULT_CODE_KEY + s, 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()))); + } + +} diff --git a/pom.xml b/pom.xml index 144eebc40..ba125ca34 100644 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,7 @@ 2.2.5 2.7.10 1.2.7 + 2.2.3 3.6.4 2.0.0 3.0.0