Grails3.3Spring安全性加密密码的beforeinsert在哪里?

Grails3.3Spring安全性加密密码的beforeinsert在哪里?,grails,spring-security,Grails,Spring Security,要向grails 3应用程序添加身份验证,请将以下内容添加到build.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 import

要向grails 3应用程序添加身份验证,请将以下内容添加到build.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`'
}
}

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

允许域类进行自动连接并将bean注入域类的每个实例的旧方法需要相当多的内存。Grails(3.3.x)的更高版本选择了
PreInsertEvent
侦听器来编码密码。这在实现相同结果的同时节省了大量内存。请参阅s2quicktstart的文档以及在应用程序中创建的类
UserPasswordEncoderListener


对密码进行编码的另一个选项是将该方法注入Application.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
    }
  }
}
在这种情况下,我不必在GORM实体中提供
@Autowire
服务。在我的应用程序中,我还可以使用另一个基于GORM standalode的模块中的域类,该模块没有SpringSec依赖项。

John,请访问。
class Application extends GrailsAutoConfiguration {

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