【问题标题】:How to create a custom User session object using spring如何使用弹簧创建自定义用户会话对象
【发布时间】:2013-02-11 19:51:22
【问题描述】:

我构建了一个简单的 web 应用程序,这是我学习 Spring 技术的一部分。

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

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

【问题讨论】:

    标签: spring session


    【解决方案1】:

    您可以编写自定义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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 2014-12-13
      相关资源
      最近更新 更多