Grails spring安全性不工作:本地身份验证(无LDAP)
我正在尝试使用我在引导文件中创建的数据库用户登录,但我不断收到错误:抱歉,我们无法找到具有该用户名和密码的用户 我查阅了各种使用spring 2.4插件的教程,我真的不知道我遗漏了什么 请帮帮我…我需要尽快解决这个问题:S 我的模型是: 用户: } 用户角色:Grails spring安全性不工作:本地身份验证(无LDAP),grails,spring-security,ldap,Grails,Spring Security,Ldap,我正在尝试使用我在引导文件中创建的数据库用户登录,但我不断收到错误:抱歉,我们无法找到具有该用户名和密码的用户 我查阅了各种使用spring 2.4插件的教程,我真的不知道我遗漏了什么 请帮帮我…我需要尽快解决这个问题:S 我的模型是: 用户: } 用户角色: class UserRole implements Serializable { private static final long serialVersionUID = 1 User user Role role boolean
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='co.edu.uelbosque.unbsqueinscripciones.modelows.User'
grails.plugin.springsecurity.userLookup.authorityJoinClassName='co.edu.uelbosque.unbsqueinscripciones.modelos.UserRole'
grails.plugin.springsecurity.authority.className='co.edu.uelbosque.unbsqueinscripciones.modelos.Role'
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):
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
}
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 = {
}
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"
}