001/* 002 * Copyright 2002-2019 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.filter.reactive; 018 019import reactor.core.publisher.Mono; 020 021import org.springframework.http.server.reactive.ServerHttpRequest; 022import org.springframework.web.server.ServerWebExchange; 023import org.springframework.web.server.WebFilter; 024import org.springframework.web.server.WebFilterChain; 025import org.springframework.web.server.adapter.ForwardedHeaderTransformer; 026 027/** 028 * Extract values from "Forwarded" and "X-Forwarded-*" headers to override the 029 * request URI (i.e. {@link ServerHttpRequest#getURI()}) so it reflects the 030 * client-originated protocol and address. 031 * 032 * <p>Alternatively if {@link #setRemoveOnly removeOnly} is set to "true", then 033 * "Forwarded" and "X-Forwarded-*" headers are only removed and not used. 034 * 035 * @author Arjen Poutsma 036 * @author Rossen Stoyanchev 037 * @deprecated as of 5.1 this filter is deprecated in favor of using 038 * {@link ForwardedHeaderTransformer} which can be declared as a bean with the 039 * name "forwardedHeaderTransformer" or registered explicitly in 040 * {@link org.springframework.web.server.adapter.WebHttpHandlerBuilder 041 * WebHttpHandlerBuilder}. 042 * @since 5.0 043 * @see <a href="https://tools.ietf.org/html/rfc7239">https://tools.ietf.org/html/rfc7239</a> 044 */ 045@Deprecated 046public class ForwardedHeaderFilter extends ForwardedHeaderTransformer implements WebFilter { 047 048 @Override 049 public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { 050 ServerHttpRequest request = exchange.getRequest(); 051 if (hasForwardedHeaders(request)) { 052 exchange = exchange.mutate().request(apply(request)).build(); 053 } 054 return chain.filter(exchange); 055 } 056 057}