解决OPTIONS请求被拦截,解决前端传来的Token无法正常解析
This commit is contained in:
parent
e23f3f777d
commit
486b34a35f
1
pom.xml
1
pom.xml
@ -38,6 +38,7 @@
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
<version>1.18.34</version>
|
||||
</dependency>
|
||||
<!-- Thumbnailator -->
|
||||
|
@ -19,6 +19,8 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@ -28,10 +30,14 @@ public class SecurityInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final LinxyunProperties linxyunProperties;
|
||||
|
||||
private Pattern pattern = Pattern.compile("LoginID_\\d{14}_\\d{6}");
|
||||
|
||||
|
||||
// 生命周期: 拦截器在请求处理之前调用,只有返回true才会继续调用下一个拦截器或者处理器,否则不会调用
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
// 跨域请求会首先发送一个OPTIONS请求,这里我们给OPTIONS请求直接返回正常状态
|
||||
if (request.getMethod().equals("OPTIONS")) return true;
|
||||
log.info("鉴权拦截:{} {}", request.getMethod(), request.getRequestURI());
|
||||
|
||||
// 获取请求头上的Token
|
||||
@ -39,15 +45,22 @@ public class SecurityInterceptor implements HandlerInterceptor {
|
||||
if (StringUtils.isEmpty(token)) {
|
||||
token = request.getParameter("Token");
|
||||
}
|
||||
log.info("请求头中Token:{}", token);
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setContentType("application/json; charset=utf-8");
|
||||
Result<JSONObject> result;
|
||||
if (StringUtils.isEmpty(token)) {
|
||||
log.info("请求头中无Authorization信息");
|
||||
log.info("请求头中无 Token 信息");
|
||||
result = Result.error(ErrorCode.USER_NOT_LOGGED_IN);
|
||||
response.getWriter().write(JSON.toJSONString(result));
|
||||
return false;
|
||||
}
|
||||
if (token.startsWith("Session")) {
|
||||
Matcher matcher = pattern.matcher(token);
|
||||
if (matcher.find()) {
|
||||
token = matcher.group();
|
||||
}
|
||||
}
|
||||
UserAuth userAuth = ApiUtils.getUserAuth(token);
|
||||
if (userAuth == null) {
|
||||
// 如果为空,说明 token 无效
|
||||
|
@ -13,6 +13,7 @@ import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
@Slf4j
|
||||
|
@ -13,10 +13,11 @@ import java.util.Map;
|
||||
public class HttpUtils {
|
||||
|
||||
// 通用请求方法,处理所有的 GET 和 POST 请求
|
||||
private static JSONObject executeRequest(HttpRequest HttpRequest) {
|
||||
try (HttpResponse response = HttpRequest.execute()) {
|
||||
private static JSONObject executeRequest(HttpRequest request) {
|
||||
try (HttpResponse response = request.execute()) {
|
||||
log.info("HttpRequest:{} {}", request.getUrl(), request.getMethod());
|
||||
if (!response.isOk()) {
|
||||
log.error("HttpRequest failed: {} {}", response.getStatus(), response.body());
|
||||
log.error("HttpRequest failed: {}", response.getStatus());
|
||||
return null;
|
||||
}
|
||||
if (response.body() == null) {
|
||||
|
Loading…
Reference in New Issue
Block a user