2016-03-07 326 views
5

附上了我的上下文提供的SAMLContextProviderLB豆Spring Security的SAML,重定向的页面,而不是去到HTTP HTTPS的使用SAMLContextProviderLB设置为https配置

**<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,它应该是HTTP。

2016-03-07 18:24:11,907 INFO org.springframework.security.saml.log.SAMLDefaultLogger.log:127 - AuthNResponse; SUCCESS; 10.4.203.88; https://myserver:89/fct;https://www.myADFS.com/adfs/services/trust;[email protected] ;;

2016-03-07 18:24:11,909 DEBUG org.springframework.security.saml.SAMLProcessingFilter.successfulAuthentication:317 - 认证成功。更新SecurityContextHolder以包含:org.springf[email protected]830e9237:Principal:[email protected];证书:[PROTECTED];已验证:true;详细信息:null;未授予任何机关

2016年3月7日18:24:11,910 DEBUG org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler.onAuthenticationSuccess:79 - 重定向到DefaultSavedRequest网址:HTTP:// MYSERVER: 89/FCT /页

2016年3月7日18:24:11911 DEBUG org.springframework.security.web.DefaultRedirectStrategy.sendRedirect:36 - 重定向到 'http://myserver:89/fct/page'

2016年3月7日18: 24:11,911 DEBUG org.springframework.security.web.context.HttpSessionSecurityContextRepository.saveCon文本:292 - SecurityContext存储到HttpSession:'[email protected]0e9237:身份验证:org.springf[email protected]830e9237:主体:[email protected];证书:[PROTECTED];已验证:true;详细信息:null;未授予任何当局

2016年3月7日18:24:11912 DEBUG org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter:97 - SecurityContextHolder中已被清除,已完成

任何想法,请求处理如何在此设置下强制使用HTTPS?提前致谢。

回答

3

这个问题是旧的,但如果我发现它的人可能会这样我会发表一个答案。

您的负载平衡器或您的反向代理(Apache httpdnginx)必须为您做一些额外的工作。 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/ 

ProxyPassProxyPassReverse是什么,你可能已经拥有。但ProxyPreserveHostX-Forwarded-Proto真的很重要。

查看Spring Boot Docs的这一部分。如果X-Forwarded-ForX-Forwarded-Proto设置,你需要添加到您的application.properties文件:

server.use-forward-headers=true 

您还将看到在该文件中,你可以为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。是的,这是一团糟。但是这让所有这些荒唐的事情都变得有用了。