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



我有一个Grails应用程序,其中一些页面只能通过https访问,另一些只能通过http访问。使用before过滤器很容易处理这个问题。然而,当在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)

我怎么能得到控制器重定向调用"记住"当前协议等?还是我做错了什么?

我通过使用after过滤器在需要时将响应中的"Location"标头转换为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) { ... }
}

这是一个bug,将在2.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)

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

然而,在你的代码中,你不应该关心是否SSL。http://www.juliesoft.com/2010/04/automatic-httphttps-switching-with-grails/

最新更新