|
|
@@ -1,17 +1,21 @@
|
|
|
package com.nb.web.service.system.controller;
|
|
|
|
|
|
+import com.anji.captcha.model.common.ResponseModel;
|
|
|
+import com.anji.captcha.model.vo.CaptchaVO;
|
|
|
+import com.anji.captcha.service.CaptchaService;
|
|
|
+import com.anji.captcha.util.StringUtils;
|
|
|
import com.nb.core.exception.CustomException;
|
|
|
+import com.nb.core.result.R;
|
|
|
import com.nb.web.service.system.properties.CaptchaProperties;
|
|
|
import com.nb.web.service.system.utils.ArithmeticCaptcha;
|
|
|
import com.nb.web.service.system.utils.CaptchaTool;
|
|
|
import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiModelProperty;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestParam;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
@@ -37,6 +41,7 @@ public class SysCaptchaController {
|
|
|
|
|
|
@GetMapping
|
|
|
@ApiOperation("获取验证码")
|
|
|
+ @Deprecated
|
|
|
public void get(@RequestParam("key")String key, HttpServletRequest request, HttpServletResponse response){
|
|
|
// 算术类型
|
|
|
ArithmeticCaptcha captcha = new ArithmeticCaptcha(captchaProperties.getWidth(), captchaProperties.getHeight());
|
|
|
@@ -48,4 +53,55 @@ public class SysCaptchaController {
|
|
|
throw new CustomException("系统繁忙,请稍后再试");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CaptchaService captchaService;
|
|
|
+
|
|
|
+ @PostMapping("/get")
|
|
|
+ @ApiOperation("获取行为验证码")
|
|
|
+ public R get(@RequestBody CaptchaVO data, HttpServletRequest request) {
|
|
|
+ assert request.getRemoteHost()!=null;
|
|
|
+ data.setBrowserInfo(getRemoteId(request));
|
|
|
+ ResponseModel responseModel = captchaService.get(data);
|
|
|
+ if("0000".equals(responseModel.getRepCode())){
|
|
|
+ return R.success(responseModel.getRepData());
|
|
|
+ }else {
|
|
|
+ return R.fail(responseModel.getRepMsg());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/check")
|
|
|
+ @ApiOperation("检查行为验证接口")
|
|
|
+ public R check(@RequestBody CaptchaVO data, HttpServletRequest request) {
|
|
|
+ data.setBrowserInfo(getRemoteId(request));
|
|
|
+ ResponseModel responseModel = captchaService.check(data);
|
|
|
+ if("0000".equals(responseModel.getRepCode())){
|
|
|
+ return R.success(responseModel.getRepData());
|
|
|
+ }else {
|
|
|
+ return R.fail(responseModel.getRepMsg());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //@PostMapping("/verify")
|
|
|
+ public ResponseModel verify(@RequestBody CaptchaVO data, HttpServletRequest request) {
|
|
|
+ return captchaService.verification(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String getRemoteId(HttpServletRequest request) {
|
|
|
+ String xfwd = request.getHeader("X-Forwarded-For");
|
|
|
+ String ip = getRemoteIpFromXfwd(xfwd);
|
|
|
+ String ua = request.getHeader("user-agent");
|
|
|
+ if (StringUtils.isNotBlank(ip)) {
|
|
|
+ return ip + ua;
|
|
|
+ }
|
|
|
+ return request.getRemoteAddr() + ua;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getRemoteIpFromXfwd(String xfwd) {
|
|
|
+ if (StringUtils.isNotBlank(xfwd)) {
|
|
|
+ String[] ipList = xfwd.split(",");
|
|
|
+ return StringUtils.trim(ipList[0]);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|