首页 文章

Grails Acegi插件注释

提问于
浏览
0

我正在使用Spring Security(AKA Acegi)插件提供的注释 . 我有控制器操作注释

@Secured(['ROLE_ADMIN', 'ROLE_USER'])

表明管理员和普通用户可以使用它们 . 但现在我需要指出管理员和未注册用户可以使用某个操作 . 是否可以使用注释来指示没有任何角色的用户,即未注册?

谢谢,唐

2 回答

  • 2

    这是一个解决方案,要求您不要登录,或者您有ROLE_ADMIN . 您需要一个处理新的“IS_NOT_AUTHENTICATED”令牌的自定义选民:

    package com.burtbeckwith.grails.springsecurity
    
    import org.springframework.security.Authentication
    import org.springframework.security.AuthenticationTrustResolverImpl
    import org.springframework.security.ConfigAttribute
    import org.springframework.security.ConfigAttributeDefinition
    import org.springframework.security.vote.AccessDecisionVoter
    
    class NotLoggedInVoter implements AccessDecisionVoter {
    
       private authenticationTrustResolver = new AuthenticationTrustResolverImpl()
    
       int vote(Authentication authentication, object, ConfigAttributeDefinition config) {
          for (configAttribute in config.configAttributes) {
             if (supports(configAttribute)) {
                if (authenticationTrustResolver.isAnonymous(authentication)) {
                   // allowed if not logged in
                   return ACCESS_GRANTED
                }
                for (authority in authentication.authorities) {
                   if ('ROLE_ADMIN' == authority.authority) {
                      // allowed if logged in as an admin
                      return ACCESS_GRANTED
                   }
                }
             }
          }
    
          return ACCESS_DENIED
       }
    
       boolean supports(ConfigAttribute attribute) {
          'IS_NOT_AUTHENTICATED' == attribute?.attribute
       }
    
       boolean supports(Class clazz) {
          true
       }
    }
    

    在resources.groovy中将其注册为bean:

    beans = {
       notLoggedInVoter(com.burtbeckwith.grails.springsecurity.NotLoggedInVoter)
    }
    

    并通过设置'decisionVoterNames'属性将其添加到SecurityConfig.groovy中的选民列表中:

    decisionVoterNames = ['notLoggedInVoter', 'authenticatedVoter', 'roleVoter']
    

    并使用以下方法注释您的控制器操作:

    @Secured(['IS_NOT_AUTHENTICATED'])
    

    它只允许使用ROLE_ADMIN的非身份验证用户和经过身份验证的用户 .

  • 2

    该令牌是IS_AUTHENTICATED_ANONYMOUSLY,这意味着任何人都登录或不登录 . IS_AUTHENTICATED_REMEMBERED表示使用remember-me cookie或通过显式登录登录的任何人,IS_AUTHENTICATED_FULLY表示通过显式登录(不使用cookie)登录 .

    如果您使用注释操作

    @Secured(['IS_AUTHENTICATED_ANONYMOUSLY'])
    

    那么它将允许任何人访问该动作 . 您可以将这些特殊令牌与角色组合在一起,例如,允许仅管理员但强制用户/密码登录,即使用户有一个你使用的记住我的cookie

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    

相关问题