1 year ago

#339792

test-img

Wais Kamal

How can I add a Principal in configureClientInboundChannel?

I am using SockJS + STOMP + Spring WebSocket with a configuration that receives user credentials during the handshake. The credentials are received in the configureClientInboundChannel method:

@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
    registration.interceptors(new ChannelInterceptor() {
        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message,
                    StompHeaderAccessor.class);
            if (accessor != null && (StompCommand.CONNECT.equals(accessor.getCommand())
                    || StompCommand.SEND.equals(accessor.getCommand()))) {
                List<String> auth = accessor.getNativeHeader("Authorization");
                System.out.printf("Authorization: %s%n", auth.get(0));
            }
            return message;
        }
    });
}

I would like to add a Principal based on the token I receive in the header. I have a handler that registers Principals:

public class PrincipalHandshakeHandler extends DefaultHandshakeHandler {
    @Override
    protected Principal determineUser(ServerHttpRequest request,
            WebSocketHandler handler, Map<String, Object> attributes) {
        System.out.println(attributes);
        return new Principal() {
            @Override
            public String getName() {
                return userId;
            }
        };
    }
}

But how do I get pass the token I get in configureClientInboundChannel to the above handshake handler? Basically, the token should go there in place of userId.

java

spring

spring-websocket

0 Answers

Your Answer

Accepted video resources