6
usign Spring Security,我試圖從我的CustomUserDetailsService上的loadUserByUsername方法返回的CustomUser實例中獲取用戶標識,就像我要獲取名稱(get.Name ())與身份驗證。感謝您的任何提示!如何從CustomUser獲取用戶ID在Spring Security
這是我如何得到登陸用戶的當前名稱:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String name = authentication.getName();
這是CustomUser
public class CustomUser extends User
{
private final int userID;
public CustomUser(String username, String password, boolean enabled, boolean accountNonExpired,
boolean credentialsNonExpired,
boolean accountNonLocked,
Collection<? extends GrantedAuthority> authorities, int userID)
{
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
this.userID = userID;
}
}
而且我的服務
工作就像一個魅力!謝謝 –
'Principal'可以被注入。 – naXa
通過方法參數注入:@AuthenticationPrincipal CustomUser customUser – Tom