【问题标题】:spring security java config - multiple authentication managerspring security java config - 多重身份验证管理器
【发布时间】:2016-04-18 16:03:04
【问题描述】:

我正在开发一个 java spring mvc 应用程序。这是我的SecurityConfig 类的一部分,用于设置AuthenticationManager

...
@Autowired
private SecurityDAO securityDAO;
...
@Override
protected void configure(AuthenticationManagerBuilder registry) throws Exception {
    registry.userDetailsService(securityDAO).passwordEncoder(new BCryptPasswordEncoder());
}

SecurityDAO 是一个实现UserDetailsService 接口的类。

现在,我需要两种不同的UserDetailsService 接口实现。 一种用于admin 用户,其网址为/admin/login,另一种用于customer,网址为/customer/login

我找到了一些使用 spring 实现多个身份验证管理器的示例,但它们都使用 XML config,我找不到使用 java config 的示例。 p>

Here is a sample of xml config.其实是想把这个config转成java config。

【问题讨论】:

    标签: java spring-mvc spring-security


    【解决方案1】:

    扩展 AbstractAutowiringFactoryBean 并在该类中添加您的实现。

    它有两个方法,doCreateInstance()知道需要实例化谁的对象,getObjectType()知道这是哪个接口实现。

    例如

    @Configuration
    public class CLass extends AbstractAutowiringFactoryBean<Object> {
    
        public enum Env {
            devloper, production
        }
    
        Env envType = Env.devloper;
    
        @Override
        protected Object doCreateInstance() {
            switch (envType) {
                case devloper:
    
                    return new ClassImpl1();
                case production:
                    rreturn new ClassImpl2();
    
            }
            throw new RuntimeException("Unsupported implementation type");
        }
    
        @Override
        public Class<?> getObjectType() {
            return SecurityDAO.class;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      相关资源
      最近更新 更多