2017-03-17 36 views
0

我在我的spring啓動項目中使用basicAuthSpringBoot basic auth忽略以.wsdl結尾的URL

需要驗證服務URL,而在WSDL上應該沒有驗證。

我想保留application.yml文件中被忽略的所有URL &。

喜歡的東西:

auth.authenticated: /onlineshop/v1/ecart,/onlineshop/v1/wishlist 
auth.ignored: /onlineshop/v1/ecart.wsdl,/onlineshop/v1/wishlist.wsdl 


@EnableWebSecurity 
@Configuration 
class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Value("${auth.authenticated}") 
    String[] allAuthenticated; 

    @Value("${auth.ignored}") 
    String[] allIgnored; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     // Something like 
     for (String ignored: allIgnored) { 
      http.authorizeRequests().antMatchers(ignored).permitAll(); 
     } 

     // Something like 
     for (String authenticated: allAuthenticated) { 
      http.authorizeRequests().antMatchers(authenticated).authenticated(); 
     } 
     .... 
    } 

} 

上面的代碼是一個粗略的草稿(對不起那個),但我已經試過沿着這些線路編碼,但它無法正常工作。

它沒有應用任何形式的驗證。

請建議我該如何完成這項工作。

此外,而不是忽略結束的.wsdl選擇的網址,我怎麼能忽略與的.wsdl

結尾的所有網址,謝謝您

回答

2

首先,我相信你應該允許未經認證做一個白名單方法訪問。因此,我刪除了allAuthenticated參數,並且對於不在allIgnored參數中的每個url都要求進行身份驗證,這在設計上更安全。

以下配置對於您所需的功能已足夠。

@EnableWebSecurity 
@Configuration 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Value("${auth.ignored}") 
    private String[] allIgnored; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests() 
      .antMatchers(allIgnored).permitAll() 
      .anyRequest().authenticated() 
      .and() 
      .httpBasic(); 
    } 

} 

注意,因爲antMatchers()需要String[],你並不需要自己迭代循環。

如果您仍然想要使用allAuthenticated進行配置,則只需將.antMatchers(allAuthenticated).authenticated()添加到配置。

+0

剛纔看到你給出了同樣的評論,我把它放入一個答案。讓我們補償;-) – GhostCat