我在弹簧启动应用程序中配置了过滤器的代码.当我发出请求时,我的第二个过滤器是B,不会调用.
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity webSecurity) throws Exception {
webSecurity.ignoring().antMatchers(HttpMethod.GET,"/health");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(new A(),BasicAuthenticationFilter.class);
http.addFilterAfter(new B(),new A().getClass());
}
}
import org.springframework.web.filter.GenericFilterBean;
public class A extends GenericFilterBean {
@Override
public void doFilter(ServletRequest arg0,ServletResponse arg1,FilterChain arg2)
throws IOException,ServletException {
System.out.println("filter A");
}
}
import org.springframework.web.filter.GenericFilterBean;
public class B extends GenericFilterBean {
@Override
public void doFilter(ServletRequest arg0,ServletException {
System.out.println("filter B");
}
}
编辑:
public class A extends GenericFilterBean {
@Override
public void doFilter(ServletRequest arg0,FilterChain arg2) throws IOException,ServletException {
System.out.println("filter A Before");
arg2.doFilter(arg0,arg1);
System.out.println("filter A After");
}
}
最佳答案
你的配置是正确的.但是你需要将你的请求从过滤器A传递到过滤器B,如M. Deinum所述.只是打印不行.在你的代码中它应该像过滤器A中的arg2.doFilter().
原文链接:https://www.f2er.com/spring/431569.html从docs开始,它说,
A typical implementation of this method would follow the following pattern:
>检查请求
>可选择使用自定义实现将请求对象包装到
过滤内容或标题以进行输入过滤
>可选择使用自定义实现包装响应对象
过滤内容或标题以进行输出过滤
>使用FilterChain调用链中的下一个实体
object(chain.doFilter()),或者不传递请求/响应对
到筛选器链中的下一个实体来阻止请求
处理
>在调用下一个响应后直接在响应上设置标头
过滤器链中的实体.