feat: 优化 captcha 代码
parent
865f101070
commit
aaf62982e2
@ -1,65 +0,0 @@
|
|||||||
/*
|
|
||||||
*Copyright © 2018 anji-plus
|
|
||||||
*安吉加加信息技术有限公司
|
|
||||||
*http://www.anji-plus.com
|
|
||||||
*All rights reserved.
|
|
||||||
*/
|
|
||||||
package com.anji.captcha.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 org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/captcha")
|
|
||||||
public class CaptchaController {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private CaptchaService captchaService;
|
|
||||||
|
|
||||||
@PostMapping("/get")
|
|
||||||
public ResponseModel get(@RequestBody CaptchaVO data, HttpServletRequest request) {
|
|
||||||
assert request.getRemoteHost()!=null;
|
|
||||||
data.setBrowserInfo(getRemoteId(request));
|
|
||||||
return captchaService.get(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/check")
|
|
||||||
public ResponseModel check(@RequestBody CaptchaVO data, HttpServletRequest request) {
|
|
||||||
data.setBrowserInfo(getRemoteId(request));
|
|
||||||
return captchaService.check(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
//@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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,53 +0,0 @@
|
|||||||
//
|
|
||||||
// Source code recreated from a .class file by IntelliJ IDEA
|
|
||||||
// (powered by Fernflower decompiler)
|
|
||||||
//
|
|
||||||
|
|
||||||
package com.anji.captcha.util;
|
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Base64;
|
|
||||||
|
|
||||||
public abstract class Base64Utils {
|
|
||||||
private static final Charset DEFAULT_CHARSET;
|
|
||||||
|
|
||||||
public Base64Utils() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] encode(byte[] src) {
|
|
||||||
return src.length == 0 ? src : Base64.getEncoder().encode(src);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] decode(byte[] src) {
|
|
||||||
return src.length == 0 ? src : Base64.getDecoder().decode(src);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] encodeUrlSafe(byte[] src) {
|
|
||||||
return src.length == 0 ? src : Base64.getUrlEncoder().encode(src);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] decodeUrlSafe(byte[] src) {
|
|
||||||
return src.length == 0 ? src : Base64.getUrlDecoder().decode(src);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String encodeToString(byte[] src) {
|
|
||||||
return src.length == 0 ? "" : new String(encode(src), DEFAULT_CHARSET);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] decodeFromString(String src) {
|
|
||||||
return src.isEmpty() ? new byte[0] : decode(src.getBytes(DEFAULT_CHARSET));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String encodeToUrlSafeString(byte[] src) {
|
|
||||||
return new String(encodeUrlSafe(src), DEFAULT_CHARSET);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] decodeFromUrlSafeString(String src) {
|
|
||||||
return decodeUrlSafe(src.getBytes(DEFAULT_CHARSET));
|
|
||||||
}
|
|
||||||
|
|
||||||
static {
|
|
||||||
DEFAULT_CHARSET = StandardCharsets.UTF_8;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue