简体   繁体   English

Spring Security SAML,在使用 SAMLContextProviderLB 设置为 HTTPs 方案时重定向到 HTTP 而不是 HTTPS

[英]Spring Security SAML, Redirects going to HTTP instead of HTTPS when using SAMLContextProviderLB set to HTTPs scheme

Attached my context provider for SAMLContextProviderLB bean为 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}"/>

I'm behind a reverse Proxy so I'm offloading the SSL Termination.我在反向代理后面,所以我正在卸载 SSL 终止。 the back-end server itself is listening on non-SSL but the webtier is terminating the SSL for us and forwarding to the non-ssl port.后端服务器本身正在侦听非 SSL,但 webtier 正在为我们终止 SSL 并转发到非 SSL 端口。 I've set up SAMLContextProviderLB with the above properties so that even tho the backend is https, it will know to map the intended recipient for the saml token as the https audience.我已经使用上述属性设置了 SAMLContextProviderLB,这样即使后端是 https,它也会知道将 saml 令牌的预期收件人映射为 https 受众。 What I'm seeing in the logs below however, when I go to a protected resource, its returning garbage on the browser.然而,我在下面的日志中看到的是,当我访问受保护的资源时,它会在浏览器上返回垃圾。 When I change it to https in the browser, it works as intended.当我在浏览器中将其更改为 https 时,它会按预期工作。 Seeing the logs below shows that the value being return from DefaultSavedRequest url is HTTP when it should be HTTPs.查看下面的日志显示从 DefaultSavedRequest url 返回的值是 HTTP,而它应该是 HTTPs。

2016-03-07 18:24:11,907 INFO org.springframework.security.saml.log.SAMLDefaultLogger.log:127 - AuthNResponse;SUCCESS;10.4.203.88; 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 ;; https://myserver:89/fct;https://www.myADFS.com/adfs/services/trust;camachof@email.com ;;

2016-03-07 18:24:11,909 DEBUG org.springframework.security.saml.SAMLProcessingFilter.successfulAuthentication:317 - Authentication success. 2016-03-07 18:24:11,909 调试 org.springframework.security.saml.SAMLProcessingFilter.successfulAuthentication:317 - 认证成功。 Updating SecurityContextHolder to contain: org.springframework.security.providers.ExpiringUsernameAuthenticationToken@830e9237: Principal: camachof@email.com;更新 SecurityContextHolder 以包含:org.springframework.security.providers.ExpiringUsernameAuthenticationToken@830e9237:Principal:camachof@email.com; Credentials: [PROTECTED];凭据:[受保护]; Authenticated: true;已认证:真实; Details: null;详细信息:空; Not granted any authorities未授予任何权限

2016-03-07 18:24:11,910 DEBUG org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler.onAuthenticationSuccess:79 - Redirecting to DefaultSavedRequest Url: http ://myserver:89/fct/page 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 DEBUG org.springframework.security.web.DefaultRedirectStrategy.sendRedirect:36 - Redirecting to ' 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 DEBUG org.springframework.security.web.context.HttpSessionSecurityContextRepository.saveContext:292 - SecurityContext stored to HttpSession: 'org.springframework.security.core.context.SecurityContextImpl@830e9237: Authentication: org.springframework.security.providers.ExpiringUsernameAuthenticationToken@830e9237: Principal: camachof@email.com; 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; Credentials: [PROTECTED];凭据:[受保护]; Authenticated: true;已认证:真实; Details: null;详细信息:空; Not granted any authorities'未授予任何权限

2016-03-07 18:24:11,912 DEBUG org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter:97 - SecurityContextHolder now cleared, as request processing completed 2016-03-07 18:24:11,912 DEBUG org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter:97 - SecurityContextHolder 现在被清除,因为请求处理完成

Any ideas how to force this to HTTPS under this set up?任何想法如何在此设置下将其强制为 HTTPS? Thanks in advance.提前致谢。

This question is old but if I found it someone else might so I will post an answer.这个问题很旧,但如果我发现它,其他人可能会因此我会发布答案。

Either your load balancer or your Reverse Proxy ( Apache httpd or nginx ) has to do some extra work for you.您的负载均衡器或反向代理( Apache httpdnginx )必须为您做一些额外的工作。 Spring (or Spring Boot ) and the embedded Tomcat (or Jetty ) think they are running an http server. Spring (或Spring Boot )和嵌入式Tomcat (或Jetty )认为它们正在运行一个 http 服务器。 Which it is doing.它正在做什么。 If the proxy passes some header variables, Tomcat will start to think that it is running https .如果代理传递了一些头变量,Tomcat 就会开始认为它正在运行https

Here is what Apache needs as an example:以下是 Apache 需要的示例:

ProxyPreserveHost On
RequestHeader add X-Forwarded-Proto https
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/

ProxyPass and ProxyPassReverse are what you probably already have. ProxyPassProxyPassReverse是您可能已经拥有的。 But the ProxyPreserveHost and the X-Forwarded-Proto really count.但是ProxyPreserveHostX-Forwarded-Proto真的很重要。

Check out this section of the Spring Boot Docs .查看Spring Boot 文档的这一部分。 If X-Forwarded-For or X-Forwarded-Proto are set, you need to add this to your application.properties file:如果设置了X-Forwarded-ForX-Forwarded-Proto则需要将其添加到 application.properties 文件中:

server.use-forward-headers=true

and/or和/或

server.forward-headers-strategy=native

depending on what version of spring boot you use.取决于您使用的 Spring Boot 版本。

You will also see in that document that you can add these properties for Tomcat specific configuration:您还将在该文档中看到您可以为 Tomcat 特定配置添加这些属性:

server.tomcat.remote-ip-header=x-your-remote-ip-header
server.tomcat.protocol-header=x-your-protocol-header

Do all of this in addition to what you have above and it will start working.除了上面的操作之外,执行所有这些操作,它就会开始工作。 What you have above, by itself, is not enough to force Tomcat to start forwarding requests with https .以上内容本身不足以强制 Tomcat 开始使用https转发请求。

I found that because my company had a hardware-based load balancer (that is managed by Rackspace), that it was difficult to configure it to make these changes.我发现因为我的公司有一个基于硬件的负载平衡器(由 Rackspace 管理),所以很难对其进行配置以进行这些更改。 So we do SSL termination in the firewall/load balancer, then forward the requests to Apache on port 80, and Apache forwards them on to Java on port 8080. Yes, it is a mess.因此,我们在防火墙/负载平衡器中执行 SSL 终止,然后将请求转发到端口 80 上的 Apache,然后 Apache 将它们转发到端口 8080 上的 Java。是的,这是一团糟。 But it got all this nonsense working.但它让所有这些废话起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM