Browse Source

add:添加图片验证码

zhouzeyu 5 months ago
parent
commit
68b293a6dd
21 changed files with 281 additions and 2 deletions
  1. 17 0
      tr-dependencies/pom.xml
  2. 1 0
      tr-modules/pom.xml
  3. 1 1
      tr-modules/tr-module-smartFollowUp/src/main/java/cn/tr/module/smart/coze/controller/CozeController.java
  4. 59 0
      tr-modules/tr-modules-tianAi/pom.xml
  5. 33 0
      tr-modules/tr-modules-tianAi/src/main/java/cn/tr/module/tianai/config/CaptchaResourceConfiguration.java
  6. 46 0
      tr-modules/tr-modules-tianAi/src/main/java/cn/tr/module/tianai/config/CorsPropConfiguration.java
  7. 91 0
      tr-modules/tr-modules-tianAi/src/main/java/cn/tr/module/tianai/controller/CaptchaController.java
  8. BIN
      tr-modules/tr-modules-tianAi/src/main/resources/bgimages/48.jpg
  9. BIN
      tr-modules/tr-modules-tianAi/src/main/resources/bgimages/a.jpg
  10. BIN
      tr-modules/tr-modules-tianAi/src/main/resources/bgimages/b.jpg
  11. BIN
      tr-modules/tr-modules-tianAi/src/main/resources/bgimages/c.jpg
  12. BIN
      tr-modules/tr-modules-tianAi/src/main/resources/bgimages/d.jpg
  13. BIN
      tr-modules/tr-modules-tianAi/src/main/resources/bgimages/e.jpg
  14. BIN
      tr-modules/tr-modules-tianAi/src/main/resources/bgimages/g.jpg
  15. BIN
      tr-modules/tr-modules-tianAi/src/main/resources/bgimages/h.jpg
  16. BIN
      tr-modules/tr-modules-tianAi/src/main/resources/bgimages/i.jpg
  17. BIN
      tr-modules/tr-modules-tianAi/src/main/resources/bgimages/j.jpg
  18. 5 0
      tr-test/pom.xml
  19. 5 0
      tr-test/src/main/resources/application-doc.yml
  20. 22 0
      tr-test/src/main/resources/application-tianai.yml
  21. 1 1
      tr-test/src/main/resources/application.yml

+ 17 - 0
tr-dependencies/pom.xml

@@ -92,6 +92,10 @@
         <coze.version>LATEST</coze.version>
 
         <spring-boot.version>2.7.8</spring-boot.version>
+
+        <thymeleaf.version>2.5.15</thymeleaf.version>
+
+        <tianai.version>1.5.1</tianai.version>
     </properties>
 
 
@@ -574,6 +578,19 @@
                 </exclusions>
             </dependency>
 
+
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-starter-thymeleaf</artifactId>
+                <version>${thymeleaf.version}</version>
+            </dependency>
+
+            <dependency>
+                <groupId>cloud.tianai.captcha</groupId>
+                <artifactId>tianai-captcha-springboot-starter</artifactId>
+                <version>${tianai.version}</version>
+            </dependency>
+
         </dependencies>
     </dependencyManagement>
 </project>

+ 1 - 0
tr-modules/pom.xml

@@ -18,6 +18,7 @@
         <module>tr-module-export</module>
         <module>tr-module-quartz</module>
         <module>tr-module-smartFollowUp</module>
+        <module>tr-modules-tianai</module>
     </modules>
 
 

+ 1 - 1
tr-modules/tr-module-smartFollowUp/src/main/java/cn/tr/module/smart/coze/controller/CozeController.java

@@ -30,7 +30,7 @@ public class CozeController {
     private CozeService cozeService;
 
     @PostMapping("/recognize")
-    @ApiOperation("识别患者图片信息")
+    @ApiOperation(value = "识别患者图片信息",notes = "权限: 无")
     public CommonResult<Map<String, Object>> recognizeImage(@RequestParam MultipartFile file,
                                                             @RequestParam(required = false) String configId,
                                                             @RequestParam(required = false) String cateId) throws Exception {

+ 59 - 0
tr-modules/tr-modules-tianAi/pom.xml

@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>tr-modules</artifactId>
+        <groupId>cn.tr</groupId>
+        <version>0.0.9</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>tr-modile-tianai</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-thymeleaf</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>cloud.tianai.captcha</groupId>
+            <artifactId>tianai-captcha-springboot-starter</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>cn.tr</groupId>
+            <artifactId>tr-spring-boot-starter-plugin-doc</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>cn.tr</groupId>
+            <artifactId>tr-spring-boot-starter-plugin-satoken</artifactId>
+        </dependency>
+    </dependencies>
+
+
+</project>

+ 33 - 0
tr-modules/tr-modules-tianAi/src/main/java/cn/tr/module/tianai/config/CaptchaResourceConfiguration.java

@@ -0,0 +1,33 @@
+package cn.tr.module.tianai.config;
+
+import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
+import cloud.tianai.captcha.resource.ResourceStore;
+import cloud.tianai.captcha.resource.common.model.dto.Resource;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+
+@Component
+@RequiredArgsConstructor
+public class CaptchaResourceConfiguration {
+
+    private final ResourceStore resourceStore;
+
+    @PostConstruct
+    public void init() {
+        // 2. 添加自定义背景图片
+       resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/a.jpg", "default"));
+       resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/b.jpg", "default"));
+       resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/c.jpg", "default"));
+       resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/d.jpg", "default"));
+       resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/e.jpg", "default"));
+       resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/g.jpg", "default"));
+       resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/h.jpg", "default"));
+       resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/i.jpg", "default"));
+       resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/j.jpg", "default"));
+       resourceStore.addResource(CaptchaTypeConstant.ROTATE, new Resource("classpath", "bgimages/48.jpg", "default"));
+       resourceStore.addResource(CaptchaTypeConstant.CONCAT, new Resource("classpath", "bgimages/48.jpg", "default"));
+       resourceStore.addResource(CaptchaTypeConstant.WORD_IMAGE_CLICK, new Resource("classpath", "bgimages/c.jpg", "default"));
+    }
+}

+ 46 - 0
tr-modules/tr-modules-tianAi/src/main/java/cn/tr/module/tianai/config/CorsPropConfiguration.java

@@ -0,0 +1,46 @@
+package cn.tr.module.tianai.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
+import org.springframework.web.util.pattern.PathPatternParser;
+
+import java.util.List;
+
+/**
+ * 该类用于设置跨域
+ */
+@Configuration
+public class CorsPropConfiguration {
+
+
+    @Bean
+    public FilterRegistrationBean coreWebFilter(CorsProperties corsProperties) {
+        CorsConfiguration config = new CorsConfiguration();
+        // * 号表示匹配任意的
+        config.setAllowedMethods(corsProperties.getControlAllowMethods());
+        config.setAllowedOrigins(corsProperties.getControlAllowOrigin());
+        config.setAllowedHeaders(corsProperties.getControlAllowHeaders());
+        PathPatternParser patternParser = new PathPatternParser();
+        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(patternParser);
+        // ** 代表所有
+        source.registerCorsConfiguration("/**", config);
+        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
+        bean.setOrder(0);
+        return bean;
+    }
+
+    @Data
+    @Configuration
+    @ConfigurationProperties(prefix = "cors")
+    public static class CorsProperties {
+        private List<String> controlAllowHeaders;
+        private List<String> controlAllowMethods;
+        private List<String> controlAllowOrigin;
+    }
+}

+ 91 - 0
tr-modules/tr-modules-tianAi/src/main/java/cn/tr/module/tianai/controller/CaptchaController.java

@@ -0,0 +1,91 @@
+package cn.tr.module.tianai.controller;
+
+
+import cloud.tianai.captcha.application.ImageCaptchaApplication;
+import cloud.tianai.captcha.application.vo.CaptchaResponse;
+import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
+import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
+import cloud.tianai.captcha.common.response.ApiResponse;
+import cloud.tianai.captcha.spring.plugins.secondary.SecondaryVerificationApplication;
+import cloud.tianai.captcha.validator.common.model.dto.ImageCaptchaTrack;
+import cn.dev33.satoken.annotation.SaIgnore;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.Collections;
+import java.util.concurrent.ThreadLocalRandom;
+
+@RestController("CaptchaController1")
+@RequestMapping("/tianai")
+@Api("测试")
+public class CaptchaController {
+
+
+    @Autowired
+    private ImageCaptchaApplication imageCaptchaApplication;
+
+    @RequestMapping("/gen")
+    @ResponseBody
+    @SaIgnore
+    @ApiOperation(value = "生成验证码",notes = "权限: 无")
+    public CaptchaResponse<ImageCaptchaVO> genCaptcha(HttpServletRequest request, @RequestParam(value = "type", required = false)String type) {
+        if (StringUtils.isBlank(type)) {
+            type = CaptchaTypeConstant.SLIDER;
+        }
+        if ("RANDOM".equals(type)) {
+            int i = ThreadLocalRandom.current().nextInt(0, 4);
+            if (i == 0) {
+                type = CaptchaTypeConstant.SLIDER;
+            } else if (i == 1) {
+                type = CaptchaTypeConstant.CONCAT;
+            } else if (i == 2) {
+                type = CaptchaTypeConstant.ROTATE;
+            } else{
+                type = CaptchaTypeConstant.WORD_IMAGE_CLICK;
+            }
+
+        }
+        CaptchaResponse<ImageCaptchaVO> response = imageCaptchaApplication.generateCaptcha(type);
+        return response;
+    }
+
+    @PostMapping("/check")
+    @ResponseBody
+    @SaIgnore
+    @ApiOperation(value = "验证码校验",notes = "权限: 无")
+    public ApiResponse<?> checkCaptcha(@RequestBody Data data,
+                                    HttpServletRequest request) {
+        ApiResponse<?> response = imageCaptchaApplication.matching(data.getId(), data.getData());
+        if (response.isSuccess()) {
+            return ApiResponse.ofSuccess(Collections.singletonMap("id", data.getId()));
+        }
+        return response;
+    }
+
+    @lombok.Data
+    public static class Data {
+        private String  id;
+        private ImageCaptchaTrack data;
+    }
+
+    /**
+     * 二次验证,一般用于机器内部调用,这里为了方便测试
+     * @param id id
+     * @return boolean
+     */
+    @GetMapping("/check2")
+    @ResponseBody
+    @SaIgnore
+    @ApiOperation(value = "二次验证",notes = "权限: 无")
+    public boolean check2Captcha(@RequestParam("id") String id) {
+        // 如果开启了二次验证
+        if (imageCaptchaApplication instanceof SecondaryVerificationApplication) {
+            return ((SecondaryVerificationApplication) imageCaptchaApplication).secondaryVerification(id);
+        }
+        return false;
+    }
+}

BIN
tr-modules/tr-modules-tianAi/src/main/resources/bgimages/48.jpg


BIN
tr-modules/tr-modules-tianAi/src/main/resources/bgimages/a.jpg


BIN
tr-modules/tr-modules-tianAi/src/main/resources/bgimages/b.jpg


BIN
tr-modules/tr-modules-tianAi/src/main/resources/bgimages/c.jpg


BIN
tr-modules/tr-modules-tianAi/src/main/resources/bgimages/d.jpg


BIN
tr-modules/tr-modules-tianAi/src/main/resources/bgimages/e.jpg


BIN
tr-modules/tr-modules-tianAi/src/main/resources/bgimages/g.jpg


BIN
tr-modules/tr-modules-tianAi/src/main/resources/bgimages/h.jpg


BIN
tr-modules/tr-modules-tianAi/src/main/resources/bgimages/i.jpg


BIN
tr-modules/tr-modules-tianAi/src/main/resources/bgimages/j.jpg


+ 5 - 0
tr-test/pom.xml

@@ -87,6 +87,11 @@
             <artifactId>flyway-core</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>cn.tr</groupId>
+            <artifactId>tr-modile-tianai</artifactId>
+            <version>0.0.9</version>
+        </dependency>
     </dependencies>
 
 

+ 5 - 0
tr-test/src/main/resources/application-doc.yml

@@ -35,6 +35,11 @@ knife4j:
         api-rule: package
         api-rule-resources:
           - cn.tr.module.smart.app
+      tianai:
+        group-name: 图片验证码
+        api-rule: package
+        api-rule-resources:
+          - cn.tr.module.tianai
   setting:
     enable-footer: false
     enable-footer-custom: true

+ 22 - 0
tr-test/src/main/resources/application-tianai.yml

@@ -0,0 +1,22 @@
+spring:
+  thymeleaf:
+    cache: false
+    prefix: classpath:/templates/
+    suffix: .html
+    mode: HTML #LEGACYHTML5
+captcha:
+  expire:
+    default: 10000
+    # 文字点选验证码 过期时间设置大一点
+    WORD_IMAGE_CLICK: 60000
+  secondary:
+    enabled: false
+  # 初始化默认模板文件
+  init-default-resource: true
+  local-cache-enabled: true
+  local-cache-size: 20
+
+cors:
+  controlAllowHeaders: "*"
+  controlAllowMethods: "*"
+  controlAllowOrigin: "*"

+ 1 - 1
tr-test/src/main/resources/application.yml

@@ -22,7 +22,7 @@ spring:
 #      7天
       time-to-live: 604800000
   profiles:
-    include: doc,sys,stream
+    include: doc,sys,stream,tianai
     active: dev
   jackson:
     date-format: yyyy-MM-dd HH:mm:ss