在春季安全的登录页面,我有公司名称,用户名和密码。 当我点击登录按钮时,它会重定向到index.jsp。我想在index.jsp中访问公司名称。 我正在使用
获取用户名和密码SecurityContextHolder.getContext().getAuthentication().getName();
和
SecurityContextHolder.getContext().getAuthentication().getCredentials();
但我想回复公司名称。怎么做?
答案 0 :(得分:2)
假设您已登录,并且需要获取从数据库检索的公司名称。首先,您需要对将保存在UserDetails
中的实现UserDetailsService
进行反对,并添加您需要的任何变量(公司等)。
public class CustomUserDetails implements UserDetails{
private String password;
private String username;
private String companyName;
}
与使用自定义Userdetails实现进行类型转换相比。
CustomUserDetails customDetails (CustomUserDetails)SecurityContextHolder.getContext().getAuthentication().getCredentials();
getCredentials将返回您保存到凭据的内容Object
。无论何种类型,您都可以将它归还给您保存的课程。