无法阻止Spring将https重定向到http

时间:2017-07-16 11:59:59

标签: java spring redirect nginx spring-security

我正在Spring Security开发项目,一切都很顺利,直到我将项目加载到生产服务器。我的本地计算机上只有http,但生产服务器上有https。

我遇到错误(如果登录错误):

Mixed Content: The page at 'https://my.production.com/login' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://my.production.com/api/login?error=bad-credentials'. This request has been blocked; the content must be served over HTTPS.

和(如果成功登录):

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

我向我的供应商询问了这个问题,但他们说" 你的app和nginx之间没有https,所以这是你的应用程序问题" ...

我试过了this,但是这个解决方案看起来非常奇怪并且没有解决问题(它需要添加很多配置类,我想它不应该那么难)。实际上我很困惑,这怎么会发生,为什么不是重定向到请求的架构的默认行为......

我还尝试将其添加到我的Spring Security配置中:

 .and().requiresChannel().anyRequest().requiresSecure()

但这只会导致本地计算机和生产服务器上的ERR_TOO_MANY_REDIRECTS ...

这也没有帮助:

http.portMapper()
                .http(8080).mapsTo(8443);

我没有使用Spring启动,但也尝试了this,没有帮助。

成功身份验证配置如下所示:

SavedRequestAwareAuthenticationSuccessHandler successHandler = new
                SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setDefaultTargetUrl(env.getProperty("app.authenticationSuccessUrl"));

1 个答案:

答案 0 :(得分:1)

当Apache Tomcat在HTTPS(反向)代理后面运行时,链接和重定向可能需要一些配置才能正常工作。

打开conf / server.xml ,找到Connector元素,添加以下属性(如果尚未存在):

  • proxyName - 设置为您的域名。
  • proxyPort - 如果您使用标准的https端口,请将其设置为443。
  • scheme - 如果使用https访问网站,则设置为“https”。
  • secure - 为https设置为“true”。
<Connector proxyName="my.production.com" proxyPort="443" scheme="https" secure="true" ...>

参考:Apache Tomcat 7: The HTTP Connector