2017-06-19 1971 views
1

我想在我的rest API中整合Spring Security。我有問題,因爲我的移民阻礙了所有路線。Spring Security禁用默認登錄頁面

我的配置:

@Configuration 
@EnableResourceServer 
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter { 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable(); 
     http.authorizeRequests() 
       .antMatchers("/").permitAll() 
       .antMatchers(HttpMethod.POST,"/api/getNews").hasRole("ADMIN"); 
    } 
} 

當我想GET上​​春回登錄表單

<html> 
    <head> 
     <title>Login Page</title> 
    </head> 
    <body onload='document.f.username.focus();'> 
     <h3>Login with Username and Password</h3> 
     <form name='f' action='/login' method='POST'> 
      <table> 
       <tr><td>User:</td><td><input type='text' name='username' value=''></td></tr> 
       <tr><td>Password:</td><td><input type='password' name='password'/></td></tr> 
       <tr><td colspan='2'><input name="submit" type="submit" value="Login"/></td></tr> 
       <input name="_csrf" type="hidden" value="5453a3c5-2573-4861-b777-d008b04863c3" /> 
      </table> 
     </form> 
    </body> 
</html> 

但我有/允許的規則。爲什麼我無法訪問我的API?

+0

也許嘗試手動寫入獲取請求的權限'.antMatchers(HttpMethod.GET,「/」)。permitAll()' –

+0

不適用於此。 – lukassz

+0

而'.antMatchers(HttpMethod.GET,「/ *」)。permitAll()'? –

回答

1

發現旁邊:

當使用permitAll這意味着每一個身份驗證的用戶,但是你禁用匿名訪問,這樣就不會工作。

嘗試this

這對我的作品,添加例如:

http.csrf().disable() 
       .anonymous().authorities("ROLE_ANONYMOUS") 
       .and() 
       .authorizeRequests() 
       .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() 
       .antMatchers(HttpMethod.GET, appConfigHolder.getSecurity().getLoginUrl()).permitAll() 

編輯:

問題是Spring版本。隨着v1.4.3.REALESE沒有問題。

+0

仍然是同樣的問題 – lukassz

+0

'web.ignoring()。antMatchers(「/」)' –

+0

我不使用'WebSecurity'而是'HttpSecurity' – lukassz

相關問題