2016-05-13 43 views
0

我在Spring Security中使用Spring引导1.3.2。 我具有以下配置(HttpSecurity HTTP)方法来INFORCE认证Spring Boot安全性不会通过WebSecurity忽略certian url

protected void configure(HttpSecurity http) throws Exception { 

    RequestMatcher csrfRequestMatcher = new RequestMatcher() { 
      private AntPathRequestMatcher[] requestMatchers = { 
       new AntPathRequestMatcher("/iams/w/*") 
      }; 
      @Override 
      public boolean matches(HttpServletRequest request) { 
      for (AntPathRequestMatcher rm : requestMatchers) { 
       if (rm.matches(request)) { return true; } 
      } 
      return false; 
      } // method matches 

     };  


    http 
     .csrf() 
     .requireCsrfProtectionMatcher(csrfRequestMatcher) 
     .and() 
     .authorizeRequests() 
      .anyRequest().authenticated() 
      .and() 
     .requestCache() 
      .requestCache(new NullRequestCache()) 
      .and() 
     .httpBasic(); 
} 

和我有下列configure(WebSecurity网)方法忽略一些如下面的网址;

public void configure(WebSecurity web) throws Exception { 

    web.ignoring().antMatchers(
      "/myapp/docs/**", 
      "/myapp/docs/*", 
      "/myapp/docs/index.html", 
      "/resources/**", 
      "/static/**"); 

} 

但HTTP请求http://127.0.0.1:9000/myapp/docs/index.html仍然reuires用户名/密码(验证),并返回“状态”:401,“错误”:“未授权” ......其实没有上WebSecurity忽略网址的 是因为工作它也需要认证。如果我提供了auth,那么它就可以工作。我怎么能在这里简单地忽略一些网址(比如“/ myapp/docs/**”)。我已经

@EnableWebSecurity 

@EnableGlobalMethodSecurity(prePostEnabled =真) 公共类SecurityConfig扩展WebSecurityConfigurerAdapter以下的SecurityConfig类定义{

我缺少什么?请指教。

回答

0

代码中有错误代码。

http 
    .csrf() 
    .requireCsrfProtectionMatcher(csrfRequestMatcher) 
    .and() 
    .authorizeRequests() 
     .anyRequest().authenticated() 
     .and() 
    .requestCache() 
     .requestCache(new NullRequestCache()) 
     .and() 
    .httpBasic(); 

因此,任何请求都需要进行身份验证。您可以直接使用antMatchers

http 
     .authorizeRequests() 
     .antMatchers("/iams/w/*") 
     .authenticated() 
     .and() 
     .httpBasic() 
     .and() 
     .requestCache() 
     .requestCache(new NullRequestCache()) 
     .csrf().disable() 

我希望它对你有帮助。

+0

谢谢您的答复,但你的建议,我的“/艾姆斯/ W/*”不受保护的。我可以找到所有这些网址; “/ iams/docs/**”,“/ iams/w/*”和“/ iams/api/*”没有基本身份验证。以下是根据您的建议设置的。在这里我想用用户名/密码保护“/ iams/w”和“/ iams/api /”,但是让所有人都可以在没有用户名/密码的情况下进入“/ iams/docs/*”。这是基于spring引导的restful实现,但想要公开一些像docs这样的url,以便它可以被所有人访问而不是api调用。 – lotse

0

感谢您的回复,但根据您的建议,我的“/ iams/w/*”根本不受保护。我可以找到所有这些网址; “/ iams/docs/**”,“/ iams/w/”和“/ iams/api/”,但没有基本身份验证。以下是根据您的建议设置的。在这里我想用用户名/密码保护“/ iams/w”和“/ iams/api /”,但是让所有人都可以在没有用户名/密码的情况下进入“/ iams/docs/*”。这是基于spring引导的restful实现,但想要公开一些像docs这样的url,以便它可以被所有人访问而不是api调用。

public void configure(WebSecurity web) throws Exception { 

    web.ignoring().antMatchers(
      "/iams/docs/**", 
      "/iams/docs/*", 
      "/iams/docs/index.html", 
      "/static/**"); 

} 


@Override 
protected void configure(HttpSecurity http) throws Exception { 

    http 
    .authorizeRequests() 
    .antMatchers("/iams/api/**","/iams/api/v1/*") 
    .authenticated() 
    .and() 
    .httpBasic() 
    .and() 
    .requestCache() 
    .requestCache(new NullRequestCache()) 
    .and() 
    .csrf().disable(); 
} 
0

它可能会更容易使用简单的一组模式来留下不安全的,然后简单地说,其他的东西都是安全的。

这可能是更接近你想要什么:

public static final String[] NOT_SECURED = {"/iams/docs/**","/static/**"}; 

@Override 
public void configure(WebSecurity web) throws Exception { 
    web.ignoring().antMatchers(NOT_SECURED); 
} 


@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
     .authorizeRequests() 
     .antMatchers(NOT_SECURED).permitAll() 
     .anyRequest().authenticated() 
     .and() 
     .httpBasic() 
     .and() 
     .requestCache() 
     .requestCache(new NullRequestCache()) 
     .and() 
     .csrf().disable(); 
}