修复服务端当前用户记录错误. (#50)

pull/84/head
chen.ma 3 years ago
parent 2340f7bc8b
commit de28a9dc06

@ -75,7 +75,11 @@ public class JWTAuthorizationFilter extends BasicAuthenticationFilter {
return;
}
super.doFilterInternal(request, response, chain);
try {
super.doFilterInternal(request, response, chain);
} finally {
UserContext.clear();
}
}
/**

@ -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> 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;
}
}

Loading…
Cancel
Save