From 6265bc1df781cde3c4ceb104096dd61483cec7cd Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 13 Jan 2014 15:28:17 -0500 Subject: [PATCH] Fall back on user in the Jetty UpgradeRequest The Jetty ServletWebUpgradeRequest implements getUserPrincipal to return the Principal from the HttpServletRequest on the upgrade. This change ensures we can fall back on that. However the JettyRequestUpgradeStrategy still passes the user from HttpServletRequest from the upgrade, in order to work with Jetty 9.0.x and avoid running into this 9.1.x issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=423118 --- .../adapter/jetty/JettyWebSocketSession.java | 25 ++++++++++++++++--- .../client/jetty/JettyWebSocketClient.java | 6 ++--- .../jetty/JettyRequestUpgradeStrategy.java | 4 +-- 3 files changed, 26 insertions(+), 9 deletions(-) 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 =