2015-11-05 235 views
7

我在Repository(与@RepositoryRestResource注释)以下示例方法:@PreAuthorize(permitAll)仍需要验证

@Override 
@PreAuthorize("permitAll") 
@PostAuthorize("permitAll") 
public Iterable<User> findAll(); 

但我仍然得到401 Unauthorized,事件,当我添加这些permitAll注释全储存库界面。

我得到这个作为我WebSecurityConfigurerAdapter

@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
@Configuration 
class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(final HttpSecurity http) throws Exception { 
     http.authorizeRequests().anyRequest().fullyAuthenticated().and().httpBasic().and().csrf().disable(); 
    } 
} 

我想这将优先于这些方法的注释,BU我不知道如何解决这个问题。

+0

也许你遗漏了'permitAll'中的括号。尝试'permitAll()' –

+0

你真的需要这些注释吗?当你删除它们会发生什么? –

回答

4

方法安全性应用于Web安全筛选器之后。

由于您在您的配置中有anyRequest().fullyAuthenticated(),您的findAll方法将永远不会被击中。 anyRequest().fullyAuthenticated()表示所有尝试访问不具有完全用户身份验证的Web端点都将失败。

根据JavaDoc

指定URL是谁已经验证,是不是 “记住”用户允许的。

您需要在您的网络安全中添加其他路径,例如。

protected void configure(final HttpSecurity http) throws Exception { 
    http.authorizeRequests() 
      .anyRequest().fullyAuthenticated() 
      .antMatchers(HttpMethod.GET, '/somePath').permitAll() 
     .and() 
      .httpBasic() 
     .and() 
      .csrf().disable(); 
} 
+0

它真的是唯一的方法吗?我的意思是保持两个地方的路线很混乱。没有办法注释那些不安全的端点吗? – Cleankod

+1

问题是,它是两个完全独立的安全系统,运行在不同的时间。 – Leon