Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ssl 如何为java Play强制使用https!Heroku上的应用程序?_Ssl_Heroku_Https_Playframework - Fatal编程技术网

Ssl 如何为java Play强制使用https!Heroku上的应用程序?

Ssl 如何为java Play强制使用https!Heroku上的应用程序?,ssl,heroku,https,playframework,Ssl,Heroku,Https,Playframework,我有一个Java剧本!Heroku上用作RESTful API的应用程序。我想对所有客户端连接强制使用TLS上的HTTP。这样做的最佳实践是什么?最短的代码是在Global.java @Override public Action onRequest(final Http.Request request, Method actionMethod) { //contains http or https String header=request.getHeader("x-forwar

我有一个Java剧本!Heroku上用作RESTful API的应用程序。我想对所有客户端连接强制使用TLS上的HTTP。这样做的最佳实践是什么?

最短的代码是在
Global.java

@Override
public Action onRequest(final Http.Request request, Method actionMethod) {

   //contains http or https
   String header=request.getHeader("x-forwarded-proto");

   if(header != null && header.equals("http")){

       return new Action.Simple() {
           @Override
           public F.Promise<Result> call(Http.Context ctx) throws Throwable {
               return F.Promise.pure(redirect("https://" + request.host() + request.path()));
           }
       };


   }
}
应用程序.java中(或在任何类/方法之前):


谢谢朱利安在游戏中的帮助!。事实证明,Heroku已经开箱即用地支持HTTPS:“所有默认的appname.herokuapp.com域都已经启用了SSL,并且可以通过使用HTTPS进行访问,例如。”事实上,如果您设置了SSL插件,您可以访问您的,但并不意味着用户将使用它。如果你希望你的网站是安全的,为了保护凭证安全或者更糟糕的是信用卡号码,你需要强制使用httpS
package actions;

import play.Play;
import play.libs.F;
import play.mvc.*;

public class ForceHttps extends Action<Controller> {

   // heroku header
   private static final String SSL_HEADER = "x-forwarded-proto";

   @Override
   public F.Promise<SimpleResult> call(Http.Context ctx) throws Throwable {

       if (Play.isProd() && !isHttpsRequest( ctx.request() )) {
           return F.Promise.promise(() -> redirect("https://" + ctx.request().host() + ctx.request().uri()) );
       }

       // let request proceed
       return this.delegate.call(ctx);
    }

    private static boolean isHttpsRequest(Http.Request request) {
       // heroku passes header on
       return request.getHeader(SSL_HEADER) != null && request.getHeader(SSL_HEADER).contains("https");
    }

}
@With(ForceHttps.class)
public class Application extends Controller {