代码之家  ›  专栏  ›  技术社区  ›  Tom

Grails Spring Security/Acegi。自定义用户+密码过期管理

  •  0
  • Tom  · 技术社区  · 15 年前

    我正在研究一个Grails遗留项目。名为用户的域类存在。它包含密码、用户名、角色等。

    此项目使用Spring安全性进行角色管理。我想添加凭据过期(强制用户续订其密码)。

    我已经修改了用户类。不是它实现了 UserDetails interface .

    但是,当我启动服务器时,会收到此错误>

    org.springframework.beans.factory.bean创建一个例外: 创建名为的bean时出错 “messagesource”:的初始化 bean失败;嵌套异常为 org.springframework.beans.factory.bean创建一个例外:

    创建名为的bean时出错 “TransactionManager”:无法解析 对bean“sessionFactory”的引用 设置bean属性时 'sessionFactory';嵌套异常为 org.springframework.beans.factory.bean创建一个例外:

    创建名为的bean时出错 “sessionFactory”:调用init 方法失败;嵌套异常为 org.hibernate.propertynotfoundexception:

    找不到属性的setter 类中未过期的帐户 com.company.app.user.user.用户

    我需要登记一些豆子吗?我发现这个错误非常令人困惑,因为接口不要求setter方法。

    更新

    在进一步调查之后,我遇到了securityconfig.groovy,看起来 很多 像这样( AcegiSecurity Plugin ):

    security {
       // see DefaultSecurityConfig.groovy for all settable/overridable properties
    
       active = true
    
       loginUserDomainClass = "User"
       authorityDomainClass = "Role"
    
     ....
    }
    

    我的用户类也看起来 很多 这样地:

    /**
     * User domain class.
     */
    class User {
       static transients = ['pass','passwordExpired','credentialsNonExpired']
       static hasMany = [authorities: Role]
       static belongsTo = Role
       /** Username */
       String username
       /** User Real Name*/
       String userRealName
       /** MD5 Password */
       String passwd
       /** enabled */
       boolean enabled
    
       String email
       boolean emailShow
    
       /** description */
       String description = ''
    
       /** plain password to create a MD5 password */
       String pass = '[secret]'
    
       static constraints = {
          username(blank: false, unique: true)
          userRealName(blank: false)
          passwd(blank: false)
          enabled()
       }
    
       public boolean isCredentialsNonExpired() {
             return true;
       }
    }
    

    我添加了一个方法来检查这个类的密码是否应该过期 (isCredentialsNonExpired) . 我需要在登录时执行此方法。现在不行。

    所以看起来这是我应该采用的Acegi方法。有什么想法吗?我的理解是Acegi使用了Spring安全性,对吧?

    1 回复  |  直到 15 年前
        1
  •  3
  •   Burt Beckwith    15 年前

    您是使用Acegi插件,还是直接配置它?如果您使用 Spring Security Core 插件这将更容易,因为它是现成的支持。

    您可能不想让您的用户类成为您的UserDetailsService类,因为它通常有其他的包袱,如映射集合等。它当然可以工作,但是创建一个简单的数据类是一种更好的方法。根据Spring Security的版本,可以方便地将org.springframework.security.userdetails.user或org.springframework.security.core.userdetails.user子类化。

    看起来您刚刚为accountnonexpired添加了一个getter,但是如果要持久化,那么它也需要一个setter。如果要派生值,但不希望该字段位于数据库中,则可以将其添加到“瞬变”列表中:

    static transients = ['accountNonExpired']
    
    推荐文章