gpt4 book ai didi

spring-boot - 如何使用spring boot stomp在ChannelInterceptor中获取端点路径?

转载 作者:行者123 更新时间:2023-12-04 15:43:14 24 4
gpt4 key购买 nike

我是 stomp 的新手,使用 spring boot 2.1.2.RELEASE。我有多个端点并配置了一个 ChannelInterceptor获取一些信息。

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {

registry.addEndpoint("/endpoint1")
.addInterceptors(new IpHandshakeInterceptor())
.setAllowedOrigins(origin)
.withSockJS();

registry.addEndpoint("/endpoint2")
.addInterceptors(new IpHandshakeInterceptor())
.setAllowedOrigins(origin)
.withSockJS();
// other andpoint
}

@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.interceptors(myChannelInterceptor());
}

所有端点使用 myChannelInterceptor (实际上,我希望端点使用自己的 ChannelInterceptor),我想在 ChannelInterceptor 中做事通过端点路径。

@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
if (endpoint.equals("endpoint1")) {
} else if (endpoint.equals("endpoint2")) {
}
}

我如何获得 endpoint信息在 ChannelInterceptor ?

最佳答案

您可以使用:

  • 在 IphandshakeInterceptor 类中将值写入属性映射:
     @Override
    public boolean beforeHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Map<String, Object> map) throws Exception {
    if (serverHttpRequest instanceof ServletServerHttpRequest) {
    ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) serverHttpRequest;
    HttpSession session = servletRequest.getServletRequest().getSession();
    //add value to session attributes
    map.put("endpoint", servletRequest.getURI().getPath());
    }
    // ... your logic ...
    return true;
    }
  • 在您的 myChannelInterceptor 从 session 属性中读取值:
     @Override
    public Message<?> preSend(final Message<?> message, final MessageChannel channel) throws AuthenticationException {
    final StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
    String endpoint=accessor.getSessionAttributes().get("endpoint").toString();
    // ... your logic ...
    return message;
    }
  • 关于spring-boot - 如何使用spring boot stomp在ChannelInterceptor中获取端点路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56927830/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com