【发布时间】:2015-11-22 08:36:00
【问题描述】:
为了进行安全检查,我需要在我的资源服务中访问用户的远程 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 starter-zuul 和 starter-eureka 的 Zuul 路由代理服务器动态路由的:
@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 比普通的remoteAddress 更喜欢X-Forwarded-For 标头值来获取正确的IP 地址,但我不知道如何将正确的远程IP 地址传递给我的通过两个代理(Apache + Zuul)时的资源服务。
谁能帮我访问这两个代理背后的正确远程 IP 地址,或者建议一种替代方法来让它工作?
【问题讨论】:
标签: apache spring-security spring-cloud netflix-zuul