java获取AuthenticationFacade中的登录用户
我遵循教程获取用户,并创建了一个facade类
@Component
public class AuthenticationFacade implements IAuthenticationFacade {
@Override
public User getAuthentication() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return (User) authentication.getDetails();
}
}
在我的服务类中,我试图使用
import org.springframework.security.core.userdetails.User;
User loggedUser = authenticationFacade.getAuthentication();
但我有个例外
java.lang.ClassCastException: com.ssssseeee.services.tool.security.TokenBasedUserDetails cannot be cast to org.springframework.security.core.userdetails.User
尝试了很多方法向用户强制转换身份验证,以获取获取主体对象的详细信息,但我不知道如何强制转换到此用户类型
# 1 楼答案
将此
User loggedUser = authenticationFacade.getAuthentication();
更改为TokenBasedUserDetails loggedUser = (TokenBasedUserDetails )authenticationFacade.getAuthentication();
,然后使用loggedUser
方法检索有关当前登录用户的信息