[英]Spring - Access denied for an Anonymous Authentication
我在Spring Security方面沒有很多專業知識,而且我有一個可能有點愚蠢的問題。 我已經嘗試解決問題了幾天,也許我誤解了配置應用程序的方式。 如果有人能對此有所啟發,我將不勝感激。
我用Spring實現了REST API,我想為其增加安全性。 從Spring Security文檔中,我讀到了…… 一般認為,采用“默認拒絕”的做法是一種良好的安全做法,您可以在其中明確指定允許的內容並禁止其他所有內容。 ...我同意。 因此,我將身份驗證要求添加到了配置中。
但是,我想很少公開(從我的GUI)對API的調用,因此我認為匿名身份驗證可以工作。 再次,從文檔中我讀到了…… 在“匿名身份驗證”的用戶和未經身份驗證的用戶之間沒有真正的概念差異,所以一切都很好。
但是,當我執行這些調用時,我得到403響應(AbstractSecurityInterceptor會在決定有關Authentication時拋出AccessDeniedException)。 因此,如果您知道我有什么問題和誤解,我在這里留下幾個問題和我的配置行。
配置...
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.cors().and()
.anonymous().and()
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers(HttpMethod.POST, "/users/login").permitAll()
.anyRequest().authenticated().and()
// We filter the api/login requests
.addFilterBefore(new JwtLoginFilter("/users/login", authenticationManager()),
UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new JwtAuthenticationFilter(),
UsernamePasswordAuthenticationFilter.class);
}
問題... 1)AnonymousAuthenticationToken仍通過身份驗證,對嗎?
2)如果我沒有自定義匿名身份驗證的配置,是否需要anonymous()行? 缺少身份驗證時(在我調試時)會有匿名令牌
3)在配置中缺少任何內容來接受那些請求嗎?
4)文檔提到了ExceptionTranslatorFilter,它應該將AccessDeniedException處理到AuthenticationEntryPoint。 我需要定義AuthenticationEntryPoint嗎? 如果是這樣,目的是什么?
我嘗試盡可能精確。 我希望有人可以答復。 非常感謝您的幫助!
您需要將其放入配置文件中:
http.authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/**").permitAll();
您是否具有未經授權的對根“ /”的訪問? 我問這個問題是因為看起來像是您打算在該行中那樣: .antMatchers("/").permitAll()
我想為了允許未經身份驗證的請求(或匿名經過身份驗證的請求,請注意,對於Spring Anonymous和未經身份驗證的請求基本上是相同的),您需要為這些路徑添加ant匹配,例如: .antMatchers("/public-page").permitAll()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.