복붙노트

[SPRING] 다른 인증 프로 바이더 (Web App 용 API 및 LDAP의 기본 인증)로 여러 WebSecurityConfigurerAdapter 사용

SPRING

다른 인증 프로 바이더 (Web App 용 API 및 LDAP의 기본 인증)로 여러 WebSecurityConfigurerAdapter 사용

Spring Security Reference 섹션 5.7에 따라 하나 이상의 보안 어댑터를 정의 할 수 있어야합니다.

나는 성공을 거듭하면서도 똑같이하려고 노력한다. 서버를 재부팅 한 후 처음 x 번은 기본 인증과 함께 잘 작동하지만 몇 번 로그인 (양식) 페이지로 리디렉션되면 API 호출이 아닌 Google 웹 앱에서만 발생합니다.

내 코드 :

@EnableWebSecurity
public class MultiHttpSecurityConfig  {

    @Configuration
    @Order(1)
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {

        @Autowired
        private Environment env;

        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().
                withUser("admin").password("pw_test").roles(API_ROLE);
        }

        protected void configure(HttpSecurity http) throws Exception {
            http
              .antMatcher("/services/**")
              .authorizeRequests()
              .anyRequest().hasRole(API_ROLE)
              .and()
              .httpBasic()
              .and()
              .csrf()
              .disable();
        }
    }

    @Configuration
    @Order(2)
    public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

        @Autowired
        private Environment env;

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

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // LDAP FORM AUTHENTICATION
            http.authorizeRequests()
                .antMatchers("/login.html").permitAll()
                .antMatchers("/css/**").permitAll() 
                .antMatchers("/js/**").permitAll() 
                .antMatchers("/images/**").permitAll() 
                .anyRequest().authenticated()
            .and().formLogin()
                .failureUrl("/login.html?error=1")
                .loginPage("/login.html")
                .loginProcessingUrl("/j_spring_security_check")
                .defaultSuccessUrl("/success.html")
                .usernameParameter("j_username")
                .passwordParameter("j_password")
                .permitAll();

            http.csrf().disable();

            // iFRAMES SETTINGS
            http
                .headers()
                .frameOptions().sameOrigin()
                .httpStrictTransportSecurity().disable();

            // HTTPS
            http
                .requiresChannel()
                .anyRequest()
                .requiresSecure();

            //MAP 8080 to HTTPS PORT
            http.portMapper().http(8080).mapsTo(443);
        }

        @Bean
        public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
            CustomLdapAuthenticationProvider provider = new CustomLdapAuthenticationProvider(env.getProperty("ldap.domain"), env.getProperty("ldap.url"), env.getProperty("ldap.base"));
            provider.setConvertSubErrorCodesToExceptions(true);
            provider.setUseAuthenticationRequestCredentials(true);
            return provider;
        }
    }
}

어떤 생각?

스프링 부트 버전 1.4.1-RELEASE와 스프링 보안 버전 4.1.3-RELEASE를 사용하고 있습니다.

해결법

  1. ==============================

    1.동일한 AuthenticationManagerBuilder를 autowire 때문에 두 구성에 동일한 AuthenticationManager를 사용합니다.

    동일한 AuthenticationManagerBuilder를 autowire 때문에 두 구성에 동일한 AuthenticationManager를 사용합니다.

    스프링 보안 아키텍처 :

  2. from https://stackoverflow.com/questions/40258583/using-multiple-websecurityconfigureradapter-with-different-authenticationprovide by cc-by-sa and MIT license