gpt4 book ai didi

mongodb - grails + mongodb中未触发beforeInsert

转载 作者:行者123 更新时间:2023-12-02 14:20:15 24 4
gpt4 key购买 nike

看起来beforeInsert永远不会触发,并且密码以纯文本格式存储在DB中,我有一个User域类,如下所示:

class User {

String email
String name
String surname
String phone
String password

static constraints = {
email(blank:false, email:true, unique:true)
name(blank:false)
surname(blank:false)
phone(blank:false)
password(blank:false, password:true)
}

def beforeInsert = { encodePassword() }

def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}

protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
}

知道我在做什么错吗?

提前致谢!

最佳答案

这很奇怪,我不知道为什么,但是这行得通,我将beforeInsert var更改为一个方法,并添加了瞬时springSecurityService

class User {

transient springSecurityService

String email
String name
String surname
String phone
String password

static constraints = {
email(blank:false, email:true, unique:true)
name(blank:false)
surname(blank:false)
phone(blank:false)
password(blank:false, password:true)
}

def beforeInsert() {
encodePassword()
}

def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}

protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
}

关于mongodb - grails + mongodb中未触发beforeInsert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14492317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com