Java Code Examples for org.springframework.messaging.simp.SimpMessageHeaderAccessor#getUser()

The following examples show how to use org.springframework.messaging.simp.SimpMessageHeaderAccessor#getUser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebSocketConnectHandler.java    From bearchoke with Apache License 2.0 7 votes vote down vote up
@Override
    public void onApplicationEvent(SessionConnectEvent event) {
        if (log.isDebugEnabled()) {
            log.debug("Caught Web Socket connect event");
        }

        MessageHeaders headers = event.getMessage().getHeaders();
        Principal user = SimpMessageHeaderAccessor.getUser(headers);

        if(user == null) {
            return;
        }

        String id = SimpMessageHeaderAccessor.getSessionId(headers);

        if (log.isDebugEnabled()) {
            log.debug("Current web socket session id: " + id);
        }

        if (StringUtils.isNotBlank(id)) {
            repository.save(new ActiveWebSocketUser(id, user.getName(), Calendar.getInstance()));
        }

//        messagingTemplate.convertAndSend("/topic/friends/signin", Arrays.asList(user.getName()));
    }
 
Example 2
Source File: DefaultUserDestinationResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Nullable
private ParseResult parseSubscriptionMessage(Message<?> message, String sourceDestination) {
	MessageHeaders headers = message.getHeaders();
	String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
	if (sessionId == null) {
		logger.error("No session id. Ignoring " + message);
		return null;
	}
	int prefixEnd = this.prefix.length() - 1;
	String actualDestination = sourceDestination.substring(prefixEnd);
	if (isRemoveLeadingSlash()) {
		actualDestination = actualDestination.substring(1);
	}
	Principal principal = SimpMessageHeaderAccessor.getUser(headers);
	String user = (principal != null ? principal.getName() : null);
	Set<String> sessionIds = Collections.singleton(sessionId);
	return new ParseResult(sourceDestination, actualDestination, sourceDestination, sessionIds, user);
}
 
Example 3
Source File: StompSubProtocolHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test // SPR-14690
public void handleMessageFromClientWithTokenAuthentication() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
	channel.addInterceptor(new AuthenticationInterceptor("[email protected]"));
	channel.addInterceptor(new ImmutableMessageChannelInterceptor());

	TestMessageHandler messageHandler = new TestMessageHandler();
	channel.subscribe(messageHandler);

	StompSubProtocolHandler handler = new StompSubProtocolHandler();
	handler.afterSessionStarted(this.session, channel);

	TextMessage wsMessage = StompTextMessageBuilder.create(StompCommand.CONNECT).build();
	handler.handleMessageFromClient(this.session, wsMessage, channel);

	assertEquals(1, messageHandler.getMessages().size());
	Message<?> message = messageHandler.getMessages().get(0);
	Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders());
	assertNotNull(user);
	assertEquals("[email protected]", user.getName());
}
 
Example 4
Source File: DefaultUserDestinationResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Nullable
private ParseResult parseSubscriptionMessage(Message<?> message, String sourceDestination) {
	MessageHeaders headers = message.getHeaders();
	String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
	if (sessionId == null) {
		logger.error("No session id. Ignoring " + message);
		return null;
	}
	int prefixEnd = this.prefix.length() - 1;
	String actualDestination = sourceDestination.substring(prefixEnd);
	if (isRemoveLeadingSlash()) {
		actualDestination = actualDestination.substring(1);
	}
	Principal principal = SimpMessageHeaderAccessor.getUser(headers);
	String user = (principal != null ? principal.getName() : null);
	Set<String> sessionIds = Collections.singleton(sessionId);
	return new ParseResult(sourceDestination, actualDestination, sourceDestination, sessionIds, user);
}
 
Example 5
Source File: StompSubProtocolHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test // SPR-14690
public void handleMessageFromClientWithTokenAuthentication() {
	ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
	channel.addInterceptor(new AuthenticationInterceptor("[email protected]"));
	channel.addInterceptor(new ImmutableMessageChannelInterceptor());

	TestMessageHandler messageHandler = new TestMessageHandler();
	channel.subscribe(messageHandler);

	StompSubProtocolHandler handler = new StompSubProtocolHandler();
	handler.afterSessionStarted(this.session, channel);

	TextMessage wsMessage = StompTextMessageBuilder.create(StompCommand.CONNECT).build();
	handler.handleMessageFromClient(this.session, wsMessage, channel);

	assertEquals(1, messageHandler.getMessages().size());
	Message<?> message = messageHandler.getMessages().get(0);
	Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders());
	assertNotNull(user);
	assertEquals("[email protected]", user.getName());
}
 
Example 6
Source File: PrincipalMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
	Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders());
	if (user == null) {
		throw new MissingSessionUserException(message);
	}
	return user;
}
 
Example 7
Source File: SendToMethodReturnValueHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
protected String getUserName(Message<?> message, MessageHeaders headers) {
	Principal principal = SimpMessageHeaderAccessor.getUser(headers);
	if (principal != null) {
		return (principal instanceof DestinationUserNameProvider ?
				((DestinationUserNameProvider) principal).getDestinationUserName() : principal.getName());
	}
	return null;
}
 
Example 8
Source File: PrincipalMethodArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
	Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders());
	if (user == null) {
		throw new MissingSessionUserException(message);
	}
	return user;
}
 
Example 9
Source File: SendToMethodReturnValueHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
protected String getUserName(Message<?> message, MessageHeaders headers) {
	Principal principal = SimpMessageHeaderAccessor.getUser(headers);
	if (principal != null) {
		return (principal instanceof DestinationUserNameProvider ?
				((DestinationUserNameProvider) principal).getDestinationUserName() : principal.getName());
	}
	return null;
}
 
Example 10
Source File: PrincipalMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
	Principal user = SimpMessageHeaderAccessor.getUser(message.getHeaders());
	if (user == null) {
		throw new MissingSessionUserException(message);
	}
	return user;
}
 
Example 11
Source File: SendToMethodReturnValueHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected String getUserName(Message<?> message, MessageHeaders headers) {
	Principal principal = SimpMessageHeaderAccessor.getUser(headers);
	if (principal != null) {
		return (principal instanceof DestinationUserNameProvider ?
				((DestinationUserNameProvider) principal).getDestinationUserName() : principal.getName());
	}
	return null;
}
 
Example 12
Source File: WebSocketConfiguration.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@Override
public Message<?> beforeHandle(Message<?> message, MessageChannel channel, MessageHandler handler) {
    SimpMessageHeaderAccessor smha = SimpMessageHeaderAccessor.wrap(message);
    Principal user = smha.getUser();
    if(user instanceof Authentication) {
        TempAuth auth = TempAuth.open((Authentication)user);
        smha.setHeader(AUTH_KEY, auth);
    }
    return message;
}
 
Example 13
Source File: WebSocketConnectHandler.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(SessionConnectEvent event) {
	MessageHeaders headers = event.getMessage().getHeaders();
	Principal user = SimpMessageHeaderAccessor.getUser(headers);
	if (user == null) {
		return;
	}
	String id = SimpMessageHeaderAccessor.getSessionId(headers);
	this.repository.save(new ActiveWebSocketUser(id, user.getName(), Calendar.getInstance()));
	this.messagingTemplate.convertAndSend("/topic/friends/signin", Collections.singletonList(user.getName()));
}
 
Example 14
Source File: SimpleBrokerMessageHandler.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected void handleMessageInternal(Message<?> message) {
	MessageHeaders headers = message.getHeaders();
	SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
	String destination = SimpMessageHeaderAccessor.getDestination(headers);
	String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
	Principal user = SimpMessageHeaderAccessor.getUser(headers);

	updateSessionReadTime(sessionId);

	if (!checkDestinationPrefix(destination)) {
		return;
	}

	SimpMessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
	if (accessor == null) {
		throw new IllegalStateException(
				"No header accessor (not using the SimpMessagingTemplate?): " + message);
	}

	if (SimpMessageType.MESSAGE.equals(messageType)) {
		logMessage(message);
		sendMessageToSubscribers(destination, message);
	}
	else if (SimpMessageType.CONNECT.equals(messageType)) {
		logMessage(message);
		long[] clientHeartbeat = SimpMessageHeaderAccessor.getHeartbeat(headers);
		long[] serverHeartbeat = getHeartbeatValue();
		this.sessions.put(sessionId, new SessionInfo(sessionId, user, clientHeartbeat, serverHeartbeat));
		SimpMessageHeaderAccessor connectAck = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
		initHeaders(connectAck);
		connectAck.setSessionId(sessionId);
		connectAck.setUser(SimpMessageHeaderAccessor.getUser(headers));
		connectAck.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, message);
		connectAck.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, serverHeartbeat);
		Message<byte[]> messageOut = MessageBuilder.createMessage(EMPTY_PAYLOAD, connectAck.getMessageHeaders());
		getClientOutboundChannel().send(messageOut);
	}
	else if (SimpMessageType.DISCONNECT.equals(messageType)) {
		logMessage(message);
		handleDisconnect(sessionId, user);
	}
	else if (SimpMessageType.SUBSCRIBE.equals(messageType)) {
		logMessage(message);
		this.subscriptionRegistry.registerSubscription(message);
	}
	else if (SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
		logMessage(message);
		this.subscriptionRegistry.unregisterSubscription(message);
	}
}