5

我有一个 Grails 应用程序,其中一些页面只能通过 https 和一些通过 http 访问。这很容易使用前置过滤器处理。但是,当控制器在 https 页面上进行重定向时,用户最终会返回 http 并被过滤器再次定向到 https。

def update = {
    ...
    redirect(action: "show", id: domainInstance.id)
}

在 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)

如何让控制器重定向调用“记住”当前协议等?还是我做错了什么?

4

4 回答 4

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) { ... }
}
于 2011-11-14T11:24:29.983 回答
3

这是一个错误,似乎已在 2.0 版中修复

于 2011-11-04T03:11:42.923 回答
0

我不确定您如何配置 grails 应用程序来运行 SSL。也许你已经在你的 tomcat 服务器连接器中透明地配置了它?

但是,在您的代码中,您不应该关心 SSL 与否。也许这有帮助:http ://www.juliesoft.com/2010/04/automatic-httphttps-switching-with-grails/

于 2011-11-02T11:55:10.690 回答
0

我建议手动构建您的 URL 并使用重定向

手动如下:

def uri = createLink(action: "show", id: domainInstance.id, absolute: false)
redirect(uri: uri)

或者

def url = createLink(action: "show", id: domainInstance.id, absolute: true)
url = url.replace("http:", "https:")
redirect(url: url)
于 2011-11-02T07:43:51.747 回答