我正在使用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 = {
}
}
}
}
出现问题的原因是重定向不会发生在我想到的地方。 我希望所有执行都停止并重定向到我在此时给出的exaact url。
当我调试到“重定向”行时,它会继续经过这条线,从而排出其他行并跳转到另一个控制器。
答案 0 :(得分: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
}
}
这在section 6.6.2 of the user guide的最后提到,但并不是特别突出:
请注意返回false如何确保不执行操作本身。