我正在尝试使用我在Bootstrap文件中创建的数据库用户登录,但我一直收到错误:抱歉,我们无法找到具有该用户名和密码的用户。
我已经检查了使用Spring插件的各种教程,我真的不知道我错过了什么。
请帮助我......我需要尽快解决这个问题:S
我的模特是:
用户:
class User {
transient springSecurityService
String username
String password
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static transients = ['springSecurityService']
static constraints = {
username blank: false, unique: true
password blank: false
}
static mapping = {
password column: '`password`'
}
Set<Role> getAuthorities() {
UserRole.findAllByUser(this).collect { it.role } as Set
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
String toString()
{
return username
}
protected void encodePassword() {
password = springSecurityService?.passwordEncoder springSecurityService.encodePassword(password) : password
}
}
作用:
class Role {
String authority
static mapping =
{
cache true
}
static constraints =
{
authority blank: false, unique: true
}
}
的UserRole:
class UserRole implements Serializable {
private static final long serialVersionUID = 1
User user
Role role
boolean equals(other) {
if (!(other instanceof UserRole)) {
return false
}
other.user?.id == user?.id &&
other.role?.id == role?.id
}
int hashCode() {
def builder = new HashCodeBuilder()
if (user) builder.append(user.id)
if (role) builder.append(role.id)
builder.toHashCode()
}
static UserRole get(long userId, long roleId) {
UserRole.where {
user == User.load(userId) &&
role == Role.load(roleId)
}.get()
}
static boolean exists(long userId, long roleId) {
UserRole.where {
user == User.load(userId) &&
role == Role.load(roleId)
}.count() > 0
}
static UserRole create(User user, Role role, boolean flush = false) {
def instance = new UserRole(user: user, role: role)
instance.save(flush: flush, insert: true)
instance
}
static boolean remove(User u, Role r, boolean flush = false) {
if (u == null || r == null) return false
int rowCount = UserRole.where {
user == User.load(u.id) &&
role == Role.load(r.id)
}.deleteAll()
if (flush) { UserRole.withSession { it.flush() } }
rowCount > 0
}
static void removeAll(User u, boolean flush = false) {
if (u == null) return
UserRole.where {
user == User.load(u.id)
}.deleteAll()
if (flush) { UserRole.withSession { it.flush() } }
}
static void removeAll(Role r, boolean flush = false) {
if (r == null) return
UserRole.where {
role == Role.load(r.id)
}.deleteAll()
if (flush) { UserRole.withSession { it.flush() } }
}
static constraints = {
role validator: { Role r, UserRole ur ->
if (ur.user == null) return
boolean existing = false
UserRole.withNewSession {
existing = UserRole.exists(ur.user.id, r.id)
}
if (existing) {
return 'userRole.exists'
}
}
}
static mapping = {
id composite: ['role', 'user']
version false
}
}
配置文件:
grails.plugin.springsecurity.userLookup.userDomainClassName =&#39; co.edu.uelbosque.UnBosqueInscripciones.modelos.User&#39; grails.plugin.springsecurity.userLookup.authorityJoinClassName =&#39; co.edu.uelbosque.UnBosqueInscripciones.modelos.UserRole&#39; grails.plugin.springsecurity.authority.className =&#39; co.edu.uelbosque.UnBosqueInscripciones.modelos.Role&#39;
Boostrap文件:
class BootStrap {
def init = { servletContext ->
def rol1 = new Role(authority:'ROLE_USER')
def rol2 = new Role(authority:'ROLE_ADMIN')
rol1.save(flush:true)
rol2.save(flush:true)
def us2 = new User(username:'testUser', password:'1234')
us2.save(flush:true)
new UserRole(user:us2, role:rol2).save(flush:true)
}
def destroy = {
}
}
使用的插件(安全插件2.0.RC):
plugins {
// plugins for the build system only
build ":tomcat:7.0.55"
// plugins for the compile step
compile ":scaffolding:2.1.2"
compile ':cache:1.1.8'
compile ":asset-pipeline:1.9.9"
// plugins needed at runtime but not for compilation
runtime ":hibernate4:4.3.6.1" // or ":hibernate:3.6.10.18"
runtime ":database-migration:1.4.0"
runtime ":jquery:1.11.1"
//Plugins adicionales
compile ":mysql-connectorj:5.1.22.1"
compile ":spring-security-ldap:2.0-RC4"
compile ":simple-captcha:1.0.0"
// plugins needed at runtime but not for compilation
runtime ":hibernate4:4.3.6.1" // or ":hibernate:3.6.10.18"
runtime ":database-migration:1.4.0"
runtime ":jquery:1.11.1"
// Uncomment these to enable additional asset-pipeline capabilities
//compile ":sass-asset-pipeline:1.9.0"
//compile ":less-asset-pipeline:1.10.0"
//compile ":coffee-asset-pipeline:1.8.0"
//compile ":handlebars-asset-pipeline:1.3.0.3"
}