在Heroku中重定向SSL

JMM*_*JMM 2 java ssl heroku

我在让我的Java Web应用程序在Heroku上工作时遇到了麻烦.

这就是我所拥有的:

使用Spring Security的Java Web应用程序(标准war文件),在我的web.xml中有一个安全约束部分,如下所示:

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>SSL URLs</web-resource-name>
            <url-pattern>/j_spring_security_check</url-pattern>
            <url-pattern>/secure/account/create</url-pattern>
            <url-pattern>/register</url-pattern>
            <url-pattern>/login/*</url-pattern>
            <url-pattern>/</url-pattern> 
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint> 
Run Code Online (Sandbox Code Playgroud)

当我将我的WAR文件部署到Heroku(使用Atlassian Bamboo的Heroku部署插件)并启动应用程序时,我的浏览器中出现"太多重定向"错误 - 看起来它与之间的轻弹有关https和http,但我无法弄清楚我需要做些什么来修复它.

我现在只想使用背负式SSL,因为SSL插件对于我的爱好项目而言相当昂贵(每月20美元).

bim*_*api 12

我通过使用requires-channelSpring Security配置和Tomcat的组合解决了类似的问题RemoteIpValve.如果您没有使用Spring Security,那么配置RemoteIpValve就足够了(以下假设您使用的是优秀webapp-runner的容器)我想有一个相当于jetty-runner,但我不知道...

问题是Heroku的Web /路由层处理SSL,并将请求作为普通HTTP代理到您的webapp.因此,Tomcat不知道请求在其他地方是安全的(SSL卸载)RemoteIpValve本质上request.isSecure()是通过查看代理服务器设置的HTTP 头来覆盖Tomcat看到的协议,端口和IP地址(反过来,如何评估)处理了SSL.

context.xml使用以下内容创建一个:

<Context>
  <Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="x-forwarded-for" protocolHeader="x-forwarded-proto" portHeader="x-forwarded-port"/>
</Context>
Run Code Online (Sandbox Code Playgroud)

这三个标题(x-forwarded-for,x-forwarded-protox-forwarded-port)由Heroku的路由层自动发送.

接下来,确保将文件复制到maven目标目录中的某个位置(我将其放在与webapp-runner自身相同的目录中).在pom.xml:

<resources>
  <resource>
    <directory>${basedir}/src/main/resources/META-INF</directory>
    <includes>
      <include>context.xml</include>
    </includes>
    <targetPath>${project.build.directory}/dependency</targetPath>
  </resource>
</resources>
Run Code Online (Sandbox Code Playgroud)

最后,确保webapp-runner指向context.xml(例如,使用支持该context-xml参数的webapp-runner 7.0.30.1 ).在你的Procfile:

web: java $JAVA_OPTS -jar target/dependency/webapp-runner.jar --context-xml target/dependency/context.xml ...etc...
Run Code Online (Sandbox Code Playgroud)

最后一点,我使用Spring Security的requires-channel<sec:intercept-url>翻起的环境变量来来回切换HTTP和HTTPS之间-在易于配置我的本地开发中,在Heroku上设置取消设置(通过heroku config)有选择地强制SSL在不同的环境.

希望这对你有用......