【问题标题】:Grails redirect in controller switching from https to http. Why?Grails 重定向控制器从 https 切换到 http。为什么?
【发布时间】:2011-12-20 02:09:30
【问题描述】:

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

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

【问题讨论】:

    标签: java grails grails-controller


    【解决方案1】:

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

    【讨论】:

    • 我正在使用 grails 1.3.7,当我使用它时,我得到:groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.grails.web.sitemesh.GrailsContentBufferingResponse.getHeader () 适用于参数类型:(java.lang.String) 值:[Location]
    • 嗯。也许这只是在 Grails 2.0 中添加的?我从未使用过 Grails 1.3.x。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 是的,但是我必须对每个控制器进行编辑 + 脚手架控制器将使用正常重定向
      • 可能与这个 JIRA 问题有关:jira.grails.org/browse/GRAILS-8053 所以我可能会在这个问题上发表我的意见以给予重视。
      • 嗯。它在 2.0M2 中标记为固定,我使用的是 2.0RC1。我想我必须查看 Grails 代码才能弄清楚这里发生了什么。
      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2019-05-05
        • 2014-03-11
        • 1970-01-01
        • 2011-08-22
        • 2011-11-24
        • 2018-02-12
        • 2019-09-10
        • 2017-10-06
        • 1970-01-01
        相关资源
        最近更新 更多