首页 > 解决方案 > 使用基于 ldap 角色的授权进行身份验证后的 grails 应用程序访问

问题描述

我们的 grails 应用程序使用 ldap 身份验证,没有任何问题,现在我需要阻止对整个应用程序的访问,如果用户没有特定的 ldap 角色。

我可以看到角色并在我的 Config.groovy 注释中使用它或保护控制器中的操作,但我需要一种方案/方式来仅显示“Denied ...”消息并注销。(禁止发布 403)。

def filters = {
    loginFilter(controller:'login', action:'ajaxSuccessSproutcore') {
        before = {
            switch(Environment.current.name) {
                case { it == 'development' || it == 'hrm'}:
                    if (springSecurityService.isLoggedIn() && grails.plugin.springsecurity.SpringSecurityUtils.ifAnyGranted("ROLE_ADMIN, ROLE_SEA_HRM_LOGIN")){
                    } else {
                        if (springSecurityService.isLoggedIn()) {
                            render ([msg:''] as JSON)
                            session.invalidate()
                            return false
                        }
                    }
                    break

                default:
                    if (springSecurityService.isLoggedIn() && grails.plugin.springsecurity.SpringSecurityUtils.ifAnyGranted("ROLE_ADMIN , ROLE_USER")){
                    } else {
                        if (springSecurityService.isLoggedIn()) {
                            render ([msg:''] as JSON)
                            session.invalidate()
                            return false
                        }
                    }
                    break
            }
        }
        after = { Map model ->
        }
        afterView = { Exception e ->
        }
    }
}

标签: grails

解决方案


在 grails 3 中,您可以设置一个拦截器来检查每个请求并采取适当的措施。在您的情况下,您希望在before块中添加一个检查。

编辑:正如 Jeff Brown 在评论中指出的那样,grails 2 使用了过滤器而不是拦截器。

编辑:在您的注销逻辑中是这样的:

...
        else {                
           if (springSecurityService.isLoggedIn()) {
               session.invalidate()
               redirect action:'youShallNotPass'
               return false
           }
        }

推荐阅读