Java Spring Security config - multiple authentication providers

前端 未结 3 1829
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 15:46

There are several references of multiple authentication providers in spring security, but no example in Java config could be located.

The following link gives the XM

3条回答
  •  爱一瞬间的悲伤
    2020-11-27 15:59

    This is a successful configuration which helps configure multiple authentication providers in java config. Thanks a lot ojus for your inputs. It did help in nailing down the issue. The key is to have

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider);
        auth.authenticationProvider(authenticationProviderDB);
    
    }
    

    Full code below

    @Configuration
    @EnableWebSecurity
    public class XSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Autowired
        private LDAPAuthenticationProvider authenticationProvider;
    
        @Autowired
        private DBAuthenticationProvider authenticationProviderDB;
    
        @Override
          public void configure(WebSecurity web) throws Exception {
            web
              .ignoring()
                 .antMatchers("/scripts/**","/styles/**","/images/**","/error/**");
          }
    
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.authenticationProvider(authenticationProvider);
            auth.authenticationProvider(authenticationProviderDB);
    
        }
    
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()
            .authorizeRequests()
                .antMatchers("/","/logout").permitAll()
                .antMatchers("/admin").hasRole("ADMIN")         
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/index")
                .loginProcessingUrl("/perform_login")
                .usernameParameter("user")
                .passwordParameter("password")
                .failureUrl("/index?failed=true")
                .defaultSuccessUrl("/test",true)
                .permitAll()
                .and()
             .logout().logoutUrl("/logout")
                      .logoutSuccessUrl("/index?logout=true").permitAll()
                .and()
                .exceptionHandling().accessDeniedPage("/error");
        }
    
    
    }
    

提交回复
热议问题