spring - 如何让 SpringSecurity/Grails 与终止 SSL 的负载均衡器很好地配合

标签 spring tomcat grails spring-security amazon-elb

我们在 Tomcat 上部署了一个 Grails 应用程序,该应用程序部署在终止 SSL 的负载均衡器后面(负载均衡器然后与端口 8080 上的 tomcat 实例通信)。我们已经将 SpringSecurity 配置为需要所有资源的安全通道,注意来自负载均衡器的 header ,强制使用 https 并映射来自负载均衡器的端口:

grails.plugin.springsecurity.secureChannel.useHeaderCheckChannelSecurity = true
grails.plugin.springsecurity.auth.forceHttps = true
grails.plugin.springsecurity.portMapper.httpPort = 80
grails.plugin.springsecurity.portMapper.httpsPort = 443
grails.plugin.springsecurity.secureChannel.definition = [
        '/**': 'REQUIRES_SECURE_CHANNEL'
]

其中大部分工作正常 - 来自 Grails 内部的重定向正按预期使用 https 协议(protocol),以及大多数 ajax 请求。

但是有一些 ajax 请求正常工作。它们都与与 j_spring_security_check 等 j_spring_security* 端点交互的结果有关。例如,如果用户尝试通过 ajax 登录,我们会在浏览器中收到此错误(这是成功登录启动的重定向):

 Mixed Content: The page at 'https://www.servernamehere.com/' was loaded over HTTPS, but 
 requested an insecure XMLHttpRequest endpoint 'http://www.servernamehere.com/login/ajaxSuccess'.
 This request has been blocked; the content must be served over HTTPS.

认证失败会出现同样的问题:

Mixed Content: The page at 'https://www.servernamehere.com/' was loaded over HTTPS, but requested 
an insecure XMLHttpRequest endpoint 'http://www.servernamehere.com/login/authfail?ajax=true'. 
This request has been blocked; the content must be served over https.

我们如何配置 spring security 来理解来自身份验证事件的所有重定向都需要是 https?

最佳答案

我们能够通过创建自定义重定向策略(实现 org.springframework.security.web.RedirectStrategy)并用我们的自定义重定向策略 bean 替换默认重定向策略 bean 来解决此问题。自定义重定向策略检查负载均衡器传入的 header ,并确保将响应重定向到适当的协议(protocol)

关于spring - 如何让 SpringSecurity/Grails 与终止 SSL 的负载均衡器很好地配合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27413217/

相关文章:

java - Mybatis log4j如何配置log4j以将sql日志打印到文件

java - 从 Netbeans 启动 Tomcat Web 应用程序时出现问题

tomcat - 如何跳过 BIRT 读取单元格,在 Tomcat 控制台上下载 pdf 报告时读取行消息

xml - Tomcat web.xml MIME 映射不适用于 .js.map 文件

grails - 获取Grails的最新更新记录

spring - 如何编写多个测试的 Spring 测试套件并运行选择性测试?

java - 为什么我们不应该使用查找而不是依赖注入(inject)?

java - Spring - RestTemplate 调用 https rest 服务时出错(证书错误)

Grails - 在开发模式下显示页面处理/渲染调试信息

Grails - 错误 fork Grails VM 因错误退出