mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 12:39:05 +08:00
✨ Introducing new features. 网关支持数学验证码
This commit is contained in:
1 parent
247da1daf6
commit
4d619e43fb
4 files changed
+90
-4
No files matched your search
@@ -72,6 +72,12 @@
|
||||
<artifactId>captcha-spring-boot-starter</artifactId>
|
||||
<version>${captcha.version}</version>
|
||||
</dependency>
|
||||
<!--图形验证码-->
|
||||
<dependency>
|
||||
<groupId>io.springboot.plugin</groupId>
|
||||
<artifactId>captcha-core</artifactId>
|
||||
<version>${captcha.image.version}</version>
|
||||
</dependency>
|
||||
<!--缓存-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
+9
-4
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
+74
@@ -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<ServerResponse> {
|
||||
|
||||
private static final Integer DEFAULT_IMAGE_WIDTH = 100;
|
||||
|
||||
private static final Integer DEFAULT_IMAGE_HEIGHT = 40;
|
||||
|
||||
private final RedisTemplate redisTemplate;
|
||||
|
||||
@Override
|
||||
public Mono<ServerResponse> handle(ServerRequest serverRequest) {
|
||||
ArithmeticCaptcha captcha = new ArithmeticCaptcha(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT);
|
||||
|
||||
String result = captcha.text();
|
||||
|
||||
// 保存验证码信息
|
||||
Optional<String> 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())));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -45,6 +45,7 @@
|
||||
<git.commit.version>2.2.5</git.commit.version>
|
||||
<spring-boot-admin.version>2.7.10</spring-boot-admin.version>
|
||||
<captcha.version>1.2.7</captcha.version>
|
||||
<captcha.image.version>2.2.3</captcha.image.version>
|
||||
<cas.sdk.version>3.6.4</cas.sdk.version>
|
||||
<dingtalk.old.version>2.0.0</dingtalk.old.version>
|
||||
<swagger.fox.version>3.0.0</swagger.fox.version>
|
||||
|
||||
Reference in new issue
Block a user