grails - 在Grails中按用户保护 Controller Action

标签 grails spring-security grails-plugin

我在一个项目上使用Grails安全插件。我在 Controller 操作上使用注释来限制对某些类别的用户的访问,例如“ROLE_ADMIN”或“ROLE_USER”。

(以此为基础,我正在做什么:http://grails-plugins.github.com/grails-spring-security-core/docs/manual/guide/5%20Configuring%20Request%20Mappings%20to%20Secure%20URLs.html#5.1%20Defining%20Secured%20Annotations)

我的问题是,如何限制操作,以便用户只能看到有关自己的信息。例如,假设我有一个ID = 1的用户。如果我有一个操作在以下位置显示有关该用户的信息:

mySite/User/Show/1

如何阻止ID = 1的同一用户访问
mySite/User/Show/2

?有没有简单的方法可以做到这一点?

最佳答案

如果要将相同的逻辑应用于多个 Action ,也可以使用Grails Controller 拦截器

class SomeController {

 def beforeInterceptor = [action: this.&checkUser ] 

   def springSecurityService

def checkUser() {
    User user = User.get(params)
       User logged = User.get(springSecurityService.principal.id)
       if (user.id != logged.id) {
   {
       redirect(action: "accessDenied", controller='access' id: params.long("id")) //re-direct accessDenied page
   return false
   }
    return true;
}

}
   Class AccessController{
     def accessDenied= {

        render(view: "accessDenied")

    }
 }

关于grails - 在Grails中按用户保护 Controller Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10037297/

相关文章:

java - 如何将用户名传递到 Spring Boot 安全性的 application.properties 的登录 SQL 代码中?

java - Spring Security + spring oauth2解析

grails - 默认验证器上的自定义验证消息

grails插件重新加载

grails - Groovy 1.5.x/Grails 1.0.x If-Else语句错误

grails 2.3.x 异步和 sleep

grails - grails create-ng-controller foo“给 'Error Command not found create-ng-controller'

grails - 无法在Grails中自动创建数据库

spring-security - 允许 get-request 但不允许 post-request (Spring Security)

grails - Grails 中的用户通知