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

The following examples show how to use org.springframework.http.server.reactive.ServerHttpRequest#getRemoteAddress() . 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: HandshakeWebSocketService.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttpRequest request,
		@Nullable String protocol, Map<String, Object> attributes) {

	URI uri = request.getURI();
	// Copy request headers, as they might be pooled and recycled by
	// the server implementation once the handshake HTTP exchange is done.
	HttpHeaders headers = new HttpHeaders();
	headers.addAll(request.getHeaders());
	Mono<Principal> principal = exchange.getPrincipal();
	String logPrefix = exchange.getLogPrefix();
	InetSocketAddress remoteAddress = request.getRemoteAddress();
	return new HandshakeInfo(uri, headers, principal, protocol, remoteAddress, attributes, logPrefix);
}
 
Example 2
Source File: HandshakeWebSocketService.java    From java-technology-stack with MIT License 5 votes vote down vote up
private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttpRequest request,
		@Nullable String protocol, Map<String, Object> attributes) {

	URI uri = request.getURI();
	// Copy request headers, as they might be pooled and recycled by
	// the server implementation once the handshake HTTP exchange is done.
	HttpHeaders headers = new HttpHeaders();
	headers.addAll(request.getHeaders());
	Mono<Principal> principal = exchange.getPrincipal();
	String logPrefix = exchange.getLogPrefix();
	InetSocketAddress remoteAddress = request.getRemoteAddress();
	return new HandshakeInfo(uri, headers, principal, protocol, remoteAddress, attributes, logPrefix);
}
 
Example 3
Source File: XForwardedHeadersFilter.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Override
public HttpHeaders filter(HttpHeaders input, ServerWebExchange exchange) {

	ServerHttpRequest request = exchange.getRequest();
	HttpHeaders original = input;
	HttpHeaders updated = new HttpHeaders();

	original.entrySet().stream()
			.forEach(entry -> updated.addAll(entry.getKey(), entry.getValue()));

	if (isForEnabled() && request.getRemoteAddress() != null
			&& request.getRemoteAddress().getAddress() != null) {
		String remoteAddr = request.getRemoteAddress().getAddress().getHostAddress();
		List<String> xforwarded = original.get(X_FORWARDED_FOR_HEADER);
		// prevent duplicates
		if (remoteAddr != null
				&& (xforwarded == null || !xforwarded.contains(remoteAddr))) {
			write(updated, X_FORWARDED_FOR_HEADER, remoteAddr, isForAppend());
		}
	}

	String proto = request.getURI().getScheme();
	if (isProtoEnabled()) {
		write(updated, X_FORWARDED_PROTO_HEADER, proto, isProtoAppend());
	}

	if (isPrefixEnabled()) {
		// If the path of the url that the gw is routing to is a subset
		// (and ending part) of the url that it is routing from then the difference
		// is the prefix e.g. if request original.com/prefix/get/ is routed
		// to routedservice:8090/get then /prefix is the prefix
		// - see XForwardedHeadersFilterTests, so first get uris, then extract paths
		// and remove one from another if it's the ending part.

		LinkedHashSet<URI> originalUris = exchange
				.getAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
		URI requestUri = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR);

		if (originalUris != null && requestUri != null) {

			originalUris.stream().forEach(originalUri -> {

				if (originalUri != null && originalUri.getPath() != null) {
					String prefix = originalUri.getPath();

					// strip trailing slashes before checking if request path is end
					// of original path
					String originalUriPath = stripTrailingSlash(originalUri);
					String requestUriPath = stripTrailingSlash(requestUri);

					updateRequest(updated, originalUri, originalUriPath,
							requestUriPath);

				}
			});
		}
	}

	if (isPortEnabled()) {
		String port = String.valueOf(request.getURI().getPort());
		if (request.getURI().getPort() < 0) {
			port = String.valueOf(getDefaultPort(proto));
		}
		write(updated, X_FORWARDED_PORT_HEADER, port, isPortAppend());
	}

	if (isHostEnabled()) {
		String host = toHostHeader(request);
		write(updated, X_FORWARDED_HOST_HEADER, host, isHostAppend());
	}

	return updated;
}
 
Example 4
Source File: ForwardedHeadersFilter.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Override
public HttpHeaders filter(HttpHeaders input, ServerWebExchange exchange) {
	ServerHttpRequest request = exchange.getRequest();
	HttpHeaders original = input;
	HttpHeaders updated = new HttpHeaders();

	// copy all headers except Forwarded
	original.entrySet().stream().filter(
			entry -> !entry.getKey().toLowerCase().equalsIgnoreCase(FORWARDED_HEADER))
			.forEach(entry -> updated.addAll(entry.getKey(), entry.getValue()));

	List<Forwarded> forwardeds = parse(original.get(FORWARDED_HEADER));

	for (Forwarded f : forwardeds) {
		updated.add(FORWARDED_HEADER, f.toHeaderValue());
	}

	// TODO: add new forwarded
	URI uri = request.getURI();
	String host = original.getFirst(HttpHeaders.HOST);
	Forwarded forwarded = new Forwarded().put("host", host).put("proto",
			uri.getScheme());

	InetSocketAddress remoteAddress = request.getRemoteAddress();
	if (remoteAddress != null) {
		// If remoteAddress is unresolved, calling getHostAddress() would cause a
		// NullPointerException.
		String forValue = remoteAddress.isUnresolved() ? remoteAddress.getHostName()
				: remoteAddress.getAddress().getHostAddress();
		int port = remoteAddress.getPort();
		if (port >= 0) {
			forValue = forValue + ":" + port;
		}
		forwarded.put("for", forValue);
	}
	// TODO: support by?

	updated.add(FORWARDED_HEADER, forwarded.toHeaderValue());

	return updated;
}