我已经安装了grails Spring-Security插件:
plugins {
compile ':spring-security-core:2.0-RC2'
}
然后,我使用grails s2-quickstart com.jane Person Role命令创建所需的域类。
由于我拥有自己的User类,因此重构了代码以使用User类:
package com.jane
class User {
transient springSecurityService
String email
String name
String password
Boolean isAgreeTerms = false
Date agreeTermsDt
Boolean isActive = false
Boolean isBlocked = false
Date dateCreated
Integer createdBy = 0
Date lastUpdated
Integer modifiedBy = 0
static transients = [ 'springSecurityService' ]
static hasMany = [ userProductTier: UserProductTier ]
static mapping = {
id column: "userID"
dateCreated column: 'createdDt'
lastUpdated column: 'modifiedDT'
}
static constraints = {
email blank: false, email: true, unique: true, size: 5..100
name blank: false, size: 3..50
password blank: false
}
void beforeInsert() {
if ( isAgreeTerms ) {
agreeTermsDt = new Date()
}
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
}
然后,我将config.groovy文件修改为:
grails.plugin.springsecurity.userLookup.userDomainClassName = 'com.jane.User'
grails.plugin.springsecurity.userLookup.usernamePropertyName = 'email'
grails.plugin.springsecurity.userLookup.enabledPropertyName = 'isActive'
grails.plugin.springsecurity.userLookup.accountExpiredPropertyName = 'isBlocked'
grails.plugin.springsecurity.userLookup.accountLockedPropertyName = 'isBlocked'
grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'com.jane.UserRole'
grails.plugin.springsecurity.authority.className = 'com.jane.Role'
我可以很好地创建用户,验证他们是否在数据库中,并验证一次调用了encodePassword。但是,每次尝试登录时,都会出现以下错误:
Sorry, we were not able to find a user with that username and password.
这是创建用户的服务方法:
User createTeamLeader( String name, String email, String password, Boolean isAgreeTerms, Integer productTierId ) {
User user = new User( name: name, email: email, password: password, isAgreeTerms: isAgreeTerms, isActive: true)
UserProductTier userProductTier = new UserProductTier( productTierId: productTierId )
user.addToUserProductTier( userProductTier )
user.save()
UserRole.create( user, Role.findByAuthority('ROLE_USER'), true )
UserRole.create( user, Role.findByAuthority('ROLE_LEAD'), true )
user
}
最佳答案
通常,添加调试日志记录会有所帮助,因为Spring Security在调试级别记录了大量日志:
log4j = {
...
debug 'org.springframework.security'
}
在这种情况下,它没有显示问题,因此我添加了一个事件侦听器,以查看故障事件中是否包含信息:
grails.plugin.springsecurity.useSecurityEventListener = true
grails.plugin.springsecurity.onAbstractAuthenticationFailureEvent = { e, appCtx ->
println "\nERROR auth failed for user $e.authentication.name: $e.exception.message\n"
}
显示如下:
ERROR auth failed for user ernie@ss.com: No such property: authorities for class: com.jane.User
在类中进行更改时,您删除了原始版本中的
getAuthorities
方法,并且UserDetailsService
使用该方法确定身份验证期间的授予角色。将其重新添加即可正常工作:Set<Role> getAuthorities() {
UserRole.findAllByUser(this).collect { it.role } as Set
}
关于grails - 使用指定的用户名登录int时获得 'Sorry, we were not able to find a user with that username and password.'消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20462446/