Sunday, June 7, 2009

redirect http to https with grails

use a grails filter:
class SecurityFilters {
def filters = {
https(controller: '*', action: '*') {
before = {
if (!request.secure) {
String url = request.requestURL.toString()
String prefix = "http://"
String suffix = ".dispatch"

int startIndex = prefix.size()
def endIndex = url.indexOf(suffix) - 1
def grails = url.indexOf("/grails/")

String body = url[startIndex..grails - 1] + url[grails + 7..endIndex]
String secureHttp = "https://$body"
redirect(url: secureHttp)
return false
}
}
}

No comments: