diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java index bf2714eee5..517594c08a 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,11 +55,24 @@ public class JettyWebSocketSession extends AbstractWebSocketSession { /** * Create a new {@link JettyWebSocketSession} instance. - * @param principal the user associated with the session, or {@code null} + * * @param handshakeAttributes attributes from the HTTP handshake to make available * through the WebSocket session */ - public JettyWebSocketSession(Principal principal, Map handshakeAttributes) { + public JettyWebSocketSession(Map handshakeAttributes) { + this(handshakeAttributes, null); + } + + /** + * Create a new {@link JettyWebSocketSession} instance associated with the given user. + * + * @param handshakeAttributes attributes from the HTTP handshake to make available + * through the WebSocket session + * @param principal the user associated with the session; can be left + * {@code null} in which case, we'll fallback on the user available via + * {@link org.eclipse.jetty.websocket.api.Session#getUpgradeRequest()} + */ + public JettyWebSocketSession(Map handshakeAttributes, Principal principal) { super(handshakeAttributes); this.principal = principal; } @@ -90,7 +103,11 @@ public class JettyWebSocketSession extends AbstractWebSocketSession { @Override public Principal getPrincipal() { - return this.principal; + if (this.principal != null) { + return this.principal; + } + checkNativeSessionInitialized(); + return getNativeSession().getUpgradeRequest().getUserPrincipal(); } @Override diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/client/jetty/JettyWebSocketClient.java b/spring-websocket/src/main/java/org/springframework/web/socket/client/jetty/JettyWebSocketClient.java index 1e02a8ad5b..5d83118e32 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/client/jetty/JettyWebSocketClient.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/client/jetty/JettyWebSocketClient.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -186,7 +186,7 @@ public class JettyWebSocketClient extends AbstractWebSocketClient implements Sma } Principal user = getUser(); - final JettyWebSocketSession wsSession = new JettyWebSocketSession(user, handshakeAttributes); + final JettyWebSocketSession wsSession = new JettyWebSocketSession(handshakeAttributes, user); final JettyWebSocketHandlerAdapter listener = new JettyWebSocketHandlerAdapter(wsHandler, wsSession); return this.taskExecutor.submitListenable(new Callable() { @@ -201,7 +201,7 @@ public class JettyWebSocketClient extends AbstractWebSocketClient implements Sma /** * @return the user to make available through {@link WebSocketSession#getPrincipal()}; - * by default this method returns {@code null} + * by default this method returns {@code null} */ protected Principal getUser() { return null; diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/jetty/JettyRequestUpgradeStrategy.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/jetty/JettyRequestUpgradeStrategy.java index 12ed94c426..2115212795 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/jetty/JettyRequestUpgradeStrategy.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/jetty/JettyRequestUpgradeStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -141,7 +141,7 @@ public class JettyRequestUpgradeStrategy implements RequestUpgradeStrategy { Assert.isTrue(this.factory.isUpgradeRequest(servletRequest, servletResponse), "Not a WebSocket handshake"); - JettyWebSocketSession session = new JettyWebSocketSession(request.getPrincipal(), attributes); + JettyWebSocketSession session = new JettyWebSocketSession(attributes, request.getPrincipal()); JettyWebSocketHandlerAdapter handlerAdapter = new JettyWebSocketHandlerAdapter(wsHandler, session); WebSocketHandlerContainer container =