2015-03-06 11 views
15

मेरी प्रोजेक्ट में दो अलग-अलग हिस्सों, एक जेएसएफ व्यवस्थापक पैनल और एक RESTfull सेवा शामिल है। मैं यूआरएल उपयोगकर्ता नेविगेट करता है के आधार पर अलग प्रमाणीकरण तरीकों का उपयोग करने के लिए सेटअप वसंत सुरक्षा के लिए कोशिश कर रहा हूँ।स्प्रिंग सुरक्षा oauth2 और फॉर्म लॉगिन कॉन्फ़िगरेशन

आवश्यकताओं

  • JSF पृष्ठ पर नेविगेट करने वाले उपयोगकर्ता को प्रवेश स्क्रीन जहां वे प्रपत्र प्रमाणीकरण का उपयोग प्रमाणीकरण प्राप्त कर रहे हैं।
  • उपयोगकर्ता बाकी सेवा उपयोग करने के लिए नेविगेट टोकन देने के लिए बुनियादी प्रमाणीकरण के साथ अंतर्निहित प्रमाणीकरण OAuth2।

खुद से अलग विन्यास काम करते हैं, समस्या यह है कि जब मैं एक विन्यास में उन दोनों के गठबंधन करने के लिए प्रयास करते हैं, उस मामले में ऐसा लगता है बाकी प्रदाता की तरह रास्ते में हो जाता है और प्रत्येक अनुरोध भले ही अनुरोध जाना प्रमाणित करता है व्यवस्थापक यूआरएल के लिए (यह वसंत सुरक्षा आदेश से दस्तावेज है)।

मेरे नमूना विन्यास के रूप में दिखाया गया है:

  • प्रपत्र लॉगिन के लिए (JSF)

    @Override 
    @Order(1) 
    protected void configure(HttpSecurity http) throws Exception { 
    http 
         .csrf().disable() 
         .authorizeRequests() 
         .antMatchers("/resources/**").permitAll() 
         .antMatchers("/templates/**").permitAll() 
         .antMatchers("/401.html").permitAll() 
         .antMatchers("/404.html").permitAll() 
         .antMatchers("/500.html").permitAll() 
         .antMatchers("/api/**").permitAll() 
         .antMatchers("/ui/admin.xhtml").hasAnyAuthority("admin", "ADMIN") 
         .antMatchers("/thymeleaf").hasAnyAuthority("admin", "ADMIN") 
         //.anyRequest().authenticated() 
         .and() 
         .formLogin() 
         .loginPage("/login") 
         .defaultSuccessUrl("/ui/index.xhtml") 
         .failureUrl("/login?error=1") 
         .permitAll() 
         .and() 
         .logout() 
         .permitAll() 
         .and() 
         .rememberMe() 
         .and().exceptionHandling().accessDeniedPage("/error/403"); 
    
  • OAuth2 सुरक्षा config (REST) ​​

    @EnableResourceServer 
    @Order(2) 
    public class RestSecurityConfig extends WebSecurityConfigurerAdapter { 
    
        @Inject 
        private UserRepository userRepository; 
    
        @Inject 
        private PasswordEncoder passwordEncoder; 
    
        @Bean 
        ApplicationListener<AbstractAuthorizationEvent> loggerBean() { 
         return new AuthenticationLoggerListener(); 
        } 
    
        @Bean 
        AccessDeniedHandler accessDeniedHandler() { 
         return new AccessDeniedExceptionHandler(); 
        } 
    
        @Bean 
        AuthenticationEntryPoint entryPointBean() { 
         return new UnauthorizedEntryPoint(); 
        } 
    
        /*Override 
        public void configure(WebSecurity web) throws Exception { 
         web.ignoring() 
           .antMatchers(
             "/resources/**" 
             , "/templates/**" 
             , "/login" 
             , "/logout" 
             , "/ui/**" 
             , "/401.html" 
             , "/404.html" 
             , "/500.html" 
           ); 
        }*/ 
    
        @Override 
        protected void configure(HttpSecurity http) throws Exception { 
         ContentNegotiationStrategy contentNegotiationStrategy = http.getSharedObject(ContentNegotiationStrategy.class); 
         if (contentNegotiationStrategy == null) { 
          contentNegotiationStrategy = new HeaderContentNegotiationStrategy(); 
         } 
         MediaTypeRequestMatcher preferredMatcher = new MediaTypeRequestMatcher(contentNegotiationStrategy, 
           MediaType.APPLICATION_FORM_URLENCODED, 
           MediaType.APPLICATION_JSON, 
           MediaType.MULTIPART_FORM_DATA); 
    
         http.authorizeRequests() 
           .antMatchers("/ui/**").permitAll() 
           .and() 
           .anonymous().disable() 
           .sessionManagement() 
           .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
           .and().httpBasic() 
           .and() 
           .exceptionHandling() 
           .accessDeniedHandler(accessDeniedHandler()) // handle access denied in general (for example comming from @PreAuthorization 
           .authenticationEntryPoint(entryPointBean()) // handle authentication exceptions for unauthorized calls. 
           .defaultAuthenticationEntryPointFor(entryPointBean(), preferredMatcher) 
           .and() 
           .authorizeRequests() 
           .antMatchers("/api/**").fullyAuthenticated(); 
    
        } 
    
        @Override 
        @Bean 
        public AuthenticationManager authenticationManagerBean() throws Exception { 
         return super.authenticationManagerBean(); 
        } 
    
        @Override 
        protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
         auth.userDetailsService(new UserDetailsService() { 
          @Override 
          public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { 
           User user = userRepository.findOneByUsername(s); 
    
           if (null == user) { 
            // leave that to be handled by log listener 
            throw new UsernameNotFoundException("The user with email " + s + " was not found"); 
           } 
    
           return (UserDetails) user; 
          } 
         }).passwordEncoder(passwordEncoder); 
        } 
    
    
        @Configuration 
        @EnableAuthorizationServer 
        protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter { 
    
    
    
         @Autowired 
         private AuthenticationManager authenticationManager; 
    
    
         @Bean 
         public JwtAccessTokenConverter accessTokenConverter() { 
          return new JwtAccessTokenConverter(); 
         } 
    
         @Override 
         public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { 
          oauthServer.tokenKeyAccess("isAnonymous() || hasAuthority('ROLE_TRUSTED_CLIENT')").checkTokenAccess("hasAuthority('ROLE_TRUSTED_CLIENT')"); 
         } 
    
         @Override 
         public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
          endpoints.authenticationManager(authenticationManager).accessTokenConverter(accessTokenConverter()); 
         } 
    
    
         @Override 
         public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
          clients.inMemory() 
            .withClient("xxx") 
            .resourceIds(xxx) 
            .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit") 
            .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT") 
            .scopes("read", "write", "trust", "update") 
            .accessTokenValiditySeconds(xxx) 
            .refreshTokenValiditySeconds(xxx) 
            .secret("xxx") 
    
         } 
        } 
    } 
    

इन विन्यास अलग-अलग मौजूद हैं पंख वर्ग और ऑर्डरिंग मैन्युअल रूप से सेट है।

किसी को भी इस मुद्दे पर किसी भी समाधान है?

बेस्ट,

+0

आप इस समस्या को ठीक करने के लिए सक्षम थे? क्या आप कृपया एक उत्तर पोस्ट कर सकते हैं। –

+0

@Maleenc दुर्भाग्य से नहीं, मैं वास्तव में वसंत सुरक्षा लोगों से सराहना करता हूं और जवाब देता हूं। – maxsap

उत्तर

16

मैंने आपकी सुरक्षा कॉन्फ़िगरेशन को अनुकूलित करने का प्रयास किया। दुर्भाग्यवश, मैं अनुपलब्ध संदर्भ एप्लिकेशन के कारण इस कॉन्फ़िगरेशन को मान्य नहीं कर सकता।

शायद यह आपकी मदद कर सकते हैं:

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private UserRepository userRepository; 

    @Autowired 
    private PasswordEncoder passwordEncoder; 

    @Autowired 
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(new UserDetailsService() { 
      @Override 
      public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { 
       User user = userRepository.findOneByUsername(s); 

       if (null == user) { 
        throw new UsernameNotFoundException("The user with email " + s + " was not found"); 
       } 

       return (UserDetails) user; 
      } 
     }).passwordEncoder(passwordEncoder); 
    } 

    @Override 
    public void configure(WebSecurity webSecurity) throws Exception { 
     webSecurity 
       .ignoring() 
       .antMatchers("/resources/**" 
         , "/templates/**" 
         , "/login" 
         , "/logout" 
         , "/ui/**" 
         , "/401.html" 
         , "/404.html" 
         , "/500.html"); 
    } 

    @Configuration 
    @EnableAuthorizationServer 
    public static class OAuth2Configuration extends AuthorizationServerConfigurerAdapter { 

     @Autowired 
     private AuthenticationManager authenticationManager; 

     @Bean 
     public JwtAccessTokenConverter accessTokenConverter() { 
      return new JwtAccessTokenConverter(); 
     } 

     @Override 
     public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { 
      oauthServer.tokenKeyAccess("isAnonymous() || hasAuthority('ROLE_TRUSTED_CLIENT')").checkTokenAccess("hasAuthority('ROLE_TRUSTED_CLIENT')"); 
     } 

     @Override 
     public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
      endpoints.authenticationManager(authenticationManager).accessTokenConverter(accessTokenConverter()); 
     } 


     @Override 
     public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
      clients.inMemory() 
        .withClient("xxx") 
        .resourceIds("xxx") 
        .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit") 
        .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT") 
        .scopes("read", "write", "trust", "update") 
        .accessTokenValiditySeconds(xxx) 
        .refreshTokenValiditySeconds(xxx) 
        .secret("xxx"); 

     } 
    } 

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

     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      http 
        .csrf().disable() 
        .authorizeRequests() 
        .antMatchers("/ui/admin.xhtml").hasAnyAuthority("admin", "ADMIN") 
        .antMatchers("/thymeleaf").hasAnyAuthority("admin", "ADMIN") 
        .and() 
        .formLogin() 
        .loginPage("/login") 
        .defaultSuccessUrl("/ui/index.xhtml") 
        .failureUrl("/login?error=1") 
        .permitAll() 
        .and() 
        .logout() 
        .permitAll() 
        .and() 
        .rememberMe() 
        .and().exceptionHandling().accessDeniedPage("/error/403"); 
     } 
    } 

    @Order(2) 
    @Configuration 
    @EnableResourceServer 
    public static class CustomResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter { 

     @Bean 
     ApplicationListener<AbstractAuthorizationEvent> loggerBean() { 
      return new AuthenticationLoggerListener(); 
     } 

     @Bean 
     AccessDeniedHandler accessDeniedHandler() { 
      return new AccessDeniedExceptionHandler(); 
     } 

     @Bean 
     AuthenticationEntryPoint entryPointBean() { 
      return new UnauthorizedEntryPoint(); 
     } 

     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      ContentNegotiationStrategy contentNegotiationStrategy = http.getSharedObject(ContentNegotiationStrategy.class); 
      if (contentNegotiationStrategy == null) { 
       contentNegotiationStrategy = new HeaderContentNegotiationStrategy(); 
      } 
      MediaTypeRequestMatcher preferredMatcher = new MediaTypeRequestMatcher(contentNegotiationStrategy, 
        MediaType.APPLICATION_FORM_URLENCODED, 
        MediaType.APPLICATION_JSON, 
        MediaType.MULTIPART_FORM_DATA); 

      http.authorizeRequests() 
        .and() 
        .anonymous().disable() 
        .sessionManagement() 
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
        .and().httpBasic() 
        .and() 
        .exceptionHandling() 
        .accessDeniedHandler(accessDeniedHandler()) // handle access denied in general (for example comming from @PreAuthorization 
        .authenticationEntryPoint(entryPointBean()) // handle authentication exceptions for unauthorized calls. 
        .defaultAuthenticationEntryPointFor(entryPointBean(), preferredMatcher) 
        .and() 
        .authorizeRequests() 
        .antMatchers("/api/**").fullyAuthenticated(); 
     } 
    } 
} 
+0

क्या मैं फॉर्म लॉगिन का उपयोग/** के साथ कर सकता हूं और/api/** के साथ एक और आराम फॉर्म लॉग इन कर सकता हूं? अपने फॉर्म को अनुकूलित नहीं करना चाहते हैं जो आप ऊपर करते हैं। – WhiteWater

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