Java Code Examples for org.springframework.http.server.ServerHttpRequest#getPrincipal()

The following examples show how to use org.springframework.http.server.ServerHttpRequest#getPrincipal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NettyRequestUpgradeStrategy.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
@Override
protected void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response, String selectedProtocol,
                               List<Extension> selectedExtensions, Endpoint endpoint) throws HandshakeFailureException {
    HttpServletRequest servletRequest = getHttpServletRequest(request);
    ServletHttpServletRequest httpServletRequest = ServletUtil.unWrapper(servletRequest);
    if(httpServletRequest == null) {
        throw new HandshakeFailureException(
                "Servlet request failed to upgrade to WebSocket: " + servletRequest.getRequestURL());
    }

    WebSocketServerContainer serverContainer = getContainer(servletRequest);
    Principal principal = request.getPrincipal();
    Map<String, String> pathParams = new LinkedHashMap<>(3);

    ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(servletRequest.getRequestURI(), endpoint);
    endpointConfig.setSubprotocols(Arrays.asList(WebSocketServerHandshaker.SUB_PROTOCOL_WILDCARD,selectedProtocol));
    if(selectedExtensions != null) {
        endpointConfig.setExtensions(selectedExtensions);
    }

    try {
        handshakeToWebsocket(httpServletRequest, selectedProtocol, maxFramePayloadLength, principal,
                selectedExtensions, pathParams, endpoint,
                endpointConfig, serverContainer);
    } catch (Exception e) {
        throw new HandshakeFailureException(
                "Servlet request failed to upgrade to WebSocket: " + servletRequest.getRequestURL(), e);
    }
}
 
Example 2
Source File: AbstractHttpSockJsSession.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Handle the first request for receiving messages on a SockJS HTTP transport
 * based session.
 * <p>Long polling-based transports (e.g. "xhr", "jsonp") complete the request
 * after writing the open frame. Streaming-based transports ("xhr_streaming",
 * "eventsource", and "htmlfile") leave the response open longer for further
 * streaming of message frames but will also close it eventually after some
 * amount of data has been sent.
 * @param request the current request
 * @param response the current response
 * @param frameFormat the transport-specific SocksJS frame format to use
 */
public void handleInitialRequest(ServerHttpRequest request, ServerHttpResponse response,
		SockJsFrameFormat frameFormat) throws SockJsException {

	this.uri = request.getURI();
	this.handshakeHeaders = request.getHeaders();
	this.principal = request.getPrincipal();
	this.localAddress = request.getLocalAddress();
	this.remoteAddress = request.getRemoteAddress();

	synchronized (this.responseLock) {
		try {
			this.response = response;
			this.frameFormat = frameFormat;
			this.asyncRequestControl = request.getAsyncRequestControl(response);
			this.asyncRequestControl.start(-1);

			disableShallowEtagHeaderFilter(request);

			// Let "our" handler know before sending the open frame to the remote handler
			delegateConnectionEstablished();

			handleRequestInternal(request, response, true);

			// Request might have been reset (e.g. polling sessions do after writing)
			this.readyToSend = isActive();
		}
		catch (Throwable ex) {
			tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
			throw new SockJsTransportFailureException("Failed to open session", getId(), ex);
		}
	}
}
 
Example 3
Source File: AbstractHandshakeHandler.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * A method that can be used to associate a user with the WebSocket session
 * in the process of being established. The default implementation calls
 * {@link ServerHttpRequest#getPrincipal()}
 * <p>Subclasses can provide custom logic for associating a user with a session,
 * for example for assigning a name to anonymous users (i.e. not fully authenticated).
 * @param request the handshake request
 * @param wsHandler the WebSocket handler that will handle messages
 * @param attributes handshake attributes to pass to the WebSocket session
 * @return the user for the WebSocket session, or {@code null} if not available
 */
@Nullable
protected Principal determineUser(
		ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {

	return request.getPrincipal();
}
 
Example 4
Source File: AbstractHandshakeHandler.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * A method that can be used to associate a user with the WebSocket session
 * in the process of being established. The default implementation calls
 * {@link ServerHttpRequest#getPrincipal()}
 * <p>Subclasses can provide custom logic for associating a user with a session,
 * for example for assigning a name to anonymous users (i.e. not fully authenticated).
 * @param request the handshake request
 * @param wsHandler the WebSocket handler that will handle messages
 * @param attributes handshake attributes to pass to the WebSocket session
 * @return the user for the WebSocket session, or {@code null} if not available
 */
@Nullable
protected Principal determineUser(
		ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {

	return request.getPrincipal();
}
 
Example 5
Source File: AbstractHandshakeHandler.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * A method that can be used to associate a user with the WebSocket session
 * in the process of being established. The default implementation calls
 * {@link ServerHttpRequest#getPrincipal()}
 * <p>Subclasses can provide custom logic for associating a user with a session,
 * for example for assigning a name to anonymous users (i.e. not fully authenticated).
 * @param request the handshake request
 * @param wsHandler the WebSocket handler that will handle messages
 * @param attributes handshake attributes to pass to the WebSocket session
 * @return the user for the WebSocket session, or {@code null} if not available
 */
protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler,
		Map<String, Object> attributes) {

	return request.getPrincipal();
}