2015-11-05 100 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