【发布时间】:2018-06-22 05:04:40
【问题描述】:
我们一直在使用 Apache 2.2 反向代理让多个应用程序在同一个 VM 上运行。在我们添加 Keycloak 适配器 3.4.0.Final + spring security 1.5.9.RELEASE 之前,一切正常。
这就是它的工作原理:
VM1 应用程序1 应用程序2
虚拟机2 钥匙斗篷
同一网络上的笔记本电脑 浏览器 app1 - 用于开发目的
场景: 1)笔记本电脑之间一切正常 - 带有应用程序的VM2。 2)浏览器笔记本电脑-VM1-VM2之间一切正常,当没有反向代理时(所以直接访问应用程序端口)。 3) 反向代理到位时的问题。浏览器笔记本电脑 - VM1(带有 mod_prox 的 Apache) - VM2
我遵循了文档中的所有建议:
http://www.keycloak.org/docs/1.9/server_installation_guide/topics/clustering/load-balancer.html
这是我以前的规则:
LoadModule proxy_module modules/mod_proxy.so
ProxyRequests On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPassMatch ^/MyAPP/(.+) http://localhost:8585/MyAPP/$1
ProxyPassReverse ^/MyAPP/(.+) http://localhost:8585/MyAPP/$1
ProxyPreserveHost On
观察到: GET VM1/MyAPP/index.html -> 使用正确的重定向 url 重定向到 VM2 keycloak GET VM1/MyAPP/index.html?State=xxxx -> 重定向到 / GET / -> root 上没有任何东西,所以它会在这里结束
第一个变化: 我已经看到造成这种情况的原因是 keycloak 成功处理程序将我重定向到 root。 所以我将其更改为将其重定向到同一页面:
public FilterRegistrationBean keycloakAuthenticationProcessingFilterRegistrationBean( KeycloakAuthenticationProcessingFilter filter){
FilterRegistrationBean registrationBean = new
FilterRegistrationBean(filter);
successHandler = new AuthenticationSuccessHandler();
successHandler.setDefaultTargetUrl("/MyAPP/index.html");
filter.setAuthenticationSuccessHandler(successHandler);
}
观察到:
GET VM1/MyAPP/index.html -> redirected to VM2 keycloak with the right redirect url
GET VM1/MyAPP/index.html?State=xxxx -> redirected to /MyAPP/index.html
GET VM1/MyAPP/index.html?State=xxxx -> redirected to /MyAPP/index.html
GET VM1/MyAPP/index.html?State=xxxx -> redirected to /MyAPP/index.html
GET VM1/MyAPP/index.html?State=xxxx -> redirected to /MyAPP/index.html
GET VM1/MyAPP/index.html?State=xxxx -> redirected to /MyAPP/index.html
GET VM1/MyAPP/index.html?State=xxxx -> redirected to /MyAPP/index.html
....
无限循环。我还注意到应用程序每次都在进行身份验证。 可疑的。
所以我开始怀疑是 apache 导致了问题,并决定尝试使用不同的代理: 代理服务器。 它因同样的问题而失败。 但我被重定向到 /sso/login intead
所以下一步是调试不同的请求:代理与非代理
VM1/MyAPP vs VM1:8585/MyAPP
所以我发现每次请求具有授权标头时它都会对请求进行身份验证。这只发生在代理版本中。
因此,使用 apache 2.2,您无法删除标头。刚刚添加到 2.4 所以再次使用haproxy,并以相同的方式尝试强制删除授权标头。它几乎奏效了。我遇到了一些问题,请求方法是 1 而不是 GET。奇怪...
长话短说现在很明显,基本身份验证是问题所在。
如何解决这个问题?
更新:
我现在的问题是: 如何更改 KeycloakAuthenticationEntryPoint loginUri 以在配置中包含我的反向代理的子上下文? - 在 root 上拥有反向代理“sso/login”不会让我在同一服务器上拥有超过 1 个 keycloak 应用程序。
【问题讨论】:
标签: apache spring-security reverse-proxy keycloak proxypass