org.springframework.http.server.ServerHttpRequest#getPrincipal ( )源码实例Demo

下面列出了org.springframework.http.server.ServerHttpRequest#getPrincipal ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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);
    }
}
 
/**
 * 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);
		}
	}
}
 
/**
 * 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();
}
 
/**
 * 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();
}
 
/**
 * 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();
}