在授權服務器中,由於某些客戶端ID的操作,需要添加自定義BasicAuthenticationFilter。大多數實現與BasicAuthenticationFilter
相同。以下是摘錄的一樣,Spring Security OAuth2禁用默認過濾器鏈中的BasicAuthenticationFilter
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) {
...
...
String username = someDecoder(tokens[0]); // Kind of something
...
...
}
而我的自定義過濾器過濾器鏈放置BasicAuthenticationFilter
之前。
http.addFilterBefore(new CustomBasicAuthenticationFilter(authenticationManager(), authenticationEntryPoint()),
BasicAuthenticationFilter.class);
此自定義篩選器工作得很棒,用戶也可以成功進行身份驗證。但由於BasicAuthenticationFilter仍然存在於鏈中,因此此篩選器也會得到執行並嘗試再次驗證用戶,但由於未操作客戶端憑據而失敗。請參閱BasicAuthenticationFilter-GitHub
因此,要從過濾器鏈中刪除/禁用BasicAuthenticationFilter
,請遵循此SOQ,其中建議使用BeanPostProcessor
。但是在Spring Boot過濾器鏈中註冊了豆名springSecurityFilterChain和FilterChainProxy
。正如FilterChainProxy-GitHub返回不可修改的SecurityFilterChain
列表。所以它的下一個不可能改變FilterChainProxy
豆。
那麼如何實現相同或任何其他方式來刪除/禁用BasicAuthenticationFilter
或Spring Security Filter鏈中的任何其他過濾器。
使用Spring 1.5.1引導和Spring安全的OAuth2 2.0.12
來自[Spring doc](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-add-a-servlet-filter-or-listener-as-春豆)。您只能禁用那些使用'FilterRegistrationBean'註冊的filter/servlet。 'BasicAuthenticationFilter'沒有註冊。 [檢查](https://github.com/spring-projects/spring-security/blob/a82cab7afdb1fc58830b1c415f1874d36b2c6c92/config/src/main/java/org/springframework/security/config/annotation/web/configurers/HttpBasicConfigurer.java# L208) – Sheel
嘗試解決方案的具體錯誤是'考慮在你的配置中定義一個'org.springframework.security.web.authentication.www.BasicAuthenticationFilter'類型的bean。 – Sheel