2017-03-17 75 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