我正在使用带有mongoDB插件(v6.1.4(和Spring Security Core插件(v3.2.0(的Grails 3.3.2。
我使用以下持久性事件方法具有以下UserPasswordEncoderListener
:
@Override
protected void onPersistenceEvent(AbstractPersistenceEvent event) {
if (event.entityObject instanceof User) {
User u = (event.entityObject as User)
if (u.password && (event.eventType == EventType.PreInsert || (event.eventType == EventType.PreUpdate && u.hasChanged('password')))) {
event.getEntityAccess().setProperty("password", encodePassword(u.password))
}
}
}
问题是每次保存没有更新的用户对象时,hasChanged
调用总是返回 true,从而导致已编码的密码重新编码,从而破坏身份验证。
一种解决方法是采用旧方式,只需从数据库中检索原始密码并在编码前进行比较,但我想知道为什么hasChanged
错误地返回 true。
我通过在时髦的控制台中运行以下命令来测试hasChanged
在其他地方正确运行:
def user = User.findByEmail("user@email.com")
println "Result: "+ user.hasChanged('password')
结果是 Result: false
.为什么它在持久性侦听器类中不起作用?
仅供参考:我在resources.groovy中定义了以下bean:
userPasswordEncoderListener(UserPasswordEncoderListener,ref('mongoDatastore'))
您是否尝试过在听众中使用isDirty()
而不是hasChanged()
?
例如:
package com.mycompany.myapp
import grails.plugin.springsecurity.SpringSecurityService
import org.grails.datastore.mapping.engine.event.AbstractPersistenceEvent
import org.grails.datastore.mapping.engine.event.PreInsertEvent
import org.grails.datastore.mapping.engine.event.PreUpdateEvent
import org.springframework.beans.factory.annotation.Autowired
import grails.events.annotation.gorm.Listener
import groovy.transform.CompileStatic
@CompileStatic
class UserPasswordEncoderListener {
@Autowired
SpringSecurityService springSecurityService
@Listener(User)
void onPreInsertEvent(PreInsertEvent event) {
encodePasswordForEvent(event)
}
@Listener(User)
void onPreUpdateEvent(PreUpdateEvent event) {
encodePasswordForEvent(event)
}
private void encodePasswordForEvent(AbstractPersistenceEvent event) {
if (event.entityObject instanceof User) {
User u = event.entityObject as User
if (u.password && ((event instanceof PreInsertEvent) || (event instanceof PreUpdateEvent && u.isDirty('password')))) {
event.getEntityAccess().setProperty('password', encodePassword(u.password))
}
}
}
private String encodePassword(String password) {
springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
}
}
更多信息请访问:https://grails-plugins.github.io/grails-spring-security-core/3.2.x/index.html#tutorials
解决方法:
@Override
protected void onPersistenceEvent(AbstractPersistenceEvent event) {
if (event.entityObject instanceof User) {
User u = (event.entityObject as User)
if (u.password && (event.eventType == EventType.PreInsert || (event.eventType == EventType.PreUpdate && u.hasChanged('password')))) {
if(event.eventType == EventType.PreUpdate){ //Temp workaround until hasChanged behaves correctly
def originalUser = User.get(u?.id)
if(originalUser.password != u.password){
event.getEntityAccess().setProperty("password", encodePassword(u.password))
}
}else {
event.getEntityAccess().setProperty("password", encodePassword(u.password))
}
}
}
}
在 github 存储库 (https://github.com/grails-plugins/grails-spring-security-core/issues/539( 上留下了一个问题。将根据反馈进行更新。