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

The following examples show how to use org.springframework.http.server.reactive.ServerHttpRequest#getURI() . 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: CorsUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Check if the request is a same-origin one, based on {@code Origin}, and
 * {@code Host} headers.
 *
 * <p><strong>Note:</strong> as of 5.1 this method ignores
 * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the
 * client-originated address. Consider using the {@code ForwardedHeaderFilter}
 * to extract and use, or to discard such headers.
 *
 * @return {@code true} if the request is a same-origin one, {@code false} in case
 * of a cross-origin request
 * @deprecated as of 5.2, same-origin checks are performed directly by {@link #isCorsRequest}
 */
@Deprecated
public static boolean isSameOrigin(ServerHttpRequest request) {
	String origin = request.getHeaders().getOrigin();
	if (origin == null) {
		return true;
	}

	URI uri = request.getURI();
	String actualScheme = uri.getScheme();
	String actualHost = uri.getHost();
	int actualPort = getPort(uri.getScheme(), uri.getPort());
	Assert.notNull(actualScheme, "Actual request scheme must not be null");
	Assert.notNull(actualHost, "Actual request host must not be null");
	Assert.isTrue(actualPort != -1, "Actual request port must not be undefined");

	UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
	return (actualScheme.equals(originUrl.getScheme()) &&
			actualHost.equals(originUrl.getHost()) &&
			actualPort == getPort(originUrl.getScheme(), originUrl.getPort()));
}
 
Example 2
Source File: ValidateCodeFilter.java    From spring-microservice-exam with MIT License 6 votes vote down vote up
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    // 当前请求
    ServerHttpRequest request = exchange.getRequest();
    // 请求的URI
    URI uri = request.getURI();
    if (HttpMethod.POST.matches(request.getMethodValue())
            && StrUtil.containsAnyIgnoreCase(uri.getPath(), GatewayConstant.OAUTH_TOKEN_URL, GatewayConstant.REGISTER, GatewayConstant.MOBILE_TOKEN_URL)) {
        String grantType = request.getQueryParams().getFirst(GatewayConstant.GRANT_TYPE);
        // 授权类型为密码模式、手机号、注册才校验验证码
        if (CommonConstant.GRANT_TYPE_PASSWORD.equals(grantType) || CommonConstant.GRANT_TYPE_MOBILE.equals(grantType) || StrUtil.containsAnyIgnoreCase(uri.getPath(), GatewayConstant.REGISTER)) {
            // 校验验证码
            checkCode(request, getLoginType(grantType));
        }
    }
    return chain.filter(exchange);
}
 
Example 3
Source File: CorsUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Check if the request is a same-origin one, based on {@code Origin}, and
 * {@code Host} headers.
 *
 * <p><strong>Note:</strong> as of 5.1 this method ignores
 * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the
 * client-originated address. Consider using the {@code ForwardedHeaderFilter}
 * to extract and use, or to discard such headers.
 *
 * @return {@code true} if the request is a same-origin one, {@code false} in case
 * of a cross-origin request
 */
public static boolean isSameOrigin(ServerHttpRequest request) {
	String origin = request.getHeaders().getOrigin();
	if (origin == null) {
		return true;
	}

	URI uri = request.getURI();
	String actualScheme = uri.getScheme();
	String actualHost = uri.getHost();
	int actualPort = getPort(uri.getScheme(), uri.getPort());
	Assert.notNull(actualScheme, "Actual request scheme must not be null");
	Assert.notNull(actualHost, "Actual request host must not be null");
	Assert.isTrue(actualPort != -1, "Actual request port must not be undefined");

	UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
	return (actualScheme.equals(originUrl.getScheme()) &&
			actualHost.equals(originUrl.getHost()) &&
			actualPort == getPort(originUrl.getScheme(), originUrl.getPort()));
}
 
Example 4
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 5
Source File: DecodePasswordFilter.java    From spring-microservice-exam with MIT License 5 votes vote down vote up
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    // 当前请求
    ServerHttpRequest request = exchange.getRequest();
    // 请求的URI
    URI uri = request.getURI();
    // 获取token的请求
    if (HttpMethod.POST.matches(request.getMethodValue()) && StrUtil.containsAnyIgnoreCase(uri.getPath(), GatewayConstant.OAUTH_TOKEN_URL, GatewayConstant.REGISTER,
            GatewayConstant.MOBILE_TOKEN_URL)) {
        String grantType = request.getQueryParams().getFirst(GatewayConstant.GRANT_TYPE);
        // 授权类型为密码模式则解密
        if (CommonConstant.GRANT_TYPE_PASSWORD.equals(grantType) || StrUtil.containsAnyIgnoreCase(uri.getPath(), GatewayConstant.REGISTER)) {
            String credential = request.getQueryParams().getFirst(CREDENTIAL);
            if (StringUtils.isNotBlank(credential)) {
                try {
                    // 开始解密
                    credential = AesUtil.decryptAES(credential, sysProperties.getKey());
                    credential = credential.trim();
                } catch (Exception e) {
                    log.error("credential decrypt fail:{}", credential);
                }
                URI newUri = UriComponentsBuilder.fromUri(uri)
                        // 替换password字段
                        .replaceQueryParam(PASSWORD, credential)
                        // 替换credential字段
                        .replaceQueryParam(CREDENTIAL, credential)
                        .build(true).toUri();
                request = request.mutate().uri(newUri).build();
                return chain.filter(exchange.mutate().request(request).build());
            }
        }
    }
    return chain.filter(exchange);
}
 
Example 6
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 7
Source File: HttpEntityMethodArgumentResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private Object createEntity(@Nullable Object body, Class<?> entityType, ServerHttpRequest request) {
	return (RequestEntity.class.equals(entityType) ?
			new RequestEntity<>(body, request.getHeaders(), request.getMethod(), request.getURI()) :
			new HttpEntity<>(body, request.getHeaders()));
}
 
Example 8
Source File: HttpEntityArgumentResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
private Object createEntity(@Nullable Object body, Class<?> entityType, ServerHttpRequest request) {
	return (RequestEntity.class.equals(entityType) ?
			new RequestEntity<>(body, request.getHeaders(), request.getMethod(), request.getURI()) :
			new HttpEntity<>(body, request.getHeaders()));
}
 
Example 9
Source File: HttpRequestMapper.java    From charon-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
URI extractUri(ServerHttpRequest request) {
    return request.getURI();
}
 
Example 10
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;
}