编辑标题:与实际问题更相关
我正在尝试为我的测试应用程序设置 spring 安全性
我安装了插件,创建了 User 和 Role 类;
把它放到 UrlMappings.groovy 中;
"/login/$action?"(controller: "login")
"/logout/$action?"(controller: "logout")
然后我把一个用户放在 bootstrap 中,如下所示,
import org.project.auth.Role
import org.project.auth.User
import org.project.auth.UserRole;
class BootStrap {
def springSecurityService
def init = { servletContext ->
def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true,flush:true)
def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true,flush:true)
def adminUser = User.findByUsername('admin') ?: new User(
username: 'admin',
password: springSecurityService.encodePassword('admin'),
enabled: true).save(failOnError: true,flush:true)
print User.count()
if (!adminUser.authorities.contains(adminRole)) {
print "TEST"
UserRole.create adminUser, adminRole,true
}
}
def destroy = {
}
}
这个打印 User.count() 返回 1,所以我知道用户已创建,打印“TEST”也有效,所以我知道它进入 if 块但是当我运行服务器时它失败了
Sorry, we were not able to find a user with that username and password.
我使用 Grails 2.0.0.M1 ,您认为这可能是问题吗?
最佳答案
插件1.2版本中的User域类为你加密密码。所以像这样使用 springSecurityService
的旧代码双编码。更改 password: springSecurityService.encodePassword('admin')
至 password: 'admin'
它应该工作。
如果没有,打开调试,你应该会看到一条关于它为什么失败的消息。将此添加到 Config.groovy 的 log4j 块中:
debug 'org.springframework.security'
也为了安全我会改变
if (!adminUser.authorities.contains(adminRole)) {
至 if (!UserRole.findByUserAndRole(adminUser, adminRole)) {
`
关于grails - Spring Security 插件认证失败问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6946418/