spark-ssl icon indicating copy to clipboard operation
spark-ssl copied to clipboard

How to redirect all HTTP request to HTTPS?

Open M-Razavi opened this issue 7 years ago • 4 comments

I used secure method in Spark and everything goes well. Is there any solution to redirect request which is sent via HTTP to HTTPS? ( Something like before filter that redirect request of HTTP to same url with HTTPS.)

M-Razavi avatar Dec 19 '17 13:12 M-Razavi

Nothing built in, but you can create a before filter. I did the same for an app I have on Heroku:

app.before { ctx ->
    if (ctx.header("x-forwarded-proto") == "http") { // heroku sets this
        ctx.redirect("https://${ctx.header("host")}${ctx.path()}", 301)
    }
}

If possible, I think you should avoid doing SSL in Spark, get whatever you launch it on to take care of it instead.

tipsy avatar Dec 19 '17 13:12 tipsy

Unfortunately It doesn't work, after using Secure method, all filters only work with HTTPS request.

M-Razavi avatar Dec 20 '17 12:12 M-Razavi

Might have to use the instance API then, and create two services. One for http and one for https, then redirect from the http to the https-one ?

tipsy avatar Dec 20 '17 12:12 tipsy

I tried to create two server but because I use port 8080 for webserver, it is not possible to have two Server on same port. Why receive HTTP and HTTPS request is not possible at the same time?

M-Razavi avatar Dec 20 '17 12:12 M-Razavi