如何限制用户访问控制器的特定操作?
def filters = {
loginCheck(controller:'*', action:'*') {
before = {
if(!session.user && !session.merchants)
{
redirect(action:'login')
return false
}
}}
这是我的登录安全过滤器。下面是用于限制用户搜索操作的拦截器。但两者都不起作用。谁能告诉我错误是什么吗?
def beforeInterceptor = [action:this.&checkUser,Only:'search']
def scaffold = true
def checkUser()
{
if (session.user)
{
redirect(action:"search")
}
if(session.merchants)
{
redirect(action:"toIndex")
flash.message="You are not valid user for this action"
return false
}
}
def filters = {
loginCheck(controller:'*', action:'*') {
before = {
if(!session.user && !session.merchants)
{
redirect(action:'login')
return false
}
}}
That was my filter for login security. And below is interceptor in action for restricting user from search action. But both are not working. Can any one tell what the mistake is?
def beforeInterceptor = [action:this.&checkUser,Only:'search']
def scaffold = true
def checkUser()
{
if (session.user)
{
redirect(action:"search")
}
if(session.merchants)
{
redirect(action:"toIndex")
flash.message="You are not valid user for this action"
return false
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个非常好的简写方式,您可以直接应用于操作(如果您不使用过滤器):
您可以为每个用户授予 ROLE_USER 并简单地要求该角色。
There's a really nice shorthand that you can apply directly to actions (if you're not using filters):
You can give every user ROLE_USER and simply require that role.
实际上,您的过滤器应该按照为每个控制器和每个操作设置的方式工作。
这是参考文献中的 grails 示例:
我使用过这个,它对我有用。
我不确定代码中的
session.merchants
。这是什么?
你有没有遵循这个:
编辑:
如果您使用 spring security,则不需要添加过滤器或拦截器。
检查用户指南:http://burtbeckwith.github。 com/grails-spring-security-core/docs/manual/index.html
您可以使用 url 映射或注释来配置它。
前任。
编辑2:
要获取登录用户,请使用:
actually you filter should work as it is set for every controller and every action.
here is the grails example from the reference:
I worked with this one and it worked for me.
What I'm not sure is about the
session.merchants
in your code.What is this ?
Did you follow this:
Edit:
If you use spring security you don't need to add a filter or interceptor.
check the user guide: http://burtbeckwith.github.com/grails-spring-security-core/docs/manual/index.html
you can configure it with url mappings or annotations.
ex.
EDIT 2:
To get the logged in user use: