Solon Cloud Gateway 开发:Route 的过滤器与定制

RouteFilterFactory 是专为路由过滤拦截处理设计的接口。对应路由配置 filters

1、内置的路由过滤器

过滤器工厂 本置前缀 说明与示例
AddRequestHeaderFilterFactory AddRequestHeader= 添加请求头 (AddRequestHeader=Demo-Ver,1.0)
AddResponseHeaderFilterFactory AddResponseHeader= 添加响应头 (AddResponseHeader=Demo-Ver,1.0)
PrefixPathFilterFactory PrefixPath= 附加路径前缀 (PrefixPath=/app)
RedirectToFilterFactory RedirectTo= 跳转到 (RedirectTo=302,http://demo.org/a,true)
RemoveRequestHeaderFilterFactory RemoveRequestHeader= 移除请求头 (RemoveRequestHeader=Demo-Ver,1.0)
RemoveResponseHeaderFilterFactory RemoveResponseHeader= 移除响应头 (RemoveResponseHeader=Demo-Ver,1.0)
StripPrefixFilterFactory StripPrefix= 移除路径前缀段数 (StripPrefix=1)

2、定制示例

  • StripPrefix 过滤器定制示例(配置例:StripPrefix=1
java 复制代码
@Component
public class StripPrefixFilterFactory implements RouteFilterFactory {

    @Override
    public String prefix() {
        return "StripPrefix";
    }

    @Override
    public ExFilter create(String config) {
        return new StripPrefixFilter(config);
    }

    public static class StripPrefixFilter implements ExFilter {
        private int parts;

        public StripPrefixFilter(String config) {
            if (Utils.isBlank(config)) {
                throw new IllegalArgumentException("StripPrefixFilter config cannot be blank");
            }

            this.parts = Integer.parseInt(config);
        }

        @Override
        public Completable doFilter(ExContext ctx, ExFilterChain chain) {
            //目标路径重组
            List<String> pathFragments = Arrays.asList(ctx.newRequest().getPath().split("/", -1));
            String newPath = "/" + String.join("/", pathFragments.subList(parts + 1, pathFragments.size()));
            ctx.newRequest().path(newPath);

            return chain.doFilter(ctx);
        }
    }
}
相关推荐
华洛3 分钟前
我用AI做了一个48秒的真人精品漫剧,不难也不贵
前端·javascript·后端
华科易迅3 分钟前
MybatisPlus增删改查操作
android·java·数据库
WZTTMoon8 分钟前
Spring Boot 中Servlet、Filter、Listener 四种注册方式全解析
spring boot·后端·servlet
standovon33 分钟前
Spring Boot整合Redisson的两种方式
java·spring boot·后端
Cosolar1 小时前
LlamaIndex RAG 本地部署+API服务,快速搭建一个知识库检索助手
后端·openai·ai编程
IAUTOMOBILE1 小时前
Python 流程控制与函数定义:从调试现场到工程实践
java·前端·python
hutengyi1 小时前
PostgreSQL版本选择
java
皮皮林5511 小时前
重磅!JetBrains 正式发布全新的 AI 开发工具,定名 AI IDE AIR
java·intellij idea
MX_93591 小时前
SpringMVC请求参数
java·后端·spring·servlet·apache
ID_180079054732 小时前
小红书笔记评论 API,Python 调用示例与完整 JSON 返回参考
java·开发语言