Add DestinationUserNameProvider interface

The interface is to be implemented in addition to
java.security.Principal when Principal.getName() is not globally unique
enough for use in user destinations.

Issue: SPR-11327
This commit is contained in:
Rossen Stoyanchev
2014-01-21 11:50:04 -05:00
parent 2cafe9d73a
commit e4ad2b352e
5 changed files with 117 additions and 27 deletions

View File

@@ -35,6 +35,7 @@ import org.springframework.messaging.simp.stomp.StompConversionException;
import org.springframework.messaging.simp.stomp.StompDecoder;
import org.springframework.messaging.simp.stomp.StompEncoder;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.simp.user.DestinationUserNameProvider;
import org.springframework.messaging.simp.user.UserSessionRegistry;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert;
@@ -240,11 +241,20 @@ public class StompSubProtocolHandler implements SubProtocolHandler {
if (principal != null) {
headers.setNativeHeader(CONNECTED_USER_HEADER, principal.getName());
if (this.userSessionRegistry != null) {
this.userSessionRegistry.registerSessionId(principal.getName(), session.getId());
String userName = getNameForUserSessionRegistry(principal);
this.userSessionRegistry.registerSessionId(userName, session.getId());
}
}
}
private String getNameForUserSessionRegistry(Principal principal) {
String userName = principal.getName();
if (principal instanceof DestinationUserNameProvider) {
userName = ((DestinationUserNameProvider) principal).getDestinationUserName();
}
return userName;
}
@Override
public String resolveSessionId(Message<?> message) {
StompHeaderAccessor headers = StompHeaderAccessor.wrap(message);
@@ -258,8 +268,10 @@ public class StompSubProtocolHandler implements SubProtocolHandler {
@Override
public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus, MessageChannel outputChannel) {
if ((this.userSessionRegistry != null) && (session.getPrincipal() != null)) {
this.userSessionRegistry.unregisterSessionId(session.getPrincipal().getName(), session.getId());
Principal principal = session.getPrincipal();
if ((this.userSessionRegistry != null) && (principal != null)) {
String userName = getNameForUserSessionRegistry(principal);
this.userSessionRegistry.unregisterSessionId(userName, session.getId());
}
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.DISCONNECT);