@Override public Principal getPrincipal() { return this.delegate.getPrincipal(); }
@Override public Principal getPrincipal() { Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized"); return this.webSocketSession.getPrincipal(); }
@Nullable private Principal getUser(WebSocketSession session) { Principal user = this.stompAuthentications.get(session.getId()); return (user != null ? user : session.getPrincipal()); }
private void afterConnectionEstablished(WebSocketSession wsSession) { Principal principal = wsSession.getPrincipal(); if (principal == null) { return; } String httpSessionId = getHttpSessionId(wsSession); registerWsSession(httpSessionId, wsSession); }
if (isConnect) { Principal user = headerAccessor.getUser(); if (user != null && user != session.getPrincipal()) { this.stompAuthentications.put(session.getId(), user);
@Test public void onApplicationEventConnectSessionDeletedNullPrincipal() throws Exception { given(this.wsSession.getPrincipal()).willReturn(null); this.listener.onApplicationEvent(this.connect); this.listener.onApplicationEvent(this.deleted); verify(this.wsSession, times(0)).close(any(CloseStatus.class)); }
@Before public void setup() { String sessionId = "session-id"; MapSession session = new MapSession(sessionId); this.attributes = new HashMap<>(); SessionRepositoryMessageInterceptor.setSessionId(this.attributes, sessionId); given(this.wsSession.getAttributes()).willReturn(this.attributes); given(this.wsSession.getPrincipal()).willReturn(this.principal); given(this.wsSession.getId()).willReturn("wsSession-id"); given(this.wsSession2.getAttributes()).willReturn(this.attributes); given(this.wsSession2.getPrincipal()).willReturn(this.principal); given(this.wsSession2.getId()).willReturn("wsSession-id2"); Map<String, Object> headers = new HashMap<>(); headers.put(SimpMessageHeaderAccessor.SESSION_ATTRIBUTES, this.attributes); given(this.message.getHeaders()).willReturn(new MessageHeaders(headers)); this.listener = new WebSocketRegistryListener(); this.connect = new SessionConnectEvent(this.listener, this.wsSession); this.connect2 = new SessionConnectEvent(this.listener, this.wsSession2); this.disconnect = new SessionDisconnectEvent(this.listener, this.message, this.wsSession.getId(), CloseStatus.NORMAL); this.deleted = new SessionDeletedEvent(this.listener, session); this.expired = new SessionExpiredEvent(this.listener, session); }
@Override public Principal getPrincipal() { return this.delegate.getPrincipal(); }
@Override public Principal getPrincipal() { return this.delegate.getPrincipal(); }
@Override public void handleMessageFromClient(WebSocketSession session, WebSocketMessage<?> webSocketMessage, MessageChannel outputChannel) throws Exception { SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE); headerAccessor.setSessionId(session.getId()); headerAccessor.setSessionAttributes(session.getAttributes()); headerAccessor.setUser(session.getPrincipal()); headerAccessor.setHeader("content-length", webSocketMessage.getPayloadLength()); headerAccessor.setLeaveMutable(true); Message<?> message = MessageBuilder.createMessage(webSocketMessage.getPayload(), headerAccessor.getMessageHeaders()); try { SimpAttributesContextHolder.setAttributesFromMessage(message); outputChannel.send(message); } finally { SimpAttributesContextHolder.resetAttributes(); } }
@Override public Principal getPrincipal() { Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized"); return this.webSocketSession.getPrincipal(); }
@Override public Principal getPrincipal() { Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized"); return this.webSocketSession.getPrincipal(); }
@Override protected boolean shouldApplyHeaders(WebSocketSession userAgentSession, WebSocketHttpHeaders headers) { return !headers.containsKey(HttpHeaders.AUTHORIZATION) && userAgentSession.getPrincipal() instanceof Authentication; } }
@Nullable private Principal getUser(WebSocketSession session) { Principal user = this.stompAuthentications.get(session.getId()); return (user != null ? user : session.getPrincipal()); }
@Nullable private Principal getUser(WebSocketSession session) { Principal user = this.stompAuthentications.get(session.getId()); return (user != null ? user : session.getPrincipal()); }
@Override protected boolean shouldApplyHeaders(WebSocketSession userAgentSession, WebSocketHttpHeaders headers) { return !headers.containsKey(HttpHeaders.AUTHORIZATION) && userAgentSession.getPrincipal() instanceof OAuth2Authentication; } }
@Override protected void applyHeadersInternal(WebSocketSession userAgentSession, WebSocketHttpHeaders headers) { OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) userAgentSession.getPrincipal(); OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) oAuth2Authentication.getDetails(); String accessToken = details.getTokenValue(); headers.put(HttpHeaders.AUTHORIZATION, Collections.singletonList("Bearer " + accessToken)); if (logger.isDebugEnabled()) { logger.debug("Added Oauth2 bearer token authentication header for user " + oAuth2Authentication.getName() + " to web sockets http headers"); } }
@Override public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus, MessageChannel outputChannel) { Long wampSessionId = (Long) session.getAttributes() .get(WampMessageHeader.WAMP_SESSION_ID.name()); if (wampSessionId != null) { this.applicationEventPublisher.publishEvent(new WampDisconnectEvent( wampSessionId, session.getId(), session.getPrincipal())); this.wampSessionIds.remove(wampSessionId); session.getAttributes().remove(WampMessageHeader.WAMP_SESSION_ID.name()); } }
@Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { logger.debug("Opening session id {} ", session.getId()); session = new ConcurrentWebSocketSessionDecorator(session, sendTimeLimit, sendBufferSizeLimit); HiveWebsocketSessionState state = new HiveWebsocketSessionState(); session.getAttributes().put(HiveWebsocketSessionState.KEY, state); session.getAttributes().put(CommandHandlers.SUBSCRIPTION_SET_NAME, new CopyOnWriteArraySet<SubscriptionInfo>()); session.getAttributes().put(NotificationHandlers.SUBSCRIPTION_SET_NAME, new CopyOnWriteArraySet<SubscriptionInfo>()); session.getAttributes().put(WebSocketAuthenticationManager.SESSION_ATTR_AUTHENTICATION, session.getPrincipal()); sessionMonitor.registerSession(session); }
public static <T extends WampMessage> T fromJson(WebSocketSession session, JsonFactory jsonFactory, String json) throws IOException { WampSession wampSession = new WampSession(session); T newWampMessage = fromJson(jsonFactory, json, wampSession); newWampMessage.setWebSocketSessionId(session.getId()); newWampMessage.setPrincipal(session.getPrincipal()); newWampMessage.setWampSession(wampSession); return newWampMessage; }