Grails重定向控制器从https切换到http.为什么?

Dav*_*ker 5 java grails grails-controller

我有一个Grails应用程序,其中一些页面只能通过https访问,一些页面可通过http访问.使用之前的过滤器可以轻松处理.然而HTTPS页面上时,只要一个控制器并重定向用户返回HTTP的结束,并通过过滤器再次指向HTTPS.

def update = {
    ...
    redirect(action: "show", id: domainInstance.id)
}
Run Code Online (Sandbox Code Playgroud)

在Firebug我得到:

POST ... localhost:8443 (the form submit to controller)
GET ... 302 ... localhost:8080 (the redirect to show in controller)
GET ... 301 ... localhost:8443 (the redirect back to https in filter)
Run Code Online (Sandbox Code Playgroud)

如何让控制器重定向呼叫"记住"当前协议等?或者我做错了什么?

Dav*_*ker 5

我通过使用后过滤器在需要时将响应中的“位置”标头转换为 https 来解决这个问题。默认的 CachingLinkGenerator 是用 http 服务器 URL 构造的,并使用它来创建链接。所以似乎没有办法让它保持协议。我也看不出有任何简单的方法可以用我自己的扩展 LinkGenerator 替换它。

class SecurityFilters {
    def filters = {
        overall(controller: '*', action: '*') {
            after = {
                String loc = response.getHeader("Location")
                if (isRequiresHttps()) {
                    response.setHeader("Location", convertToHttps(loc))
                }
            }
        }
    }
    private boolean isRequiresHttps() { ... }
    private String convertToHttps(String url) { ... }
}
Run Code Online (Sandbox Code Playgroud)