【问题标题】:how to restrict user from accessing a perticular action of a controller?如何限制用户访问控制器的特定操作?
【发布时间】:2023-03-14 21:02:01
【问题描述】:

嗨。 我的应用程序有两个用户。一个是管理员 n 其他访问者..我的控制器中有两个操作。我管理员可以对两者都采取行动,但我希望限制访问者访问第二个操作。如何实现它?

提前谢谢答复,

【问题讨论】:

标签: grails permissions controller action


【解决方案1】:

弹簧安全插件是要走的路。 http://grails.org/plugin/spring-security-core

我建议使用注释方法。博文http://blog.springsource.com/2010/08/11/simplified-spring-security-with-grails/中有一个使用注解的部分

我建议您完整阅读上述博客文章

例如控制器操作将像....一样受到保护。

//all users
@Secured(['IS_AUTHENTICATED_REMEMBERED'])
def firstAction = { ... }

//only admin
@Secured(['ROLE_ADMIN'])
def secondAction = { ... }

如果您不想强迫用户始终登录,该插件还提供“记住我”选项

【讨论】:

  • 我正在尝试使用 spring security..但我的应用程序中已经有 USER 域类..和用户控制器,如果我尝试删除它并在插件中实现其功能生成的用户域.我的用户控制器无法工作..我该怎么办?
  • 你能给我一个关于如何在现有用户域类上使用 Spring Security 的任何示例应用程序的链接
  • 您可以通过将类似这样的内容添加到您的 Config.groovy grails.plugins.springsecurity.userLookup.userDomainClassName = 'com.app.Person' grails.plugins.springsecurity.userLookup.usernamePropertyName 来使用您的用户域= '电子邮件' grails.plugins.springsecurity.userLookup.passwordPropertyName = 'passwd'
  • 而不是我在我的用户域类和商家域类中扩展插件生成用户..现在我想限制我的操作命名为来自商家的搜索,因为我写了 **@Secured([' ROLE_MERCHANTS']) ** 搜索操作
  • 现在,当我点击搜索链接时,我得到了插件生成的登录表单,但是当我输入商家表中的用户名和密码时。它给出的错误消息是没有用户 ID 和密码发现为什么会这样?
【解决方案2】:

我建议使用控制器拦截器http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.1Controllers

或安装 Spring Security Core 插件http://www.grails.org/plugin/spring-security-core

【讨论】:

  • 拦截器我用来保护登录的应用程序..我可以使用过滤器
  • 是的,您也在使用过滤器,但是 Spring Security Core 插件为您提供了所有这些功能。
  • 我建议使用过滤器通过登录来保护您的应用。这里有一个执行此任务的示例过滤器:grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.6(搜索“过滤器”)。然后,您可以使用控制器拦截器将特定操作限制为仅限管理员。
  • @Fabrizio:我认为 SpringSecurity 插件是你不错的选择
  • '如何在堆栈溢出的注释中添加代码。我想显示代码'
猜你喜欢
  • 2023-04-07
  • 2015-10-12
  • 2016-05-27
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2014-08-24
  • 2017-10-24
相关资源
最近更新 更多