java - 我可以使用 spring security 将完整的用户信息存储在 cookie 中吗?

标签 java cookies spring-security remember-me

我想将其他用户信息存储在 cookie 中,例如带有用户名和密码的 userId。当我使用 spring security 记住我的功能时,我可以从 cookie 中获取用户名。

在 spring-security.xml 中,我使用自定义的 userDetailService 并且我已经实现了它

 <http>
 ......   
 <logout invalidate-session="true" 
        logout-success-url="/" 
        logout-url="/logout.htm"/>
 <remember-me user-service-ref="myUserDetailsService" key="89dqj219dn910lsAc12" token-validity-seconds="864000"/>
</http>

<authentication-manager>
        <authentication-provider user-service-ref="myUserDetailsService">
                <password-encoder ref="myEnocdePassword" >
                    <salt-source user-property="username"/>
                </password-encoder>
        </authentication-provider>
</authentication-manager>   
<beans:bean id="myEnocdePassword" class="com.mycom.myproject.utility.MyEnocdePassword" />

在 MyUserDetailService.java 我有这样的代码

  @Override
public UserDetails loadUserByUsername(String username)
        throws UsernameNotFoundException, DataAccessException {

    try {

    boolean enabled = true;
    boolean accountNonExpired = true;
    boolean credentialsNonExpired = true;
    boolean accountNonLocked = true;

    com.mycom.myproject.db.mybatis.model.User domainUser = userService.getUserByName(username);




    return  new User(
            domainUser.getUsername(), 
            domainUser.getPassword(),
            enabled,
            accountNonExpired,
            credentialsNonExpired,
            accountNonLocked,
            getAuthorities(domainUser.getRoleId);

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

在我的 Controller 类中,我可以使用

获取用户名
   String name = SecurityContextHolder.getContext().getAuthentication()
            .getName();

但我想将其他用户详细信息存储在 cookie 中,例如 userId。我该怎么做?是否需要通过 userDao(name) 获取用户信息,然后手动将用户信息存储在 cookie 中?

最佳答案

在这种情况下,您不需要对 cookie 做任何事情。

只要用户已登录(无论他如何登录 - 使用登录表单或“记住我”),您都可以从 SecurityContext 访问该用户的 UserDetails >,Spring Security 会处理它。

因此,您只需将所需信息放入 UserDetailsS​​ervice.loadUserByUsername() 中的 UserDetails(使用您自己的 UserDetails 子类) ,如有必要),并通过 SecurityContext 访问它:

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null) {
    Object principal = auth.getPrincipal();  
    if (principal instanceof UserDetails) {
        UserDetails user = (UserDetails) principal;
        ... // User is logged in, now you can access its details
    }
}

换句话说,当 Spring Security 收到没有 Activity session 但带有记住我 cookie 的请求时,它使用来自 cookie 的用户身份来加载 UserDetails 并将它们放入 SecurityContext(并进入新创建的 session session )。稍后您可以从 SecurityContext 访问这些详细信息。

关于java - 我可以使用 spring security 将完整的用户信息存储在 cookie 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12278009/

相关文章:

javascript - 如何跨设备扩展 localStorage(无 DB)

Spring Boot Webflux - 安全 CORS 不起作用

java - 使用 cas 和 spring security 执行 protected url

java - 检查一个 XML 文件是否是第二个 XML 文件的 "subset"的最简单方法是什么?

Java 将字符串解析为日期返回不正确的日期

asp.net 是否可以从静态类读取 cookie 的值?

java - JSP中登录页面 session 超时问题

java.lang.AssertionError : Content type not set - Spring Controller Junit Tests 错误

java - 强制 SimpleDateFormat 解析整个字符串

testing - 将 Cookie 作为请求 header 传递 - SSO JMeter