public class ServletAwareConfig extends ServerEndpointConfig.Configurator { @Override public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) { HttpSession httpSession = (HttpSession) request.getHttpSession(); config.getUserProperties().put("httpSession", httpSession); } }
@Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { Map<String, List<String>> requestHeaders = request.getHeaders(); List<String> cookieHeaders = requestHeaders.get("cookie"); String sessionId = null; if (cookieHeaders != null && cookieHeaders.size() > 0) { for (String cookieHeader : cookieHeaders) { Matcher matcher = cookiePattern.matcher(cookieHeader); while (matcher.find()) { String cookieKey = matcher.group(1); String cookieValue = matcher.group(2); if (cookieKey.equals("CRASHID")) { sessionId = cookieValue; } } } } if (sessionId == null) { sessionId = UUID.randomUUID().toString(); response.getHeaders().put("Set-Cookie", Collections.singletonList("CRASHID=" + sessionId + "; Path=/")); } else { } sec.getUserProperties().put("CRASHID", sessionId); } }
for (Map.Entry<String, List<String>> e : handshakeRequest.getHeaders().entrySet()) { headers.put(e.getKey(), !e.getValue().isEmpty() ? e.getValue().get(0) : ""); List<String> l = handshakeRequest.getHeaders().get("origin"); if (l == null) { l = handshakeRequest.getHeaders().get("Origin"); List<String> cookieHeaders = handshakeRequest.getHeaders().get("Cookie"); Set<Cookie> cookies = null; if (cookieHeaders != null) { .headers(headers) .cookies(cookies) .session((HttpSession) handshakeRequest.getHttpSession()) .servletPath(servletPath) .contextPath(framework.getServletContext().getContextPath())
private WebSocketContext(ServletContext context, HandshakeRequest request, Map<String, Object> userProperties) { super(context, request.getRequestURI().toString(), request.getQueryString(), request.getHeaders(), request.getParameterMap(), request.getUserPrincipal(), (HttpSession)request.getHttpSession(), // Hopefully these will become a standard, for now they are Jetty specific. (InetSocketAddress)userProperties.get("javax.websocket.endpoint.localAddress"), (InetSocketAddress)userProperties.get("javax.websocket.endpoint.remoteAddress"), WebSocketTransport.retrieveLocales(userProperties), "HTTP/1.1", WebSocketTransport.isSecure(request)); } }
@Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { if (isRaw) { // We have no reliable key (like session id) to save // headers with for raw websocket requests return; } String path = request.getRequestURI().getPath(); Matcher matcher = SESSION_PATTERN.matcher(path); if (matcher.matches()) { String sessionId = matcher.group(1); saveHeaders(sessionId, request.getHeaders()); } } };
@Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { super.modifyHandshake(sec, request, response); sec.getUserProperties().put(WebsocketSecurityInterceptor.SESSION_PRINCIPAL, request.getUserPrincipal()); sec.getUserProperties().put(WebsocketSecurityInterceptor.SESSION_SUBJECT, SecurityContextAssociation.getSubject()); sec.getUserProperties().put(WebsocketSecurityInterceptor.SESSION_CREDENTIAL, SecurityContextAssociation.getPrincipal()); Map<String,List<String>> headers = request.getHeaders(); if (headers != null) { List<String> loginHeader = headers.get(REQUEST_LOGIN_TIME_HEADER); if (loginHeader != null && !loginHeader.isEmpty()) { sec.getUserProperties().put(REQUEST_LOGIN_TIME_HEADER, loginHeader.get(0)); } } } }
/** * Extract path web socket. * * @param request * the request * @return the string */ public static String extractPathWebSocket(HandshakeRequest request) { String requestPath = request.getRequestURI().toString(); if ("".equals(requestPath)) { requestPath = IRepository.SEPARATOR; } return requestPath; }
String channelToken = request.getQueryString(); if (channelToken == null) String uri = request.getRequestURI().toString(); if (uri.indexOf('?') >= 0)
public class SomeCustomConfigurationClass extends ServerEndpointConfig.Configurator { @Override public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) { config.getUserProperties().put("UserPrincipal",request.getUserPrincipal()); config.getUserProperties().put("userInRole", request.isUserInRole("someRole")); } }
public PlatypusPrincipal platypusPrincipal(HandshakeRequest handshake, Session websocketSession) { PlatypusPrincipal principal; if (handshake.getUserPrincipal() != null) { principal = new WebSocketPlatypusPrincipal(handshake.getUserPrincipal().getName(), (String) websocketSession.getUserProperties().get(PlatypusHttpServlet.PLATYPUS_USER_CONTEXT_ATTR_NAME), handshake); } else { principal = new AnonymousPlatypusPrincipal(websocketSession.getId()); } return principal; }
private static String getParameter(HandshakeRequest request, String key) { List<String> values = request.getParameterMap().get(key); if (values != null && values.size() > 0) return values.get(0); return null; }
Object httpSession = request.getHttpSession(); LOG.trace("httpSession: {}", httpSession); if (httpSession != null) Map<String, List<String>> headers = request.getHeaders(); LOG.trace("headers: {}", headers); if (headers != null) Map<String, List<String>> parameterMap = request.getParameterMap(); LOG.trace("parameterMap: {}", parameterMap); if (parameterMap != null) String queryString = request.getQueryString(); LOG.trace("queryString: {}", queryString); if (queryString != null) URI requestURI = request.getRequestURI(); LOG.trace("requestURI: {}", requestURI); if (requestURI != null) Principal userPrincipal = request.getUserPrincipal(); LOG.trace("userPrincipal: {}", userPrincipal); if (userPrincipal != null)
HandshakeResponse response) HttpSession theSession = (HttpSession) request.getHttpSession(); config.getUserProperties().put(HttpSession.class.getName(), theSession); String ID = request.getHeaders().get("x-forwarded-for").get(0);
@Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { if (isRaw) { // We have no reliable key (like session id) to save // headers with for raw websocket requests return; } String path = request.getRequestURI().getPath(); Matcher matcher = SESSION_PATTERN.matcher(path); if (matcher.matches()) { String sessionId = matcher.group(1); saveHeaders(sessionId, request.getHeaders()); } } };
/** * Extract path web socket. * * @param request * the request * @return the string */ public static String extractPathWebSocket(HandshakeRequest request) { String requestPath = request.getRequestURI().toString(); if ("".equals(requestPath)) { requestPath = IRepository.SEPARATOR; } return requestPath; }
public class GetHttpSessionConfigurator extends ServerEndpointConfig.Configurator { @Override public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) { config.getUserProperties().put("UserPrincipal",request.getUserPrincipal()); config.getUserProperties().put("userInRole", request.isUserInRole("someRole")); }}
Object httpSession = request.getHttpSession(); LOG.trace("httpSession: {}", httpSession); if (httpSession != null) Map<String, List<String>> headers = request.getHeaders(); LOG.trace("headers: {}", headers); if (headers != null) Map<String, List<String>> parameterMap = request.getParameterMap(); LOG.trace("parameterMap: {}", parameterMap); if (parameterMap != null) String queryString = request.getQueryString(); LOG.trace("queryString: {}", queryString); if (queryString != null) URI requestURI = request.getRequestURI(); LOG.trace("requestURI: {}", requestURI); if (requestURI != null) Principal userPrincipal = request.getUserPrincipal(); LOG.trace("userPrincipal: {}", userPrincipal); if (userPrincipal != null)
public class ServletAwareConfigurator extends Configurator { @Override public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) { HttpSession httpSession = (HttpSession) request.getHttpSession(); config.getUserProperties().put("httpSession", httpSession); } }
import javax.websocket.HandshakeResponse; import javax.websocket.server.HandshakeRequest; import javax.websocket.server.ServerEndpointConfig; public class MyServerEndpointConfigurator extends ServerEndpointConfig.Configurator { @Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { if (request.getHeaders().containsKey("user-agent")) { sec.getUserProperties().put("user-agent", request.getHeaders().get("user-agent").get(0)); // lower-case! } } }
private static boolean isSecure(HandshakeRequest request) { String scheme = request.getRequestURI().getScheme(); return "https".equalsIgnoreCase(scheme) || "wss".equalsIgnoreCase(scheme); }