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

发布于 2024-12-13 08:43:55 字数 520 浏览 2 评论 0原文

我有一个 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)

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

I have a Grails app with some pages only accessible over https and some over http. This is easily handled using a before filter. However when on an https page as soon as a controller does a redirect the user ends up back on http and is directed to https again by the filter.

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

In Firebug I get:

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)

How can I get the controller redirect call to "remember" the current protocol etc.? Or am I doing something else wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

归途 2024-12-20 08:43:55

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

I sorted this out by using an after filter to convert the "Location" header in the response to https when needed. The default CachingLinkGenerator is constructed with the http server URL and uses this to create links. So there doesn't seem to be a way to get it to keep the protocol. I also couldn't see any easy way to replace it with my own extended 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) { ... }
}
你是我的挚爱i 2024-12-20 08:43:55

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

It's a bug and appears to be fixed in version 2.0

放血 2024-12-20 08:43:55

我建议手动构建您的 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)

I would suggest constructing your URL manually and using redirect with that

Manually as in:

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

OR

def url = createLink(action: "show", id: domainInstance.id, absolute: true)
url = url.replace("http:", "https:")
redirect(url: url)
内心荒芜 2024-12-20 08:43:55

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

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

I am not sure how you have configured your grails app to run SSL. Maybe you have configured it transparently in you tomcat server connector?

However, in your code you should not care about SSL or not. Maybe this helps: http://www.juliesoft.com/2010/04/automatic-httphttps-switching-with-grails/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文