为了进行安全检查,我需要访问资源服务中用户的远程 IP 地址。这个资源服务是一个简单的最近的 Spring Boot 应用程序,它在我的 Eureka 服务器上注册了自己:
@SpringBootApplication
@EnableEurekaClient
public class ServletInitializer extends SpringBootServletInitializer {
public static void main(final String[] args) {
SpringApplication.run(ServletInitializer.class, args);
}
}
在我的 Eureka 服务器上注册的所有服务都通过我基于 Angel.SR3 的 Zuul 路由代理服务器动态路由,starter-zuul
并且starter-eureka
:
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class RoutingProxyServer {
public static void main(final String[] args) {
SpringApplication.run(RoutingProxyServer.class, args);
}
}
Zuul 路由代理服务器还为下一步配置了一个 AJP 连接器:
@Configuration
@ConditionalOnProperty("ajp.port")
public class TomcatAjpConfig extends TomcatWebSocketContainerCustomizer {
@Value("${ajp.port}")
private int port;
@Override
public void doCustomize(final TomcatEmbeddedServletContainerFactory tomcat) {
super.doCustomize(tomcat);
// Listen for AJP requests
Connector ajp = new Connector("AJP/1.3");
ajp.setPort(port);
tomcat.addAdditionalTomcatConnectors(ajp);
}
}
对动态路由 zuul 代理的所有请求都通过 Apache 自己代理,以在标准 443 端口上提供 HTTPS:
# Preserve Host when proxying so jar apps return working URLs in JSON responses
RequestHeader set X-Forwarded-Proto "https"
ProxyPreserveHost On
# Redirect remaining traffic to routing proxy server
ProxyPass / ajp://192.168.x.x:8009/
# Also update Location, Content-Location and URI headers on HTTP redirect responses
ProxyPassReverse / ajp://192.168.x.x:8009/
有了这一切,资源服务就可用了,但不幸的是,我从 Spring Security 获得的 remoteAddress 是 Zuul 代理/Apache 服务器的地址,而不是远程客户端 IP 地址。
过去,我使用了一个org.springframework.security.authentication.AuthenticationDetailsSource
优先于X-Forwarded-For
正常值的标头值remoteAddress
来获取正确的 IP 地址,但是当通过两个代理(Apache + Zuul)时,我无法弄清楚如何将正确的远程 IP 地址传递给我的资源服务.
谁能帮我访问这两个代理背后的正确远程 IP 地址,或者建议一种替代方法来让它工作?