Java Spring Security config - multiple authentication providers

前端 未结 3 1827
没有蜡笔的小新
没有蜡笔的小新 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:53

    May be this will help you :-

    @Configuration
    @EnableWebSecurity
    @Profile("container")
    public class XSecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Autowired
    private AuthenticationProvider authenticationProvider;
    
    @Autowired
    private AuthenticationProvider authenticationProviderDB;
    
    @Override
    @Order(1)
    
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider);
    }
    
    @Order(2)
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProviderDB);
    }
    
    @Override
      public void configure(WebSecurity web) throws Exception {
        web
          .ignoring()
             .antMatchers("/scripts/**","/styles/**","/images/**","/error/**");
      }
    
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/rest/**").authenticated()
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .successHandler(new AuthenticationSuccessHandler() {
                    @Override
                    public void onAuthenticationSuccess(
                            HttpServletRequest request,
                            HttpServletResponse response,
                            Authentication a) throws IOException, ServletException {
                                //To change body of generated methods,
                                response.setStatus(HttpServletResponse.SC_OK);
                            }
                })
                .failureHandler(new AuthenticationFailureHandler() {
    
                    @Override
                    public void onAuthenticationFailure(
                            HttpServletRequest request,
                            HttpServletResponse response,
                            AuthenticationException ae) throws IOException, ServletException {
                                response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                            }
                })
                .loginProcessingUrl("/access/login")
                .and()
                .logout()
                .logoutUrl("/access/logout")                
                .logoutSuccessHandler(new LogoutSuccessHandler() {
                    @Override
                    public void onLogoutSuccess(
                            HttpServletRequest request, 
                            HttpServletResponse response, 
                            Authentication a) throws IOException, ServletException {
                        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                    }
                })
                .invalidateHttpSession(true)
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(new Http403ForbiddenEntryPoint())
                .and()
                .csrf()//Disabled CSRF protection
                .disable();
        }
    } 
    

提交回复
热议问题