我一直试图从这里实施Facebook OAuth: http://alvarosanchez.github.io/grails-spring-security-rest/latest/docs/index.html#_delegating_authentication_to_oauth_providers
我能够集成OAuth并从Facebook获取访问令牌,但我遇到了实施自定义OAuthUserDetailsService的问题。我创造了 自定义服务:
FacebookOauthUserDetails.groovy
class FacebookOauthUserDetailsService implements OauthUserDetailsService{
@Delegate
UserDetailsService userDetailsService
UserDetailsChecker preAuthenticationChecks
@Override
OauthUser loadUserByUserProfile(CommonProfile userProfile, Collection<GrantedAuthority> defaultRoles) throws UsernameNotFoundException {
UserDetails userDetails
OauthUser oauthUser
println("adss")
try {
println("Trying to fetch user details for user profile: ${userProfile}")
userDetails = userDetailsService.loadUserByUsername(userProfile.id)
log.debug("Checking user details with ${preAuthenticationChecks.class.name}")
preAuthenticationChecks?.check(userDetails)
Collection<GrantedAuthority> allRoles = userDetails.authorities + defaultRoles
oauthUser = new OauthUser(userDetails.username, userDetails.password, allRoles, userProfile)
} catch (UsernameNotFoundException unfe) {
println("User not found. Creating a new one with default roles: ${defaultRoles}")
oauthUser = new OauthUser(userProfile.id, 'N/A', defaultRoles, userProfile)
}
return oauthUser
}
}
官方文档提到,为了覆盖默认行为,需要在resources.groovy中使用bean名称oauthUserDetailsService定义它。这就是我的resources.groovy文件的样子:
resources.groovy :
import hungr.FacebookOauthUserDetailsService
import hungr.UserPasswordEncoderListener
beans =
{
userPasswordEncoderListener(UserPasswordEncoderListener)
oauthUserDetailsService(FacebookOauthUserDetailsService)
}
我试图在这里引用如何定义bean的文档: https://docs.grails.org/latest/guide/spring.html 但它对我来说也没有用。 我做错了什么?
答案 0 :(得分:0)
q.data$number.of.a <- lengths(gregexpr('a', q.data$string))
# number string number.of.a`
#1 1 greatgreat 2`
#2 2 magic 1`
#3 3 not 0`
接口基本上位于GrailsUserDetailsService
包中。您可以在服务(类)中实现上述服务(接口)的org.springframework.security.core.userdetails
方法并编写代码(实现)以获取所需的数据/用户详细信息,您可以将用户详细信息作为对象/列表/映射获取您想要的格式,您可以通过GORM finder方法通过给定的用户名找到数据并返回该数据。
以下是示例示例
服务代码:
loadUserByUsername
现在,您可以在控制器中注入上述服务,并按如下方式调用方法,
class Your_service_name implements GrailsUserDetailsService {
Map loadUserByUsername(String username) throws UsernameNotFoundException {
Map userDetails = [:]
User.withTransaction { status ->
User user = User.findByUsername(username, ["readOnly": true])
if (!user) throw new UsernameNotFoundException('User not found', username)
// you can also return the whole user object here instead of map keep method return type as UserDetails and return user object, you also return the user roles along with other details in Map.
userDetails["username"] = user.username
userDetails["emailAddress"] = user.emailAddress
userDetails["userFullName"] = user.userFullName
return userDetails
}
}
您的resources.groovy将如下,
class YourController{
Your_service_name myService // injected above created service which contains the loadUserByUsername method implementation
def getUserDetails(){
Map userdetails = myService.loadUserByUsername("username_to_find")
println userdetails
}
}
注意:在此更改后清理并重新启动应用程序。