|
|
@@ -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;
|
|
|
+ }
|
|
|
+}
|