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
This commit is contained in:
Rossen Stoyanchev
2014-01-13 15:28:17 -05:00
parent 547646de6d
commit 6265bc1df7
3 changed files with 26 additions and 9 deletions

View File

@@ -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<Session> {
/**
* 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<String, Object> handshakeAttributes) {
public JettyWebSocketSession(Map<String, Object> 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<String, Object> handshakeAttributes, Principal principal) {
super(handshakeAttributes);
this.principal = principal;
}
@@ -90,7 +103,11 @@ public class JettyWebSocketSession extends AbstractWebSocketSession<Session> {
@Override
public Principal getPrincipal() {
return this.principal;
if (this.principal != null) {
return this.principal;
}
checkNativeSessionInitialized();
return getNativeSession().getUpgradeRequest().getUserPrincipal();
}
@Override

View File

@@ -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<WebSocketSession>() {
@@ -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;

View File

@@ -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 =