2016-12-01 34 views
0

我想保護我的應用程序,以便任何人都可以訪問某些URL,並且admin URL可供管理員用戶訪問,並且完全通過身份驗證。Spring Security fullyAuthenticationated()and hasRole(「ADMIN」)

但現在,我無法設法找到一種方式使用fullyAuthenticaded() AND hasrole()在一起。

@Override 
public void configure(HttpSecurity http) throws Exception { 
    http 
     .requestMatchers() 
      .antMatchers("/api/**", "/health") 
      .and() 
     .authorizeRequests() 
      .antMatchers("/health").permitAll() 
      .and() 
     .authorizeRequests() 
      .antMatchers("/api/get-data").fullyAuthenticated() 
      .and() 
     .authorizeRequests() 
      .anyRequest().hasRole("ADMIN"); 
} 

回答

1

你可以寫這樣的事情:

antMatchers( 「/ API /獲取數據」)訪問( 「isFullyAuthenticated()和hasRole( 'ROLE_BANNED')」)

這就像您在xml文件中定義時所寫的那樣。這裏同樣的事情。

現在,您可以爲一個antMatches定義一些網址,只有isFullyAuthenticated和一些具有管理員角色和isFullyAuthenticated的antMatches網址。

欲瞭解更多信息,請閱讀thisthis