Grails:从被调用的方法重定向
Grails: Redirect from called method
早上好社区。
我将 Grails 用于后端应用程序,因此想检查是否允许用户执行请求的操作。
所以我在开始时调用了 checkPermission() 并在不允许用户时重定向到索引页面。但是我发生了这个错误:
无法在此处发出重定向(..)。之前对 redirect(..) 的调用已经重定向了响应。
摘自我的来源:
def test() {
println("test()")
checkPermission([TaskXYZ])
println("AfterCheck")
redirect(action: "profile", params: [lang: params.lang])
}
private checkPermission(def tasks) {
println("checkPermission()")
if ( ! userService.isAllowed(tasks)) {
println("NotAllowed")
flash.error = message(code: "msg.actionIsNotAllowed")
redirect(action: "index", params: [lang: params.lang])
return false
}
}
输出:
test()
checkPermission()
NotAllowed
AfterCheck
所以重定向在私有方法中不起作用,return false 导致 return 到被调用的方法。
我还能做什么?我考虑过beforeInterceptor,但是我需要为每个方法传递自定义参数。
谢谢,
抢
您不能在一个请求期间调用 redirect() 方法两次,尽管您的代码就是这样做的,但您可以通过多种方式简单地更改它,即
def test() {
boolean hasPermission = checkPermission([TaskXYZ])
if (!hasPermission) {
return ; // you have to finish the action, because you already called redirect inside checkPermission() method
}
// do further actions when permission granted
redirect(action: "profile", params: [lang: params.lang])
}
private checkPermission(def tasks) {
if ( ! userService.isAllowed(tasks)) {
return false
}
return true
}
您还可以对其进行改进,以便进行不同的重定向:
def test() {
boolean checkPermissionResult = checkPermission([TaskXYZ])
if (checkPermissionResult != null) {
return redirect(checkPermissionResult)
}
// do further actions when permission granted
redirect(action: "profile", params: [lang: params.lang])
}
private checkPermission(def tasks) {
if ( ! userService.isAllowed(tasks)) {
return [action: "index", params: [lang: params.lang]]
} else if (userService.userNotExists()) { // or any different situatoin
return [action: "register", params: [lang: params.lang]]
}
return null
}
您遇到的问题是发出重定向不会停止控制器的执行 method/action。在 checkPermission
内的 redirect
之后,您的控制器代码在 checkPermission
之后继续执行并遇到下一个 redirect
。如果您的 checkPermission
要终止控制器 method/action 的执行,您需要从您的控制器 return
。
例如:
...
private boolean checkPermission(def tasks) { ... }
...
if (!checkPermission([TaskXYZ])) return
...
早上好社区。
我将 Grails 用于后端应用程序,因此想检查是否允许用户执行请求的操作。
所以我在开始时调用了 checkPermission() 并在不允许用户时重定向到索引页面。但是我发生了这个错误:
无法在此处发出重定向(..)。之前对 redirect(..) 的调用已经重定向了响应。
摘自我的来源:
def test() {
println("test()")
checkPermission([TaskXYZ])
println("AfterCheck")
redirect(action: "profile", params: [lang: params.lang])
}
private checkPermission(def tasks) {
println("checkPermission()")
if ( ! userService.isAllowed(tasks)) {
println("NotAllowed")
flash.error = message(code: "msg.actionIsNotAllowed")
redirect(action: "index", params: [lang: params.lang])
return false
}
}
输出:
test()
checkPermission()
NotAllowed
AfterCheck
所以重定向在私有方法中不起作用,return false 导致 return 到被调用的方法。
我还能做什么?我考虑过beforeInterceptor,但是我需要为每个方法传递自定义参数。
谢谢, 抢
您不能在一个请求期间调用 redirect() 方法两次,尽管您的代码就是这样做的,但您可以通过多种方式简单地更改它,即
def test() {
boolean hasPermission = checkPermission([TaskXYZ])
if (!hasPermission) {
return ; // you have to finish the action, because you already called redirect inside checkPermission() method
}
// do further actions when permission granted
redirect(action: "profile", params: [lang: params.lang])
}
private checkPermission(def tasks) {
if ( ! userService.isAllowed(tasks)) {
return false
}
return true
}
您还可以对其进行改进,以便进行不同的重定向:
def test() {
boolean checkPermissionResult = checkPermission([TaskXYZ])
if (checkPermissionResult != null) {
return redirect(checkPermissionResult)
}
// do further actions when permission granted
redirect(action: "profile", params: [lang: params.lang])
}
private checkPermission(def tasks) {
if ( ! userService.isAllowed(tasks)) {
return [action: "index", params: [lang: params.lang]]
} else if (userService.userNotExists()) { // or any different situatoin
return [action: "register", params: [lang: params.lang]]
}
return null
}
您遇到的问题是发出重定向不会停止控制器的执行 method/action。在 checkPermission
内的 redirect
之后,您的控制器代码在 checkPermission
之后继续执行并遇到下一个 redirect
。如果您的 checkPermission
要终止控制器 method/action 的执行,您需要从您的控制器 return
。
例如:
...
private boolean checkPermission(def tasks) { ... }
...
if (!checkPermission([TaskXYZ])) return
...