gpt4 book ai didi

grails - 在 grails spring security rest 中获取 'Cannot invoke method loadUserByUsername() on null object'

转载 作者:行者123 更新时间:2023-12-02 15:45:57 26 4
gpt4 key购买 nike

我一直在尝试从这里实现 Facebook OAuth:
http://alvarosanchez.github.io/grails-spring-security-rest/latest/docs/index.html#_delegating_authentication_to_oauth_providers

我能够集成 OAuth 并从 Facebook 获取访问 token ,但我在实现自定义 OAuthUserDetailsS​​ervice 时遇到了问题。我创建了
定制服务:

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 名称 oauthUserDetailsS​​ervice 定义它。这就是我的 resources.groovy 文件的样子:

资源.groovy :
import hungr.FacebookOauthUserDetailsService
import hungr.UserPasswordEncoderListener

beans =
{
userPasswordEncoderListener(UserPasswordEncoderListener)
oauthUserDetailsService(FacebookOauthUserDetailsService)
}

我试图在此处引用此文档以了解如何定义 bean:
https://docs.grails.org/latest/guide/spring.html
但这对我也没有用。
我究竟做错了什么?

最佳答案

GrailsUserDetailsService是接口(interface)基本驻留在org.springframework.security.core.userdetails包裹。您可以实现 loadUserByUsername在您的服务(类)中使用上述服务(接口(interface))的方法并编写代码(实现)以获取所需的数据/用户详细信息,您可以将用户详细信息作为对象/列表/映射获取任何您想要的格式,您可以在那里找到数据通过给定用户名的 GORM finder 方法并返回该数据。
下面是示例,

服务代码:

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

现在,您可以在 Controller 中注入(inject)上述服务并调用如下方法,
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
}
}

您的 resources.groovy 将如下所示,
beans = {
userDetailsService(Your_service_name)
}

Note : Clean and restart your application after this change.

关于grails - 在 grails spring security rest 中获取 'Cannot invoke method loadUserByUsername() on null object',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47969903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com