2014-12-24 15 views
7

Próbuję zrobić przykład używając wiosennego buta ze sprężynowym zabezpieczeniem. Moim pomysłem jest stworzenie aplikacji internetowej, a także zapewnienie API, chciałbym mieć oba zabezpieczenia; więc muszę utworzyć konfigurację zabezpieczeń sieci web http, ale to nie działa.Spring Boot + Security + Multi HTTP Konfiguracja sieci

Podążyłem za tym linkiem http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#multiple-httpsecurity, ale bez powodzenia. I otrzymuję ten błąd:

Błąd podczas tworzenia komponentu bean o nazwie 'webSecurityConfiguration': Injection of autnostified dependencies failed; wyjątek zagnieżdżonych jest java.lang.IllegalStateException: Nie można stosować do org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer już wybudowany obiekt o

konfiguracji, które używam to:

@Configuration 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
@EnableGlobalAuthentication 
@EnableGlobalMethodSecurity(securedEnabled = true) 
public class WebSecurityConfiguration { 

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

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

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .antMatcher("/api/**") 
      .authorizeRequests() 
       .anyRequest().hasRole("ADMIN") 
       .and() 
      .httpBasic(); 
    } 
} 

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

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web 
      .ignoring() 
       .antMatchers("/resources/**"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests()      
       .antMatchers("/", "/home").permitAll() 
      .anyRequest() 
       .authenticated() 
      .and() 
       .formLogin() 
        .loginPage("/login").permitAll() 
      .and() 
       .logout().permitAll(); 
    } 
    } 
} 

góry dzięki

Odpowiedz

5

po dużo czytania Znalazłem coś, co działa dla mnie:

@Configuration 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
@EnableGlobalMethodSecurity(securedEnabled = true) 
public class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter { 

    @Resource(name = "customUserDetailsService") 
    protected CustomUserDetailsService customUserDetailsService; 

    @Resource 
    private DataSource dataSource; 

    @Autowired 
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(customUserDetailsService); 
    } 

    @Configuration 
    @Order(1) 
    public static class ApiConfigurationAdapter extends WebSecurityConfigurerAdapter { 
     @Resource(name = "restUnauthorizedEntryPoint") 
     private RestUnauthorizedEntryPoint restUnauthorizedEntryPoint; 
     @Resource(name = "restAccessDeniedHandler") 
     private RestAccessDeniedHandler restAccessDeniedHandler; 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      SecurityConfigurer<DefaultSecurityFilterChain, HttpSecurity> securityXAuthConfigurerAdapter = new XAuthTokenConfigurer(
        userDetailsServiceBean()); 

      // @formatter:off 
      http 
       .antMatcher("/api/**").csrf().disable() 
       .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
       .and() 
       .exceptionHandling() 
        .authenticationEntryPoint(restUnauthorizedEntryPoint) 
        .accessDeniedHandler(restAccessDeniedHandler) 
       .and() 
        .authorizeRequests() 
         .antMatchers(HttpMethod.POST, "/api/authenticate").permitAll() 
         .anyRequest().hasRole("ADMIN") 
         .and() 
         .apply(securityXAuthConfigurerAdapter); 
      // @formatter:on 
     } 
    } 

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

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      // @formatter:off 
      http 
       .authorizeRequests() 
        .antMatchers("/", "/home").permitAll() 
        .anyRequest().authenticated() 
        .and() 
        .formLogin() 
         .loginPage("/login").permitAll() 
        .and() 
        .logout().permitAll() 
      ; 
      // @formatter:on 
     } 
    } 
} 
Powiązane problemy