栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring gateway ,spring security集成,webfilter执行两次的问题处理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring gateway ,spring security集成,webfilter执行两次的问题处理

spring gateway ,spring security集成,webfilter执行两次的问题处理 1.问题说明

spring gateway ,spring security集成时候,
使用@Component注册filter

@Component
public class XFilter implements WebFilter {

又在SecurityWebFilterChain里配置了filter

 	@Autowired
	XFilter xfilter;
  	@Bean
    SecurityWebFilterChain webFluxSecurityFilterChain(ServerHttpSecurity http) throws Exception {
        http .addFilterBefore(xfilter, SecurityWebFiltersOrder.CORS)

导致filter会执行两次

2.解决方法

1.不使用@Component注册filter,只在SecurityWebFilterChain里配置,如果要使用bean的话,需要通过构造器来注入,或者直接注入ApplicationContext

public class XFilter implements WebFilter {
    private DataCache dataCache;
    public XFilter(DataCache dataCache){
        this.dataCache = dataCache;
    }
    @Override
    public Mono filter(ServerWebExchange exchange, WebFilterChain chain) {
        return null;
    }
}
 	@Autowired
	DataCache dataCache;
  	@Bean
    SecurityWebFilterChain webFluxSecurityFilterChain(ServerHttpSecurity http) throws Exception {
        http .addFilterBefore(new XFilter(dataCache), SecurityWebFiltersOrder.CORS)

2.采用类似org.springframework.web.filter.OncePerRequestFilter的处理方法来处理

public abstract class AbstractMatchWebFilter implements WebFilter{

    public static final String ALREADY_FILTERED_SUFFIX = ".FILTERED";
    private final AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Override
    public Mono filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
        if (!hasFilter(serverWebExchange) && shouldFilter(serverWebExchange) && !shouldNotFilter(serverWebExchange)) {
            signFilter(serverWebExchange);
            return doFilter(serverWebExchange,webFilterChain);
        } else {
            return webFilterChain.filter(serverWebExchange);
        }
    }

    protected String getAlreadyFilteredAttributeName() {
        return getClass().getName() + ALREADY_FILTERED_SUFFIX;
    }

    private boolean hasFilter(ServerWebExchange exchange){
        Boolean sign = (Boolean) exchange.getAttributes().get(getAlreadyFilteredAttributeName());
        return Boolean.TRUE.equals(sign);
    }

    private void signFilter(ServerWebExchange exchange){
        exchange.getAttributes().put(getAlreadyFilteredAttributeName(),Boolean.TRUE);
    }

    protected abstract Mono doFilter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain);

    protected boolean shouldNotFilter(ServerWebExchange serverWebExchange) {
        return false;
    }

    protected boolean shouldFilter(ServerWebExchange serverWebExchange) {
        return true;
    }

    protected boolean match(String requestUrl,String authHref){
        return antPathMatcher.match(authHref,requestUrl);
    }
}

实现原理是在filter执行前检查在exchange Attributes里边的一个标记,如果没有就是第一次执行,添加此标记,如果有就不执行。此抽象类可以直接继承使用,还可以实现shouldNotFilter,shouldFilter来控制filter是否执行

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/841856.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号