Commit 860b0301b7bf2c9912d6337b0c9dfaad6e3c4902
1 parent
fffe3008
refactor: captcha-plus
Showing
2 changed files
with
23 additions
and
7 deletions
yudao-dependencies/pom.xml
| ... | ... | @@ -41,7 +41,7 @@ |
| 41 | 41 | <!-- Bpm 工作流相关 --> |
| 42 | 42 | <flowable.version>6.8.0</flowable.version> |
| 43 | 43 | <!-- 工具类相关 --> |
| 44 | - <captcha-plus.version>0.0.1</captcha-plus.version> | |
| 44 | + <captcha-plus.version>1.0.0</captcha-plus.version> | |
| 45 | 45 | <jsoup.version>1.15.3</jsoup.version> |
| 46 | 46 | <lombok.version>1.18.24</lombok.version> |
| 47 | 47 | <mapstruct.version>1.5.3.Final</mapstruct.version> | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/captcha/CaptchaController.java
| 1 | 1 | package cn.iocoder.yudao.module.system.controller.admin.captcha; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.util.StrUtil; | |
| 4 | +import cn.hutool.extra.servlet.ServletUtil; | |
| 3 | 5 | import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; |
| 4 | 6 | import com.anji.captcha.model.common.ResponseModel; |
| 5 | 7 | import com.anji.captcha.model.vo.CaptchaVO; |
| 8 | +import com.anji.captcha.service.CaptchaService; | |
| 6 | 9 | import io.swagger.annotations.Api; |
| 7 | 10 | import io.swagger.annotations.ApiOperation; |
| 8 | 11 | import org.springframework.web.bind.annotation.PostMapping; |
| ... | ... | @@ -10,28 +13,31 @@ import org.springframework.web.bind.annotation.RequestBody; |
| 10 | 13 | import org.springframework.web.bind.annotation.RequestMapping; |
| 11 | 14 | import org.springframework.web.bind.annotation.RestController; |
| 12 | 15 | |
| 16 | +import javax.annotation.Resource; | |
| 13 | 17 | import javax.annotation.security.PermitAll; |
| 14 | 18 | import javax.servlet.http.HttpServletRequest; |
| 15 | 19 | |
| 16 | 20 | /** |
| 17 | 21 | * 验证码 |
| 18 | 22 | * |
| 19 | - * 问题:为什么不直接使用 anji 提供的 CaptchaController,而要另外继承? | |
| 20 | - * 回答:管理使用 /admin-api/* 作为前缀,所以需要继承! | |
| 21 | - * | |
| 22 | 23 | * @author 芋道源码 |
| 23 | 24 | */ |
| 24 | 25 | @Api(tags = "管理后台 - 验证码") |
| 25 | 26 | @RestController("adminCaptchaController") |
| 26 | 27 | @RequestMapping("/system/captcha") |
| 27 | -public class CaptchaController extends com.anji.captcha.controller.CaptchaController { | |
| 28 | +public class CaptchaController { | |
| 29 | + | |
| 30 | + @Resource | |
| 31 | + private CaptchaService captchaService; | |
| 28 | 32 | |
| 29 | 33 | @PostMapping({"/get"}) |
| 30 | 34 | @ApiOperation("获得验证码") |
| 31 | 35 | @PermitAll |
| 32 | 36 | @OperateLog(enable = false) // 避免 Post 请求被记录操作日志 |
| 33 | 37 | public ResponseModel get(@RequestBody CaptchaVO data, HttpServletRequest request) { |
| 34 | - return super.get(data, request); | |
| 38 | + assert request.getRemoteHost() != null; | |
| 39 | + data.setBrowserInfo(getRemoteId(request)); | |
| 40 | + return captchaService.get(data); | |
| 35 | 41 | } |
| 36 | 42 | |
| 37 | 43 | @PostMapping("/check") |
| ... | ... | @@ -39,7 +45,17 @@ public class CaptchaController extends com.anji.captcha.controller.CaptchaContro |
| 39 | 45 | @PermitAll |
| 40 | 46 | @OperateLog(enable = false) // 避免 Post 请求被记录操作日志 |
| 41 | 47 | public ResponseModel check(@RequestBody CaptchaVO data, HttpServletRequest request) { |
| 42 | - return super.check(data, request); | |
| 48 | + data.setBrowserInfo(getRemoteId(request)); | |
| 49 | + return captchaService.check(data); | |
| 50 | + } | |
| 51 | + | |
| 52 | + public static String getRemoteId(HttpServletRequest request) { | |
| 53 | + String ip = ServletUtil.getClientIP(request); | |
| 54 | + String ua = request.getHeader("user-agent"); | |
| 55 | + if (StrUtil.isNotBlank(ip)) { | |
| 56 | + return ip + ua; | |
| 57 | + } | |
| 58 | + return request.getRemoteAddr() + ua; | |
| 43 | 59 | } |
| 44 | 60 | |
| 45 | 61 | } | ... | ... |