我构建了一个简单的 webapp,它是我学习 spring 技术的一部分。
我正在考虑创建某种自定义用户会话,其中包含对许多用户属性的引用,例如语言、货币可能是。
我怎样才能在春天做到这一点?还是已经存在一些东西?
您可以编写自定义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);
}
}