如何使用spring创建自定义用户会话对象

时间:2013-02-11 10:28:48

标签: spring session

我构建了一个简单的webapp,这是我学习弹簧技术的一部分。

我正在考虑创建某种自定义用户会话,该会话保存对许多用户属性的引用,例如语言,货币可能是。

我怎样才能在春天实现这一目标?或者已经存在某些东西?

1 个答案:

答案 0 :(得分:0)

您可以撰写自定义authentication provider并传递自定义UserDetailsService

<authentication-manager>
    <authentication-provider user-service-ref="customUserService" />
</authentication-manager>

<beans:bean id="customUserService" class="custom.UserServiceImpl">
</beans:bean>

public class UserServiceImpl implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
        //Your logic
        return new AuthenticatedUser("", "", true, true, true, new HashSet<GrantedAuthority>());
    }
}

public class AuthenticatedUser extends
        org.springframework.security.core.userdetails.User {
    private static final long serialVersionUID = 1L;

    // Your additional properties

    public AuthenticatedUser(String username, String password,
            boolean enabled, boolean accountNonExpired,
            boolean credentialsNonExpired,
            Collection<? extends GrantedAuthority> authorities)
            throws IllegalArgumentException {
        super(username, password, enabled, accountNonExpired,
                credentialsNonExpired, true, authorities);
    }
}