springcloud gateway websocket拦截

配置websocket路径:

spring.cloud.gateway.routes[7].id=testws
spring.cloud.gateway.routes[7].uri=lb:ws://iflynote-fs
spring.cloud.gateway.routes[7].filters[0]=StripPrefix=1
spring.cloud.gateway.routes[7].predicates[0]=Path=/testws/**
spring.cloud.gateway.routes[7].predicates[1]=Query=name

创建拦截器:

@Component
public class WsRoutingFilter extends WebsocketRoutingFilter {
    public WsRoutingFilter(WebSocketClient webSocketClient, WebSocketService webSocketService, ObjectProvider<List<HttpHeadersFilter>> headersFiltersProvider) {
        super(webSocketClient, webSocketService, headersFiltersProvider);
    }

    @Override
    public int getOrder() {
        return super.getOrder();
    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println(exchange.getRequest().getPath());
        System.out.println(exchange.getRequest().getQueryParams());
        return super.filter(exchange, chain);
    }

}

可以根据path header进行拦截做相应的业务处理

使用网关连接websocket地址:

ws://localhost:8081/testws/websocket?name=666

发表新评论