2014-12-24 13 views
7

मैं बसंत सुरक्षा के साथ वसंत-बूट का उपयोग करके एक उदाहरण करने की कोशिश कर रहा हूं। मेरा विचार है कि एक वेब ऐप बनाना और एक एपीआई भी प्रदान करना है, मैं दोनों को सुरक्षा चाहिए; इसलिए मुझे एक बहु http वेब सुरक्षा कॉन्फ़िगरेशन बनाने की आवश्यकता है हालांकि यह काम नहीं कर रहा है।स्प्रिंग बूट + सुरक्षा + मल्टी HTTP वेब कॉन्फ़िगरेशन

मैंने इस लिंक का पालन किया http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#multiple-httpsecurity लेकिन कोई सफलता नहीं। और, मुझे यह त्रुटि मिल रही है

नाम 'वेब सुरक्षा सुरक्षा कॉन्फ़िगरेशन' नाम से बीन बनाने में त्रुटि: स्वत: निर्भरता की इंजेक्शन विफल रही; नेस्टेड अपवाद java.lang.IllegalStateException है:

: पहले से ही वस्तु का निर्माण करने के लिए

विन्यास है कि मैं उपयोग कर रहा हूँ org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer लागू नहीं कर सकता पीछा कर रहा है

@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(); 
    } 
    } 
} 

अग्रिम

उत्तर

5

के बाद पढ़ने का एक बहुत मैं कुछ है कि मेरे लिए काम करता पाया:

@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 
     } 
    } 
} 
0

धन्यवाद मैं भी यही मुद्दा का सामना करना पड़ा रहा है। लेकिन जब मैंने WebSecurityConfigurerAdapter से WebSecurity कॉन्फ़िगरेशन मास्टर क्लास का विस्तार किया, तो मुझे हल हो गया।

कृपया निम्नलिखित स्टैक ओवरफ्लो पोस्ट देखें, जिसमें आप पूर्ण कॉन्फ़िगरेशन पा सकते हैं।

Spring Security HTTP Basic for RESTFul and FormLogin for web - Annotations

संबंधित मुद्दे