【问题标题】:grails 3.3 spring security where is the beforeinsert to encrypt the password?grails 3.3 spring security 加密密码的前插入在哪里?
【发布时间】:2019-01-29 17:17:12
【问题描述】:

要将身份验证添加到 grails 3 应用程序,请将其添加到 build.gradle:

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

然后运行

 grails s2-quickstart com.myapp Operator Role

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

Operator 域对象如下所示:

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`'
}
}

问题是,缺少用于加密密码的之前插入。在 grails 2.5 中,它曾经将其放入 Operator 域对象中:

    def beforeInsert() {
            encodePassword()
    }

所以我希望密码以纯文本形式插入,但似乎并非如此。它似乎是加密的,至少对于使用 bootstrap 创建的 Operators 问题是在哪里以及如何?

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

    允许域类进行自动装配并将 bean 注入到域类的每个实例中的旧方法需要相当多的内存。更高版本的 Grails (3.3.x) 选择使用 PreInsertEvent 侦听器对密码进行编码。这在获得相同结果的同时节省了相当多的内存。请参阅 s2quicktstart 的文档和在您的应用中创建的 UserPasswordEncoderListener 类。

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

    【讨论】:

    【解决方案2】:

    对密码进行编码的另一个选项是在 Application.groovy 中注入方法。

    我的 UserAccont 类:

    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-standalode 的模块中的域类,而没有 SpringSec 依赖项。

    【讨论】:

      猜你喜欢
      • 2013-04-25
      • 2012-04-10
      • 2011-12-06
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2014-07-29
      相关资源
      最近更新 更多