【问题标题】:String in grails aren't strings?grails中的字符串不是字符串吗?
【发布时间】:2011-05-19 14:14:26
【问题描述】:

这个错误在我身上发生过无数次,通常我会找到一种不使用字符串的方法,但这非常令人沮丧。

时不时会出现这样的错误:

控制器的执行动作[编辑] [edu.drexel.goodwin.events.web.EventController] 导致异常: groovy.lang.MissingMethodException:否 方法签名: grails.plugins.springsecurity.SpringSecurityService.ifAllGranted() 适用于参数类型: (java.lang.String) 值: [ROLE_SUPER_ADMIN]"

我使用的代码如下所示:

springSecurityService.ifAllGranted(new String("ROLE_SUPER_ADMIN")) 

我也尝试了以下所有方法均无济于事:

springSecurityService.ifAllGranted("ROLE_SUPER_ADMIN")
springSecurityService.ifAllGranted("""ROLE_SUPER_ADMIN""")
springSecurityService.ifAllGranted('ROLE_SUPER_ADMIN')
springSecurityService.ifAllGranted('''ROLE_SUPER_ADMIN''')

基本上就好像每个字符串变量都立即变成了存储在字符串中的值......但是你如何实际使用字符串变量?

非常感谢您的帮助, -阿萨夫

【问题讨论】:

  • 永远不要使用new String(string) - 只在传入字节数组时创建一个新字符串。字符串是不可变的和缓存的,因此创建一个与另一个值相同的新 String 实例是非常浪费的。

标签: string grails


【解决方案1】:

我很确定问题不是字符串问题,而是“错误地使用 API”问题。检查文档:

http://burtbeckwith.github.com/grails-spring-security-core/docs/manual/index.html

我在SpringSecurityService 类上没有看到ifAllGranted。我相信它曾经存在于旧版本的 SpringSecurity(即 Acegi)中。

尝试使用SpringSecurityUtils.ifAllGranted 代替 springSecurityService。如果您对它的工作原理感兴趣,请使用源代码,luke。

/**
     * Check if the current user has all of the specified roles.
     * @param roles  a comma-delimited list of role names
     * @return <code>true</code> if the user is authenticated and has all the roles
     */
    public static boolean ifAllGranted(final String roles) {
        Collection<GrantedAuthority> inferred = findInferredAuthorities(getPrincipalAuthorities());
        return inferred.containsAll(parseAuthoritiesString(roles));
    }

【讨论】:

  • 令人沮丧的是,我一遍又一遍地看到这个错误......因为在这种情况下,问题通常是别的。错误信息不是很有帮助...
  • @asaf np,这就是我们来这里的目的。请注意,这是一件很时髦的事情。由于 groovy 的松散类型,它试图做正确的事情。如果它无法弄清楚,那就是您收到的消息。在 Java 中,这将是一个编译器警告。使用 Grails 的权衡之一。
  • @Asaf 如果它帮助您解决了问题,请考虑接受这个答案。请参阅此处了解为什么这很重要:meta.stackexchange.com/questions/5234(您可以通过单击答案左侧的复选标记来接受答案)
猜你喜欢
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 2022-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
相关资源
最近更新 更多