2013-09-10 14 views
7

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

मैं जो समाधान ढूंढ रहा हूं वह http://meera-subbarao.blogspot.co.uk/2010/11/spring-security-combining-basic-and.html समझाया गया है, लेकिन मैं इसे जावा कॉन्फ़िगरेशन का उपयोग करके करना चाहता हूं।

अग्रिम धन्यवाद।

@Configuration 
@EnableWebSecurity 
public class AppSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private UserService userService; 

    @Override 
    protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userService); 
    } 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     // Ignore any request that starts with "/resources/". 
     web.ignoring().antMatchers("/resources/**"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeUrls().antMatchers("/", "/index", "/user/**", "/about").permitAll() 
     .antMatchers("/admin/**").hasRole("ADMIN") 
     .anyRequest().authenticated() 
     .and().formLogin() 
     .loginUrl("/login") 
     .failureUrl("/login-error") 
     .loginProcessingUrl("/security_check") 
     .usernameParameter("j_username").passwordParameter("j_password") 
     .permitAll(); 

     http.logout().logoutUrl("/logout"); 
     http.rememberMe().rememberMeServices(rememberMeServices()).key("password"); 
    } 

    @Bean 
    public RememberMeServices rememberMeServices() { 
     TokenBasedRememberMeServices rememberMeServices = new TokenBasedRememberMeServices("password", userService); 
     rememberMeServices.setCookieName("cookieName"); 
     rememberMeServices.setParameter("rememberMe"); 
     return rememberMeServices; 
    } 
} 
+0

आप क्या परिणाम देख रहे हैं? – Taylor

+0

वर्तमान में मेरे पास इसके साथ फॉर्म प्रमाणीकरण है और मुझे यह नहीं पता कि यूआरएल पैटर्न में मूल एथ जोड़ने के लिए/api/* –

उत्तर

9

समाधान मैंने पाया की तरह https://github.com/spring-projects/spring-security-javaconfig/blob/master/samples-web.md#sample-multi-http-web-configuration वर्णन किया गया है, पहले एक के अंदर एक और वर्ग का विस्तार WebSecurityConfigurerAdapter का निर्माण करना था

मेरे समाधान इस प्रकार है:

@Configuration 
@EnableWebSecurity 
public class AppSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private UserService userService; 

    @Override 
    protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userService); 
    } 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     // Ignore any request that starts with "/resources/". 
     web.ignoring().antMatchers("/resources/**"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeUrls().antMatchers("/", "/index", "/user/**", "/about").permitAll() 
      .antMatchers("/admin/**").hasRole("ADMIN") 
      .anyRequest().authenticated() 
      .and().formLogin() 
      .loginUrl("/login") 
      .failureUrl("/login-error") 
      .loginProcessingUrl("/security_check") 
      .usernameParameter("j_username").passwordParameter("j_password") 
      .permitAll(); 

     http.logout().logoutUrl("/logout"); 
     http.rememberMe().rememberMeServices(rememberMeServices()).key("password"); 
    } 

    @Bean 
    public RememberMeServices rememberMeServices() { 
     TokenBasedRememberMeServices rememberMeServices = new TokenBasedRememberMeServices("password", userService); 
     rememberMeServices.setCookieName("cookieName"); 
     rememberMeServices.setParameter("rememberMe"); 
     return rememberMeServices; 
    } 

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

     @Override 
     protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
      auth.inMemoryAuthentication().withUser("api").password("pass").roles("API"); 
     } 

     protected void configure(HttpSecurity http) throws Exception { 
      http.authorizeUrls() 
       .antMatchers("/api/**").hasRole("API") 
       .and() 
       .httpBasic(); 
     } 
    } 
} 
2

मैं बस यह करने से कहेंगे:

यह विन्यास मैं वर्तमान में है। AuthorizeUrls() के साथ एक दूसरी पंक्ति निर्दिष्ट करें, लेकिन आपके प्रमाणीकरण के साथ आवश्यक यूआरएल के लिए। formLogin() के बजाय का उपयोग httpBasic()

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeUrls().antMatchers("/", "/index", "/user/**", "/about").permitAll() 
    .antMatchers("/admin/**").hasRole("ADMIN") 
    .anyRequest().authenticated() 
    .and().formLogin() 
    .loginUrl("/login") 
    .failureUrl("/login-error") 
    .loginProcessingUrl("/security_check") 
    .usernameParameter("j_username").passwordParameter("j_password") 
    .permitAll(); 

    http.authorizeUrls().antMatchers("/api/*").hasRole("YOUR_ROLE_HERE").and().httpBasic(); 

    http.logout().logoutUrl("/logout"); 
    http.rememberMe().rememberMeServices(rememberMeServices()).key("password"); 
} 

कुछ इस तरह काम करना चाहिए।

लिंक: HttpSecurity, HttpBasicConfgurer

+0

यह काम नहीं करता है, एपीआई का दावा है कि प्राधिकृत करने वाले यूआरएल() दो बार पिछले आमंत्रणों को ओवरराइड करता है संभवतः ऐसा करना संभव नहीं है –

0

आप यह सिर्फ के बाद .antMatcher("/api/**") जोड़कर हल कर सकते हैं केवल /api यूआरएल प्रबंधित करने के लिए आपकी पहली कॉन्फ़िगरेशन में http। आप इसे पहले एडाप्टर पर होना चाहिए:

http 
    .antMatcher("/api/*") 
    .authorizeRequests() 
     .antMatchers("^/api/.+$").hasRole("ADMIN") 
    .... 
-1

यहाँ में अन्य उत्तर अपेक्षाकृत पुरानी है और उदाहरण के लिए, मैं authorizeUrls वसंत में नहीं मिल सकता है कर रहे हैं 4.

SpringBoot 1.4.0/स्प्रिंग 4 में , मैं इस तरह बुनियादी/प्रपत्र लॉगिन क्रियान्वित किया है:

@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter 
{ 
    protected void configure (HttpSecurity aHttp) throws Exception 
    {  
     aHttp.authorizeRequests().antMatchers (("/api/**")).fullyAuthenticated().and().httpBasic(); 

     aHttp.formLogin() 
     .loginPage ("/login").permitAll() 
     .and().logout().permitAll(); 
    } 
} 

इस लेखन के और अधिक ellegant तरीके हो सकता है - मैं अभी भी समझ कैसे इस बिल्डर अनुक्रम के मामले में काम करता है और बहुत आगे है पर काम कर रहा हूँ। लेकिन यह काम किया।

+0

यह मेरे लिए काम नहीं करता है - मूल प्रमाणीकरण के बजाय लॉगिन फॉर्म में/api/को रीडायरेक्ट करने के प्रयासों को रीडायरेक्ट किया जाता है। –

+0

@ जेम्सस्कर - ओपी ने कभी भी उल्लेख नहीं किया कि उसे मूल लेख के लिए प्रचारित करने की आवश्यकता है ... लेकिन ऊपर के साथ मूल लेख, काम करता है। यदि आप सॉफ़्टवेयर के HTTP क्लाइंट का उपयोग करते हैं और अपने अनुरोध में बेसिक ऑथ क्रेडिट भेजते हैं, तो यह काम करता है। – ETL

0

स्पष्ट रूप से वसंत अपडेट के रूप में ये उनकी प्रयोज्यता में फीका होता है। वसंत क्लाउड स्टार्टर सुरक्षा 1.4.0 चल रहा है। यह मेरा समाधान है। मेरा उपयोग मामला थोड़ा अलग था क्योंकि मैं क्लाउड कॉन्फ़िगरेशन के लिए मूल ऑथ का उपयोग करके रीफ्रेश एंडपॉइंट सुरक्षित करने और अन्य सभी मामलों में प्रमाणीकरण पास करने के लिए वसंत सत्र के साथ गेटवे का उपयोग करने का प्रयास कर रहा हूं।

@EnableWebSecurity 
@Configuration 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    public void configureGlobal1(AuthenticationManagerBuilder auth) throws Exception { 
     //try in memory auth with no users to support the case that this will allow for users that are logged in to go anywhere 
     auth.inMemoryAuthentication(); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .httpBasic() 
       .disable() 
      .authorizeRequests() 
       .antMatchers(HttpMethod.POST, "/user").permitAll() 
       .anyRequest().authenticated() 
       .and() 
      .csrf().disable(); 
    } 

    @Bean 
    public BCryptPasswordEncoder encoder() { 
     return new BCryptPasswordEncoder(11); 
    } 

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

     @Autowired 
     private AuthenticationProvider customAuthenticationProvider; 

     @Autowired 
     protected void configureGlobal2(AuthenticationManagerBuilder auth) throws Exception { 
      auth 
        .authenticationProvider(customAuthenticationProvider); 
     } 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
       .httpBasic() 
        .and() 
       .authorizeRequests() 
        .antMatchers(HttpMethod.POST, "/refresh").hasRole("SUPER") 
        .and() 
       .csrf().disable(); 
     } 
    } 
} 

आगे स्पष्टीकरण के लिए वसंत सुरक्षा दस्तावेज़ देख सकेंगे: Spring Security

मूल विचार यह है कि @Order एनोटेशन क्या प्रमाणन योजनाओं चलाए जा रहे हैं आदेश हुक्म होगा। नहीं @ ऑर्डर का मतलब है कि यह आखिरी है। अगर AuthorizeRequests() सेक्शन आने वाले यूआरएल पर मेल नहीं खा सकता है तो कॉन्फ़िगरेशन पास हो जाएगा और अगला प्रमाणीकरण का प्रयास करेगा। यह तब तक जारी रहेगा जब तक प्रमाणीकरण सफल न हो या विफल हो जाए।

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