0

我正在使用 grails 1.3.7。

我有以下过滤器设置:

class MyFilters {
    def userService
    def springSecurityService

    def filters = {
        all(controller: '*', action: '*') {
            before = {
                String userAgent = request.getHeader('User-Agent')

                int buildVersion = 0

                // Match "app-/{version}" where {version} is the build number
                def matcher = userAgent =~ "(?i)app(?:-\\w+)?\\/(\\d+)"

                if (matcher.getCount() > 0)
                {                   
                    buildVersion = Integer.parseInt(matcher[0][1])

                    log.info("User agent is from a mobile with build version = " + buildVersion)
                    log.info("User agent = " + userAgent)

                    String redirectUrl = "https://anotherdomain.com"

                    if (buildVersion > 12)
                    {
                        if (request.queryString != null)
                        {
                            log.info("Redirecting request to anotherdomain with query string")
                            redirect(url:"${redirectUrl}${request.forwardURI}?${request.queryString}",params:params)
                        }

                        return
                    }
                }
            }
            after = { model ->
                if (model) {
                    model['currentUser'] = userService.currentUser
                }
            }
            afterView = {

            }
        }
    }
}

一个问题是重定向不会发生在我想的那一点上。我希望所有执行都停止并重定向到我此时给出的确切 url。

当我调试到“重定向”行时,它会继续通过该行执行其他行并跳转到另一个控制器。

4

1 回答 1

0

为了防止正常的处理流程继续进行,您需要false从您的before过滤器返回:

if (buildVersion > 12)
{
    if (request.queryString != null)
    {
        log.info("Redirecting request to anotherdomain with query string")
        redirect(url:"${redirectUrl}${request.forwardURI}?${request.queryString}",params:params)
        return false
    }
}

这在用户指南第 6.6.2 节的最后提到,但不是特别突出:

请注意返回 false 如何确保操作本身不被执行。

于 2012-12-21T11:02:15.897 回答