2017-12-25 54 views
0
私はここからのFacebookのOAuthを実装しようとしてきた

http://alvarosanchez.github.io/grails-spring-security-rest/latest/docs/index.html#_delegating_authentication_to_oauth_providersGrailsの中で「nullオブジェクトのメソッドloadUserByUsername()を呼び出すことはできません」の取得春のセキュリティ残り

を私はOAuthのを統合し、アクセストークンを取得することができますよ私はカスタムOAuthUserDetailsS​​erviceを実装する問題に直面しています。公式ドキュメントはデフォルトの動作を上書きするために、1つは、Bean名oauthUserDetailsS​​erviceとresources.groovyでそれを定義する必要があると述べて

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

:私は カスタムサービスを作成しました.Thisは私の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

'userDetailsS​​ervice' が適切に注入しない場合があります/解決していない、あなたはgrails3を使用していますか? –

+0

はい私はgrails 3を使用しています。 –

+0

ヌルの問題とサービスが注入されない理由を見つけましたか?ここ –

答えて

0

GrailsUserDetailsServiceは、基本的org.springframework.security.core.userdetailsパッケージに存在するインターフェースです。あなたは、あなたが好きなフォーマットオブジェクト/リスト/マップとしてユーザーの詳細情報を取得することができ、あなたのサービスで上記のサービス(インターフェース)(クラス)のloadUserByUsernameメソッドを実装して、ご希望のデータ/ userdetailsを取得するためのコード(実装)を書くことができます指定されたユーザー名で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 
    } 
} 

さて、あなたはあなたのコントローラで上記のサービスを注入することができますし、以下のようにメソッドを呼び出し、

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

注:この変更の後、アプリケーションをクリーニングして再起動してください。

+0

公式ドキュメント(http://alvarosanchez.github.io/grails-spring-security-rest/latest/docs/index.html#_delegating_authentication_to_oauth_providersは)私は、デフォルトの動作を上書きするために、OAuthUserDetailsS​​erviceインタフェースを実装する必要があると述べています。私がばかげているなら、すみません。私は新しく、グルーヴィー/グライルズの知識はかなり限られています。インターネット上で入手できる限られたリソースだけで私の原因を助けていません。 –

+0

@AdityaGurjar:サンプルコードを使用して回答を更新しましたので、あなたに役立ちます。 –

+0

GrailsUserDetailsS​​erviceを実装するカスタムサービスを作成しました。私はまだ同じ問題に直面しています。私は、resources.groovyファイルでBeanを定義することに何か間違っていると感じています。 –

関連する問題