1、本次使用的圖形驗證碼為 kaptcha
<!--圖形驗證碼 ################### 開始--> <!--<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>--> <!--圖形驗證碼 ################### 結束-->
2、 設置圖形驗證碼樣式以及生成規則
2.1 yaml 文件增加
kaptcha:
#是否有邊框,默認為yes,可選yes、no
border: "yes"
#//邊框顏色
border.color: 105,179,90
textproducer:
font:
#驗證碼字體顏色
color: blue
#文本字符大小
size: 30
#文本字體樣式
names: 宋體,楷體,微軟雅黑
char:
#驗證碼文本字符長度
length: 4
image:
#圖片寬度
width: 120
#圖片高度
height: 40
session:
#存儲session key
key: code
2.2 增加 KaptchaConfig
import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.util.Properties; /** * @program: jfxt-parent * @description: * @author: shiyanling * @create: 2021-01-25 19:35 **/ @Component @RefreshScope public class KaptchaConfig { @Value("${kaptcha.border}") private String border; @Value("${kaptcha.border.color}") private String borderColor; @Value("${kaptcha.textproducer.font.color}") private String textproducerFontColor; @Value("${kaptcha.textproducer.font.size}") private String textproducerFontSize; @Value("${kaptcha.textproducer.font.names}") private String textproducerFontNames; @Value("${kaptcha.textproducer.char.length}") private String textproducerCharLength; @Value("${kaptcha.image.width}") private String imageWidth; @Value("${kaptcha.image.height}") private String imageHeight; @Value("${kaptcha.session.key}") private String sessionKey; @Bean public DefaultKaptcha getDefaultKapcha() { DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); Properties properties = new Properties(); properties.setProperty("kaptcha.border", border); properties.setProperty("kaptcha.border.color", borderColor); properties.setProperty("kaptcha.textproducer.font.color", textproducerFontColor); properties.setProperty("kaptcha.textproducer.font.size", textproducerFontSize); properties.setProperty("kaptcha.textproducer.font.names", textproducerFontNames); properties.setProperty("kaptcha.textproducer.char.length", textproducerCharLength); properties.setProperty("kaptcha.image.width", imageWidth); properties.setProperty("kaptcha.image.height", imageHeight); properties.setProperty("kaptcha.session.key", sessionKey); Config config = new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; } }
2.3 設置handler
import com.google.code.kaptcha.Producer; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.sinoiov.jfxt.common.cache.util.RedisUtils; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ByteArrayResource; 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 javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.UUID; /** * @program: jfxt-parent * @description: * @author: shiyanling * @create: 2021-01-25 16:28 **/ @Slf4j @Component @AllArgsConstructor public class CaptchaImageHandler implements HandlerFunction<ServerResponse> { //隨機數code_key public static final String DEFAULT_CODE_KEY = "random_code_"; //// private final Producer producer; // private final DefaultKaptcha producer; @Autowired DefaultKaptcha defaultKaptcha; @Autowired RedisUtils redisUtils; @Override public Mono<ServerResponse> handle(ServerRequest serverRequest) { // 生成驗證碼 String capText = defaultKaptcha.createText(); // String capStr = capText.substring(0, capText.lastIndexOf("@")); // String code = capText.substring(capText.lastIndexOf("@") + 1); BufferedImage image = defaultKaptcha.createImage(capText); // 保存驗證碼信息 String randomStr = UUID.randomUUID().toString().replaceAll("-", ""); // redisTemplate.opsForValue().set(DEFAULT_CODE_KEY + randomStr, code, 60, TimeUnit.SECONDS); redisUtils.setEx(DEFAULT_CODE_KEY + randomStr, capText,60); // 轉換流信息寫出 FastByteArrayOutputStream os = new FastByteArrayOutputStream(); try { ImageIO.write(image, "jpg", os); } catch (IOException e) { log.error("ImageIO write error", e); return Mono.error(e); } return ServerResponse.status(HttpStatus.OK) .contentType(MediaType.IMAGE_JPEG) .header("randomstr", randomStr) .body(BodyInserters.fromResource(new ByteArrayResource(os.toByteArray()))); } }
2.4 設置網關路由
@Bean
public RouterFunction<ServerResponse> routeFunction(CaptchaImageHandler captchaImageHandler){
return RouterFunctions
.route(RequestPredicates.GET("/code")
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), captchaImageHandler::handle) ;
}
訪問url http://ip:port/code 返回圖形驗證碼
