diff --git a/hippo4j-auth/src/main/java/cn/hippo4j/auth/filter/JWTAuthorizationFilter.java b/hippo4j-auth/src/main/java/cn/hippo4j/auth/filter/JWTAuthorizationFilter.java index 7be6b2b2..d7e65a87 100644 --- a/hippo4j-auth/src/main/java/cn/hippo4j/auth/filter/JWTAuthorizationFilter.java +++ b/hippo4j-auth/src/main/java/cn/hippo4j/auth/filter/JWTAuthorizationFilter.java @@ -75,7 +75,11 @@ public class JWTAuthorizationFilter extends BasicAuthenticationFilter { return; } - super.doFilterInternal(request, response, chain); + try { + super.doFilterInternal(request, response, chain); + } finally { + UserContext.clear(); + } } /** diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/UserContext.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/UserContext.java index 24b15eb7..89fde6bb 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/UserContext.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/UserContext.java @@ -1,5 +1,9 @@ package cn.hippo4j.common.toolkit; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + /** * User context (Transition scheme). * @@ -8,29 +12,33 @@ package cn.hippo4j.common.toolkit; */ public class UserContext { - private static String username; - - private static String userRole; + private static final ThreadLocal USER_THREAD_LOCAL = new ThreadLocal(); - public static void setUserName(String username) { - UserContext.username = username; + public static void setUserInfo(String username, String userRole) { + USER_THREAD_LOCAL.set(new User(username, userRole)); } - public static void setUserRole(String userRole) { - UserContext.userRole = userRole; + public static String getUserName() { + return USER_THREAD_LOCAL.get().username; } - public static void setUserInfo(String username, String userRole) { - UserContext.username = username; - UserContext.userRole = userRole; + public static String getUserRole() { + return USER_THREAD_LOCAL.get().userRole; } - public static String getUserName() { - return username; + public static void clear() { + USER_THREAD_LOCAL.remove(); } - public static String getUserRole() { - return userRole; + @Data + @NoArgsConstructor + @AllArgsConstructor + static class User { + + private String username; + + private String userRole; + } }