阻止Spring Boot注册Servlet过滤器
有一个Spring Boot" title="Spring Boot">Spring Boot WebMVC应用程序,还有一个从AbstractPreAuthenticatedProcessingFilter继承的bean,我将其显式添加到Spring Security过滤器链中的特定位置。我的Spring Security配置如下所示:
<http pattern="/rest/**"> <intercept-url pattern="/**" access="ROLE_USER"/>
<http-basic/>
<custom-filter after="BASIC_AUTH_FILTER" ref="preAuthenticationFilter"/>
</http>
<beans:bean id="preAuthenticationFilter" class="a.b.PreAuthenticationFilter">
<beans:property name="authenticationManager" ref="customAuthenticationManager"/>
</beans:bean>
安全配置有效。问题是,因为PreAuthenticationFilter类继承自AbstractPreAuthenticatedProcessingFilter,所以Spring Boot将其视为通用servlet过滤器,并将其添加到所有请求的servlet过滤器链中。我不希望此过滤器成为所有请求的过滤器链的一部分。我只希望它成为我配置的特定Spring Security过滤器链的一部分。有没有一种方法可以防止Spring Boot自动将preAuthenticationFilter bean添加到过滤器链中?
回答:
默认情况下,Spring Boot 在应用程序上下文中为FilterRegistrationBean
每个尚不存在的对象创建一个。这使你可以采取注册过程的控制,包括禁止注册,宣称自己的。对于你所需的配置如下所示:FilterFilterRegistrationBean FilterRegistrationBean FilterPreAuthenticationFilter
@Beanpublic FilterRegistrationBean registration(PreAuthenticationFilter filter) {
FilterRegistrationBean registration = new FilterRegistrationBean(filter);
registration.setEnabled(false);
return registration;
}
以上是 阻止Spring Boot注册Servlet过滤器 的全部内容, 来源链接: utcz.com/qa/403564.html