Fetching Correct IP Address And Host
Description#
- This doc demonstrates how to get correct IP address and host when
Apache ShenYuserves behindnginxreverse proxy. - After fetched real
IPandhost, you can match them with plugins and selectors.
Default Implementation#
The embedded implementation in
Apache ShenYuis :org.apache.shenyu.web.forward.ForwardedRemoteAddressResolver.You need to config
X-Forwarded-Forinnginxfirst to get correctIP addressandhost.
Implement through a Plugin#
- Declare a new class named
CustomRemoteAddressResolverand implementsorg.apache.shenyu.plugin.api.RemoteAddressResolver.
public interface RemoteAddressResolver {
/** * Resolve inet socket address. * * @param exchange the exchange * @return the inet socket address */ default InetSocketAddress resolve(ServerWebExchange exchange) { return exchange.getRequest().getRemoteAddress(); }
}- Register defined class as a
Spring Bean.
@Bean public SignService customRemoteAddressResolver() { return new CustomRemoteAddressResolver(); }