[英]Spring Security SAML, Redirects going to HTTP instead of HTTPS when using SAMLContextProviderLB set to HTTPs scheme
為 SAMLContextProviderLB bean 附加了我的上下文提供程序
**<property name="scheme" value="https"/>**
<property name="serverName" value="${sp.hostname}"/>
<property name="serverPort" value="#{'${sp.ssl.port}'=='' ? 443 : '${sp.ssl.port}'}"/>
<property name="includeServerPortInRequestURL" value="#{'${sp.ssl.port}'=='443' ? false : true }"/>
<property name="contextPath" value="/${sp.context.root}"/>
我在反向代理后面,所以我正在卸載 SSL 終止。 后端服務器本身正在偵聽非 SSL,但 webtier 正在為我們終止 SSL 並轉發到非 SSL 端口。 我已經使用上述屬性設置了 SAMLContextProviderLB,這樣即使后端是 https,它也會知道將 saml 令牌的預期收件人映射為 https 受眾。 然而,我在下面的日志中看到的是,當我訪問受保護的資源時,它會在瀏覽器上返回垃圾。 當我在瀏覽器中將其更改為 https 時,它會按預期工作。 查看下面的日志顯示從 DefaultSavedRequest url 返回的值是 HTTP,而它應該是 HTTPs。
2016-03-07 18:24:11,907 信息 org.springframework.security.saml.log.SAMLDefaultLogger.log:127 - AuthNResponse;成功;10.4.203.88; https://myserver:89/fct;https://www.myADFS.com/adfs/services/trust;camachof@email.com ;;
2016-03-07 18:24:11,909 調試 org.springframework.security.saml.SAMLProcessingFilter.successfulAuthentication:317 - 認證成功。 更新 SecurityContextHolder 以包含:org.springframework.security.providers.ExpiringUsernameAuthenticationToken@830e9237:Principal:camachof@email.com; 憑據:[受保護]; 已認證:真實; 詳細信息:空; 未授予任何權限
2016-03-07 18:24:11,910 調試 org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler.onAuthenticationSuccess:79 - 重定向到DefaultSavedRequest Url: http ://myserver:89/fct/page
2016-03-07 18:24:11,911 調試 org.springframework.security.web.DefaultRedirectStrategy.sendRedirect:36 - 重定向到“ http://myserver:89/fct/page ”
2016-03-07 18:24:11,911 調試 org.springframework.security.web.context.HttpSessionSecurityContextRepository.saveContext:292 - SecurityContext 存儲到 HttpSession: 'org.springframework.security.core.context.SecurityContextImpl@830e9237: 身份驗證: .springframework.security.providers.ExpiringUsernameAuthenticationToken@830e9237:委托人:camachof@email.com; 憑據:[受保護]; 已認證:真實; 詳細信息:空; 未授予任何權限
2016-03-07 18:24:11,912 DEBUG org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter:97 - SecurityContextHolder 現在被清除,因為請求處理完成
任何想法如何在此設置下將其強制為 HTTPS? 提前致謝。
這個問題很舊,但如果我發現它,其他人可能會因此我會發布答案。
您的負載均衡器或反向代理( Apache httpd
或nginx
)必須為您做一些額外的工作。 Spring
(或Spring Boot
)和嵌入式Tomcat
(或Jetty
)認為它們正在運行一個 http 服務器。 它正在做什么。 如果代理傳遞了一些頭變量,Tomcat 就會開始認為它正在運行https
。
以下是 Apache 需要的示例:
ProxyPreserveHost On
RequestHeader add X-Forwarded-Proto https
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
ProxyPass
和ProxyPassReverse
是您可能已經擁有的。 但是ProxyPreserveHost
和X-Forwarded-Proto
真的很重要。
查看Spring Boot 文檔的這一部分。 如果設置了X-Forwarded-For
或X-Forwarded-Proto
則需要將其添加到 application.properties 文件中:
server.use-forward-headers=true
和/或
server.forward-headers-strategy=native
取決於您使用的 Spring Boot 版本。
您還將在該文檔中看到您可以為 Tomcat 特定配置添加這些屬性:
server.tomcat.remote-ip-header=x-your-remote-ip-header
server.tomcat.protocol-header=x-your-protocol-header
除了上面的操作之外,執行所有這些操作,它就會開始工作。 以上內容本身不足以強制 Tomcat 開始使用https
轉發請求。
我發現因為我的公司有一個基於硬件的負載平衡器(由 Rackspace 管理),所以很難對其進行配置以進行這些更改。 因此,我們在防火牆/負載平衡器中執行 SSL 終止,然后將請求轉發到端口 80 上的 Apache,然后 Apache 將它們轉發到端口 8080 上的 Java。是的,這是一團糟。 但它讓所有這些廢話起作用了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.