潘志宝
2024-11-14 afa8fc57084c423218c6816b710dfb2f0b87ea89
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.controller.admin.captcha;
H 2
3 import cn.hutool.core.util.StrUtil;
4 import com.iailab.framework.common.util.servlet.ServletUtils;
5 import com.xingyuv.captcha.model.common.ResponseModel;
6 import com.xingyuv.captcha.model.vo.CaptchaVO;
7 import com.xingyuv.captcha.service.CaptchaService;
8 import io.swagger.v3.oas.annotations.Operation;
9 import io.swagger.v3.oas.annotations.tags.Tag;
10 import org.springframework.web.bind.annotation.PostMapping;
11 import org.springframework.web.bind.annotation.RequestBody;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.RestController;
14
15 import javax.annotation.Resource;
16 import javax.annotation.security.PermitAll;
17 import javax.servlet.http.HttpServletRequest;
18
19 @Tag(name = "管理后台 - 验证码")
20 @RestController("adminCaptchaController")
21 @RequestMapping("/system/captcha")
22 public class CaptchaController {
23
24     @Resource
25     private CaptchaService captchaService;
26
27     @PostMapping({"/get"})
28     @Operation(summary = "获得验证码")
29     @PermitAll
30     public ResponseModel get(@RequestBody CaptchaVO data, HttpServletRequest request) {
31         assert request.getRemoteHost() != null;
32         data.setBrowserInfo(getRemoteId(request));
33         return captchaService.get(data);
34     }
35
36     @PostMapping("/check")
37     @Operation(summary = "校验验证码")
38     @PermitAll
39     public ResponseModel check(@RequestBody CaptchaVO data, HttpServletRequest request) {
40         data.setBrowserInfo(getRemoteId(request));
41         return captchaService.check(data);
42     }
43
44     public static String getRemoteId(HttpServletRequest request) {
45         String ip = ServletUtils.getClientIP(request);
46         String ua = request.getHeader("user-agent");
47         if (StrUtil.isNotBlank(ip)) {
48             return ip + ua;
49         }
50         return request.getRemoteAddr() + ua;
51     }
52
53 }