Spring Boot 拦截rsockets中的WebSocket初始握手

q5lcpyga  于 2024-01-06  发布在  Spring
关注(0)|答案(2)|浏览(161)

我希望能够拦截客户端向我的端点发出的第一个请求,以便能够使用101(已建立WebSocket)或拒绝它。
有什么拦截器可以用吗?我试着注册一个过滤器bean,但它似乎只是跳过了,我想是因为我在一个webflux应用程序中。

  1. @Bean
  2. public FilterRegistrationBean<ContextFilter> initialFilter() {
  3. FilterRegistrationBean<ContextFilter> registrationBean
  4. = new FilterRegistrationBean<>();
  5. registrationBean.setFilter(new MyFilter());
  6. registrationBean.addUrlPatterns("/*");
  7. registrationBean.setOrder(1);
  8. return registrationBean;
  9. }

字符串

  1. public class MyFilter extends OncePerRequestFilter{
  2. @Override
  3. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  4. throws ServletException, IOException {
  5. log.info("You know, for filtering");
  6. filterChain.doFilter(request, response);
  7. }

d6kp6zgx

d6kp6zgx1#

要建立RSocket连接,客户端必须发送第一个称为SETUP帧的帧。您可以使用Spring @ConnectMapping annotation捕获它。

  1. @ConnectMapping
  2. fun onConnect(rSocketRequester: RSocketRequester, clientId: String) {
  3. if (clientId == "wrong_client") rSocketRequester.rsocket().dispose() //to reject connection
  4. }

字符串
要拒绝连接,请使用如上所示的rSocketRequester.rsocket().dispose()。注意,使用SETUP帧,您可以像往常一样从客户端传递负载和元数据。

ilmyapht

ilmyapht2#

您可以通过实现WebSocketHandler拦截第一个请求,并使用ServerHttpRequest访问请求的头和查询参数,并使用ServerHttpResponse修改响应以返回101状态代码。

  1. import io.rsocket.RSocket;
  2. import io.rsocket.core.RSocketServer;
  3. import io.rsocket.transport.netty.server.CloseableChannel;
  4. import io.rsocket.transport.netty.server.WebsocketServerTransport;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.web.reactive.HandlerMapping;
  9. import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
  10. import org.springframework.web.reactive.socket.WebSocketHandler;
  11. import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;
  12. import org.springframework.http.server.reactive.ServerHttpRequest;
  13. import org.springframework.http.server.reactive.ServerHttpResponse;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. @Configuration
  17. public class WebSocketConfig {
  18. @Bean
  19. public RSocket rSocket() {
  20. // Your RSocket bean configuration
  21. return RSocketServer.create()
  22. .bind(WebsocketServerTransport.create("localhost", 8080))
  23. .start()
  24. .block();
  25. }
  26. @Bean
  27. public HandlerMapping webSocketHandlerMapping() {
  28. Map<String, WebSocketHandler> map = new HashMap<>();
  29. map.put("/rsocket", new CustomWebSocketHandler());
  30. SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
  31. mapping.setOrder(1);
  32. mapping.setUrlMap(map);
  33. return mapping;
  34. }
  35. @Bean
  36. public WebSocketHandlerAdapter handlerAdapter() {
  37. return new WebSocketHandlerAdapter();
  38. }
  39. private static class CustomWebSocketHandler implements WebSocketHandler {
  40. @Override
  41. public CloseableChannel handle(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
  42. // Access headers and query parameters here
  43. String headerValue = serverHttpRequest.getHeaders().getFirst("your-header");
  44. String queryParamValue = serverHttpRequest.getQueryParams().getFirst("your-query-param");
  45. // Modify the response to return 101 status code
  46. serverHttpResponse.setStatusCode(HttpStatus.SWITCHING_PROTOCOLS);
  47. // Implement your logic here
  48. return null; // Return the CloseableChannel or handle as needed
  49. }
  50. }
  51. }

字符串

展开查看全部

相关问题