Grails, spring-security-core plugin:使用email登录
2012-08-14 17:05
585 查看
1. Implement the first requirement – Add an email address property to the user domain
This is really simple, just add the property to the domain class
Groovy代码
package org.customauth
class CustomUser {
String username
String password
String email
boolean enabled
boolean accountExpired
// *******
// the rest of the generated class contents
// *******
}
2. Implement the second requirement – Authenticate using the username or the email
In order to implement this requirement, I will have to implement a new UserDetailsService
Groovy代码
package org.customauth
import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUser
import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUserDetailsService
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
import org.springframework.security.core.authority.GrantedAuthorityImpl
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UsernameNotFoundException
class CustomUserDetailsService implements GrailsUserDetailsService {
static final List NO_ROLES = [new GrantedAuthorityImpl(SpringSecurityUtils.NO_ROLE)]
UserDetails loadUserByUsername(String username, boolean loadRoles)
throws UsernameNotFoundException {
return loadUserByUsername(username)
}
UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
CustomUser.withTransaction { status ->
CustomUser user = CustomUser.findByUsernameOrEmail(username, username)
if (!user)
throw new UsernameNotFoundException('User not found', username)
def authorities = user.authorities.collect {
new GrantedAuthorityImpl(it.authority)}
return new GrailsUser(user.username, user.password, user.enabled,
!user.accountExpired, !user.passwordExpired, !user.accountLocked,
authorities ?: NO_ROLES, user.id)
}
}
}
3. With the custom service in place, I need to register it in grails-app/conf/spring/resources.groovy by adding
Groovy代码
beans = {
userDetailsService(org.customauth.CustomUserDetailsService)
}
原文:/article/3766202.html
This is really simple, just add the property to the domain class
Groovy代码
package org.customauth
class CustomUser {
String username
String password
String email
boolean enabled
boolean accountExpired
// *******
// the rest of the generated class contents
// *******
}
2. Implement the second requirement – Authenticate using the username or the email
In order to implement this requirement, I will have to implement a new UserDetailsService
Groovy代码
package org.customauth
import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUser
import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUserDetailsService
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
import org.springframework.security.core.authority.GrantedAuthorityImpl
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UsernameNotFoundException
class CustomUserDetailsService implements GrailsUserDetailsService {
static final List NO_ROLES = [new GrantedAuthorityImpl(SpringSecurityUtils.NO_ROLE)]
UserDetails loadUserByUsername(String username, boolean loadRoles)
throws UsernameNotFoundException {
return loadUserByUsername(username)
}
UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
CustomUser.withTransaction { status ->
CustomUser user = CustomUser.findByUsernameOrEmail(username, username)
if (!user)
throw new UsernameNotFoundException('User not found', username)
def authorities = user.authorities.collect {
new GrantedAuthorityImpl(it.authority)}
return new GrailsUser(user.username, user.password, user.enabled,
!user.accountExpired, !user.passwordExpired, !user.accountLocked,
authorities ?: NO_ROLES, user.id)
}
}
}
3. With the custom service in place, I need to register it in grails-app/conf/spring/resources.groovy by adding
Groovy代码
beans = {
userDetailsService(org.customauth.CustomUserDetailsService)
}
原文:/article/3766202.html
相关文章推荐
- SpringBoot + Spring Security 基本使用及个性化登录配置详解
- [安全] 使用 SpringBoot + SpringSecurity 做登录认证
- grails 之 Spring Security Core Plugin 使用
- SSO单点登录Spring-Security+CAS+使用手册.doc
- webloigc 使用 spring-loaded 报错,java.lang.SecurityException: 无法定位登录配置
- 使用Spring-Security进行登录控制的session问题
- Spring boot 使用restful风格的spring security login
- 使用Spring发送Email---只包括主题和一段信息的邮件
- SpringBoot使用Shiro验证登录笔记
- Spring Security ACL使用Oracle数据库的配置与数据库脚本
- 3.Spring Boot + Security初步使用(覆盖配置的两种方式 配置类和XML注入以自定义页面为例)
- Grails 之 Acegi Plugin 转移到Spring Security Core Plugin
- spring boot+spring security+thymeleaf在页面上判断用户是否登录
- 使用 Spring4 + CXF3 + WS-Security 开发 WebService
- Spring boot + LayIM + t-io 使用thyemleaf模板和Shiro实现登录
- spring-security认证过程的分析及自定义登录
- grails之spring-security-core自定义加密方式
- spring-security 多类型用户登录+登录多参数验证
- springSecurity+cas 单点登录
- spring boot中使用security样例抛异常,Circular view path跳转失败