2017-09-06 25 views
0

我有了這個安全配置:春季安全使用濾鏡permitAll不工作

@Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
      .addFilterBefore(
        new JwtLoginFilter("/login", authenticationManager()), 
        UsernamePasswordAuthenticationFilter.class) 
      .addFilterBefore(
        new JwtAuthenticationFilter(), 
        UsernamePasswordAuthenticationFilter.class); 
     http.csrf().disable() 
       .authorizeRequests().antMatchers("/", "/register").permitAll() 
       .and() 
       .authorizeRequests().anyRequest().authenticated(); 
    } 

兩個過濾器正在做認證工作。 authenticationFilter檢查auth cookie。

然而,permitAll不讓根路線和「/註冊」路線通行證(亦稱通過authenticationFilter,我認爲permitAll會讓這些路由信息通過過濾器仍然會)

有什麼不對?

+0

我知道我可以使用webSecurity.ignoring來解決這個問題,但爲什麼上面的代碼不起作用? –

回答

3

permitAll()不會忽略過濾器。只需授予訪問權限,無論在處理完所有過濾器後請求的安全上下文中是否存在Authentication

您應該檢查您的過濾器和任何AuthenticationProvider實現,以確保它們不會通過拋出未檢查/未捕獲的異常或在失敗的身份驗證中明確發送響應來破壞Spring Security的執行流。