![](/img/trans.png)
[英]Grails springSecurityService injection in domain class (NULL)
[英]Null springSecurityService causes encodePassword to fail in Grails 2.0.1
我们正在使用 Grails 2.0.1 和 Spring Security 开发一个新项目。 由于用户域 object 中的 springSecurityService 为 null,因此创建用户上下文失败。奇怪的是,这仅发生在我们的 Linux 文本框上,而在所有开发人员 windows 框中它工作正常。 不知道是跟环境有关系还是其他什么原因。 在 linux 盒子上,这一直失败。
我们正在使用的用户域 class 如下(插件生成的 class 带有几个附加字段)。 encodePassword 由 beforeInsert()、beforeUpdate() 触发器处理。
遇到这个线程,它讨论了导致 webflows 问题的瞬态引用,我假设这里没有使用它,所以不确定这是否相关。 http://grails.1312388.n4.nabble.com/Spring-Security-Plugin-1-of-the-time-springSecurityService-null-td4349941.html
class User {
transient springSecurityService
static constraints = {
firstName blank: false, nullable: false, size: 2..100
lastName blank: false, nullable: false, size: 2..100
username blank: false, nullable: false, unique : true, email: true
password blank: false, nullable: false, size: 6..255
}
static mapping = {
password column: '`password`'
}
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
/* user details */
String firstName;
String lastName;
Set<Role> getAuthorities() {
UserRole.findAllByUser(this).collect { it.role } as Set
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
}
提前致谢
如果您只想确保保存User对象并且不关心测试的密码,则可以使用Groovy的元编程覆盖encodePassword方法。
@TestFor(UserService)
@Mock(User)
class UserServiceTest {
void testMethod() {
User.metaClass.encodePassword = { -> }
service.invokeTestThatSavesUserDomainClass()
}
}
springSecurityService
不能是瞬态的。 以下代码应该有效
class User {
def springSecurityService
static transients = ["springSecurityService"]
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
}
我知道测试注入User域类的SpringSecurity服务存在一些问题。 看看这个问题: 如何测试使用SpringSecurity服务的单元测试控制器 。
在用户域 class 中添加这些:
static mapping = {
autowire true
}
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.