官方文档-spring websocket/stomp
官方文档-spring session
共享Session
WebsocketSTOMPConfig:
@Configuration
@EnableWebSocketMessageBroker
@ConditionalOnWebApplication
public class WebsocketSTOMPConfig extends AbstractSessionWebSocketMessageBrokerConfigurer<ExpiringSession> {
/**
* AbstractSessionWebSocketMessageBrokerConfigurer实现了在handshake时获取httpsession,并且每次websocket消息发生时也刷新了httpsession的时间。同时在websocket session中加入了SPRING.SESSION.ID字段。
*/
@Override
protected void configureStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws")
// 在握手时就获得user,判断是否登录。
.addInterceptors(new SessionAuthHandshakeInterceptor())
.setHandshakeHandler(new DefaultHandshakeHandler(){
@Override
protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
return new MyPrincipal