关于Java:无法停止Spring将https重定向到http

Cannot stop Spring redirecting https to http

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

我遇到了一个错误(如果出现登录错误):

1
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.

和(如果成功登录):

1
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.

我问过供应商有关此问题的信息,但他们说"您的应用程序和nginx之间没有https,所以这是您的应用程序问题" ...

我尝试过此方法,但此解决方案看起来很奇怪,并且不能解决问题(它需要添加许多配置类,我想应该不会那么难)。其实我很困惑如何发生,为什么不是重定向到发出请求的模式的默认行为...

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

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

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

这也无济于事:

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

我没有使用Spring boot,但也尝试了此操作,没有帮助。

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

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


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

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

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

参考:Apache Tomcat 7:HTTP连接器