安装Spring Security Core作为插件然后快速启动
这是我的用户域类
package auth
class User {
def springSecurityService
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static mapping = {
// password is a keyword in some sql dialects, so quote with backticks
// password is stored as 44-char base64 hashed value
password column: '`password`', length: 64
}
static constraints = {
username blank: false, size: 1..50, unique: true
password blank: false, size: 8..100
}
Set getAuthorities() {
UserRole.findAllByUser(this).collect { it.role } as Set
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected encodePassword() {
password = springSecurityService.encodePassword(password, username)
}
}
我的boostrap.groovy是
class BootStrap {
def init = { servletContext ->
auth.User james = new auth.User(username: 'test', enabled: true, password: 'password')
james.save()
if (james.hasErrors())
{
println("he has errors")
}
println("we made it! ")
}
def destroy = {
}
}
但是当我登录时,它一直说“抱歉,我们无法找到使用该用户名和密码的用户。”有什么想法吗?
答案 0 :(得分:2)
这是因为您在编码密码时使用salt。
password = springSecurityService.encodePassword(password, username)
我不知道腌制,因此无法引导你。
但是,如果您在没有上瘾的情况下对密码进行编码,那么您的代码可以正常工作,只需在编码密码时删除用户名,请尝试此操作
password = springSecurityService.encodePassword(password)
希望这有帮助。
答案 1 :(得分:1)
如果您在BootStrap.groovy
中创建用户,请尝试更改此内容:
def adminUser = User.findByUsername('admin') ?: new User(
username: 'admin',
password: springSecurityService.encodePassword('admin'),
enabled: true).save(failOnError: true)
到此:
def adminUser = User.findByUsername('admin') ?: new User(
username: 'admin',
password: 'admin',
enabled: true).save(failOnError: true)
问题是您使用的是编码密码两次,一次在域中,一次在构造函数的参数中。
答案 2 :(得分:0)
您能否验证用户是否实际被引导到数据库中?
如果是这样,我遇到了类似的问题,Tomcat错误地缓存了一些数据。
这是我做的:
之后,它运作良好。
如果有帮助,请告诉我。
答案 3 :(得分:0)
此外,自从我从头开始构建Grails网站已经有一段时间了,但我想我记得有一些在线说明存在问题。 SpringSecurity可能会为您编码密码,因此当您执行此操作时,它将获得 双重编码 。
尝试删除编码密码的行。