修复 匿名用户会被鉴定为anonymousUser

pull/2/head
niudehua 2021-03-09 20:08:44 +08:00
parent 61c7687473
commit 4749090fc0
1 changed files with 6 additions and 5 deletions

View File

@ -20,13 +20,14 @@ import java.util.Set;
*/ */
public class SecurityFrameworkUtils { public class SecurityFrameworkUtils {
private SecurityFrameworkUtils() {} private SecurityFrameworkUtils() {
}
/** /**
* Token * Token
* *
* @param request * @param request
* @param header Token Header * @param header Token Header
* @return Token * @return Token
*/ */
public static String obtainAuthorization(HttpServletRequest request, String header) { public static String obtainAuthorization(HttpServletRequest request, String header) {
@ -56,7 +57,7 @@ public class SecurityFrameworkUtils {
if (authentication == null) { if (authentication == null) {
return null; return null;
} }
return (LoginUser) authentication.getPrincipal(); return authentication.getPrincipal() instanceof LoginUser ? (LoginUser) authentication.getPrincipal() : null;
} }
/** /**
@ -85,12 +86,12 @@ public class SecurityFrameworkUtils {
* *
* *
* @param loginUser * @param loginUser
* @param request * @param request
*/ */
public static void setLoginUser(LoginUser loginUser, HttpServletRequest request) { public static void setLoginUser(LoginUser loginUser, HttpServletRequest request) {
// 创建 UsernamePasswordAuthenticationToken 对象 // 创建 UsernamePasswordAuthenticationToken 对象
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
loginUser, null, null); loginUser, null, null);
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
// 设置到上下文 // 设置到上下文
SecurityContextHolder.getContext().setAuthentication(authenticationToken); SecurityContextHolder.getContext().setAuthentication(authenticationToken);