Compare commits
2 Commits
master
...
springboot
Author | SHA1 | Date | |
---|---|---|---|
22517078e4 | |||
127bb80997 |
@ -71,6 +71,7 @@ public enum ErrorCode {
|
|||||||
|
|
||||||
USER_NO_AUTHORITY("1025", "用户没有权限"),
|
USER_NO_AUTHORITY("1025", "用户没有权限"),
|
||||||
|
|
||||||
|
|
||||||
// 用户组已经存在
|
// 用户组已经存在
|
||||||
USER_GROUP_ALREADY_EXISTS("1030", "用户组已经存在"),
|
USER_GROUP_ALREADY_EXISTS("1030", "用户组已经存在"),
|
||||||
|
|
||||||
|
34
src/main/java/com/linxyun/core/holder/TokenHolder.java
Normal file
34
src/main/java/com/linxyun/core/holder/TokenHolder.java
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package com.linxyun.core.holder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TokenHolder: 用于在当前线程中存储和管理 Token。
|
||||||
|
*/
|
||||||
|
public class TokenHolder {
|
||||||
|
// 使用 ThreadLocal 并设置默认值
|
||||||
|
private static ThreadLocal<String> threadLocal = ThreadLocal.withInitial(() -> null);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前线程存储的 Token。
|
||||||
|
* @return 当前线程的 Token,可能为 null。
|
||||||
|
*/
|
||||||
|
public static String get() {
|
||||||
|
return threadLocal.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为当前线程设置 Token。
|
||||||
|
* @param token 要存储的 Token。
|
||||||
|
*/
|
||||||
|
public static void set(String token) {
|
||||||
|
threadLocal.set(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移除当前线程存储的 Token。
|
||||||
|
* 避免线程池复用导致的潜在内存泄漏。
|
||||||
|
*/
|
||||||
|
public static void remove() {
|
||||||
|
threadLocal.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,7 @@ import com.alibaba.fastjson2.JSONObject;
|
|||||||
import com.linxyun.core.common.entity.Result;
|
import com.linxyun.core.common.entity.Result;
|
||||||
import com.linxyun.core.common.entity.UserAuth;
|
import com.linxyun.core.common.entity.UserAuth;
|
||||||
import com.linxyun.core.common.enums.ErrorCode;
|
import com.linxyun.core.common.enums.ErrorCode;
|
||||||
|
import com.linxyun.core.holder.TokenHolder;
|
||||||
import com.linxyun.core.properties.LinxyunProperties;
|
import com.linxyun.core.properties.LinxyunProperties;
|
||||||
import com.linxyun.core.utils.ApiUtils;
|
import com.linxyun.core.utils.ApiUtils;
|
||||||
import com.linxyun.core.utils.URLUtils;
|
import com.linxyun.core.utils.URLUtils;
|
||||||
@ -39,7 +40,6 @@ public class SecurityInterceptor implements HandlerInterceptor {
|
|||||||
// 跨域请求会首先发送一个OPTIONS请求,这里我们给OPTIONS请求直接返回正常状态
|
// 跨域请求会首先发送一个OPTIONS请求,这里我们给OPTIONS请求直接返回正常状态
|
||||||
if (request.getMethod().equals("OPTIONS")) return true;
|
if (request.getMethod().equals("OPTIONS")) return true;
|
||||||
log.info("鉴权拦截:{} {}", request.getMethod(), request.getRequestURI());
|
log.info("鉴权拦截:{} {}", request.getMethod(), request.getRequestURI());
|
||||||
|
|
||||||
// 获取请求头上的Token
|
// 获取请求头上的Token
|
||||||
String token = request.getHeader("Token");
|
String token = request.getHeader("Token");
|
||||||
if (StringUtils.isEmpty(token)) {
|
if (StringUtils.isEmpty(token)) {
|
||||||
@ -55,6 +55,8 @@ public class SecurityInterceptor implements HandlerInterceptor {
|
|||||||
response.getWriter().write(JSON.toJSONString(result));
|
response.getWriter().write(JSON.toJSONString(result));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// Token 线程存储
|
||||||
|
TokenHolder.set(token);
|
||||||
if (token.startsWith("Session")) {
|
if (token.startsWith("Session")) {
|
||||||
Matcher matcher = pattern.matcher(token);
|
Matcher matcher = pattern.matcher(token);
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
@ -110,8 +112,12 @@ public class SecurityInterceptor implements HandlerInterceptor {
|
|||||||
// 生命周期: 拦截器在视图渲染之后调用,即在视图渲染完成之后,页面响应给客户端之前,只有返回true才会继续调用下一个拦截器或者处理器,否则不会调用
|
// 生命周期: 拦截器在视图渲染之后调用,即在视图渲染完成之后,页面响应给客户端之前,只有返回true才会继续调用下一个拦截器或者处理器,否则不会调用
|
||||||
@Override
|
@Override
|
||||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||||
|
// 清除 Token
|
||||||
|
String token = TokenHolder.get();
|
||||||
|
if (token != null) {
|
||||||
|
log.info("Token removed: {}", token);
|
||||||
|
TokenHolder.remove();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,20 +3,18 @@ package com.linxyun.core.utils;
|
|||||||
import cn.hutool.http.HttpRequest;
|
import cn.hutool.http.HttpRequest;
|
||||||
import cn.hutool.http.HttpResponse;
|
import cn.hutool.http.HttpResponse;
|
||||||
import cn.hutool.http.HttpUtil;
|
import cn.hutool.http.HttpUtil;
|
||||||
import cn.hutool.http.body.RequestBody;
|
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.linxyun.core.common.entity.Result;
|
import com.linxyun.core.common.entity.Result;
|
||||||
import com.linxyun.core.common.entity.UserAuth;
|
import com.linxyun.core.common.entity.UserAuth;
|
||||||
import com.linxyun.core.common.enums.ErrorCode;
|
import com.linxyun.core.common.enums.ErrorCode;
|
||||||
|
import com.linxyun.core.holder.TokenHolder;
|
||||||
import com.linxyun.core.properties.LinxyunProperties;
|
import com.linxyun.core.properties.LinxyunProperties;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.jodah.expiringmap.ExpirationPolicy;
|
import net.jodah.expiringmap.ExpirationPolicy;
|
||||||
import net.jodah.expiringmap.ExpiringMap;
|
import net.jodah.expiringmap.ExpiringMap;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@ -115,6 +113,9 @@ public class ApiUtils {
|
|||||||
|
|
||||||
// 创建请求
|
// 创建请求
|
||||||
HttpRequest request = HttpUtil.createPost(uploadUrl);
|
HttpRequest request = HttpUtil.createPost(uploadUrl);
|
||||||
|
String token = TokenHolder.get();
|
||||||
|
// 添加请求头 Cookie
|
||||||
|
request.header("Cookie", token);
|
||||||
request.form("file", fileBytes, fileName);
|
request.form("file", fileBytes, fileName);
|
||||||
// 执行请求
|
// 执行请求
|
||||||
try (HttpResponse response = request.execute()) {
|
try (HttpResponse response = request.execute()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user