Grails, spring-security-core plugin:使用email登录

[b]1. Implement the first requirement – Add an email address property to the user domain[/b]

This is really simple, just add the property to the domain class :)

package org.customauth

class CustomUser {

String username
String password
String email
boolean enabled
boolean accountExpired

// *******
// the rest of the generated class contents
// *******
}


[b]2. Implement the second requirement – Authenticate using the username or the email[/b]

In order to implement this requirement, I will have to implement a new UserDetailsService


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)
}
}
}


[b]3. With the custom service in place, I need to register it in grails-app/conf/spring/resources.groovy by adding [/b]

beans = {
userDetailsService(org.customauth.CustomUserDetailsService)
}



Reference: [url]http://omarello.com/2010/09/grails-customize-authentication/[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值