在身份验证后使用基于LDAP角色的授权进行身份验证后的Grails应用程序访问



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

我可以看到角色并在我的config.groovy注释中使用它或保护控制器中的操作,但是我需要一种方案/方法来显示"拒绝..."消息和注销。(邮政禁止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 3中您可以设置一个拦截器来检查每个请求并采取适当的操作。在您的情况下,您需要在before块中添加检查。

编辑:正如杰夫·布朗(Jeff Brown

编辑:登录逻辑中的类似内容:

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

最新更新