代码之家  ›  专栏  ›  技术社区  ›  John Little

grails3.3spring安全性beforeinstert在哪里加密密码?

  •  1
  • John Little  · 技术社区  · 5 年前

    要向grails3应用程序添加身份验证,您可以将此添加到构建.gradle:

    compile 'org.grails.plugins:spring-security-core:3.2.3'
    

     grails s2-quickstart com.myapp Operator Role
    

    这将创建3个域对象,但我找不到其他对象。

    操作员域对象如下所示:

    package com.myapp
    
    importgroovy.transform.EqualsAndHashCode
    importgroovy.transform.ToString
    importgrails.compiler.GrailsCompileStatic
    
    @GrailsCompileStatic
    @EqualsAndHashCode(includes='username')
    @ToString(includes='username',includeNames=true,includePackage=false)
    classOperatorimplementsSerializable{
    
    privatestaticfinallongserialVersionUID=1
    
    Stringusername
    Stringpassword
    booleanenabled=true
    booleanaccountExpired
    booleanaccountLocked
    booleanpasswordExpired
    
    Set<Role>getAuthorities(){
    (OperatorRole.findAllByOperator(this)asList<OperatorRole>)*.roleasSet<Role>
    }
    
    staticconstraints={
    passwordnullable:false,blank:false,password:true
    usernamenullable:false,blank:false,unique:true
    }
    
    staticmapping={
    passwordcolumn:'
    `password`'
    }
    }
    

    问题是,缺少用于加密密码的before insert。在grails 2.5中,它用于将其放入Operator域对象中:

        def beforeInsert() {
                encodePassword()
        }
    

    所以我希望密码是纯文本插入的,但似乎不是。它似乎是加密的,至少对于用bootstrap创建的操作员来说,问题是在哪里以及如何加密?

    2 回复  |  直到 5 年前
        1
  •  3
  •   erichelgeson    5 年前

    允许域类进行自动连接并向域类的每个实例中注入bean的旧方法消耗了相当多的内存。更高版本的Grails(3.3.x)选择 PreInsertEvent 侦听器对密码进行编码。这在获得相同结果的同时节省了相当多的内存。参见s2quicktstart和类的文档 UserPasswordEncoderListener

    https://grails-plugins.github.io/grails-spring-security-core/latest/index.html#tutorials

        2
  •  0
  •   injecteer    5 年前

    另一种对密码进行编码的方法是在应用程序.groovy.

    class UserAccount {
    
      //.. fields 
    
      def beforeInsert() {
        encodePassword()
      }
    
      def beforeUpdate() {
        encodePassword()
      }
    
      String encodePassword() {
        throw new UnsupportedOperationException('Not implemented password encoder!' )
      }
    }
    

    注射看起来像:

    class Application extends GrailsAutoConfiguration {
    
      @Override
      void doWithDynamicMethods() {
        SpringSecurityService springSecurityService = applicationContext.getBean 'springSecurityService'
        UserAccount.metaClass.encodePassword = {-> 
          delegate.password = springSecurityService.encodePassword delegate.password
        }
      }
    }
    

    在这种情况下我不必 @Autowire GORM实体中的服务。在我的应用程序中,我还可以使用另一个基于GORM标准模块的域类,而不依赖于SpringSec。