Adnotacje metod zabezpieczeń z konfiguracją Java i Spring Security 3.2

Mam pewne problemy z ustawieniem aplikacji przy użyciu adnotacji poziomu metody kontrolowanej przez@EnableGlobalMethodSecurity Korzystam z inicjalizacji w stylu Servlet 3.0

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {

    public SecurityWebApplicationInitializer() {
        super(MultiSecurityConfig.class);
    }
}

Próbowałem 2 różne sposoby inicjalizacjiAuthenticationManager oba z własnymi problemami. Proszę to zanotowaćnie za pomocą@EnableGlobalMethodSecurity powoduje pomyślne uruchomienie serwera i wszystkie zabezpieczenia formularza są wykonywane zgodnie z oczekiwaniami. Moje problemy pojawiają się, gdy dodaję@EnableGlobalMethodSecurity i@PreAuthorize("hasRole('ROLE_USER')") adnotacje na moim kontrolerze.

Próbuję samodzielnie skonfigurować zabezpieczenia oparte na formularzach i api. Adnotacje oparte na metodzie muszą działać tylko dla bezpieczeństwa interfejsu API.

Jedna konfiguracja była następująca.

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class MultiSecurityConfig {

    @Configuration
    @Order(1)
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
        protected void configure(HttpSecurity http) throws Exception {
            http.antMatcher("/api/**").httpBasic();
        }

        protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
                .withUser("user").password("password").roles("USER").and()
                .withUser("admin").password("password").roles("USER", "ADMIN");
        }
    }

    @Configuration
    public static class FormWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("/static/**","/status");
        }

        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().anyRequest().hasRole("USER").and()
                .formLogin().loginPage("/login").permitAll();
        }

        protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
                .withUser("user").password("password").roles("USER").and()
                .withUser("admin").password("password").roles("USER", "ADMIN");
        }
    }

}

Nie jest to idealne rozwiązanie, ponieważ naprawdę chcę tylko jednej rejestracji mechanizmu uwierzytelniania, ale głównym problemem jest to, że powoduje następujący wyjątek:

java.lang.IllegalArgumentException: Expecting to only find a single bean for type interface org.springframework.security.authentication.AuthenticationManager, but found []

O ile mi wiadomo@EnableGlobalMethodSecurity tworzy własneAuthenticationManager więc nie jestem pewien, na czym polega problem.

Druga konfiguracja jest następująca.

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class MultiSecurityConfig {

    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
        return new AuthenticationManagerBuilder(ObjectPostProcessor.QUIESCENT_POSTPROCESSOR)
                .inMemoryAuthentication()
                    .withUser("user").password("password").roles("USER").and()
                    .withUser("admin").password("password").roles("USER", "ADMIN").and()
                    .and()
                .build();
    }

    @Configuration
    @Order(1)
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
        @Override protected void configure(HttpSecurity http) throws Exception {
            http.antMatcher("/api/**").httpBasic();
        }
    }

    @Configuration
    public static class FormWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("/static/**","/status");
        }

        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().anyRequest().hasRole("USER").and()
                .formLogin().loginPage("/login").permitAll();
        }
    }

}

Ta konfiguracja faktycznie zaczyna się pomyślnie, ale z wyjątkiem

java.lang.IllegalArgumentException: A parent AuthenticationManager or a list of AuthenticationProviders is required
at org.springframework.security.authentication.ProviderManager.checkState(ProviderManager.java:117)
at org.springframework.security.authentication.ProviderManager.<init>(ProviderManager.java:106)
at org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder.performBuild(AuthenticationManagerBuilder.java:221)

a kiedy testuję, odkryłem, że bezpieczeństwo nie działa.

Patrzę na to od kilku dni i nawet po zanurzeniu się w wiosennym kodzie implementacyjnym nie mogę znaleźć tego, co jest nie tak w mojej konfiguracji.

Używam spring-security-3.2.0.RC1 i spring-framework-3.2.3.RELEASE.

questionAnswers(1)

yourAnswerToTheQuestion