2017-12-31 39 views
1

私はSpring Security OAuthを使用してGithubから承認し、プリンシパルオブジェクトをページに返す準備ができています。校長、次のような出力を得るために、Ajaxを使用時:principal.userAuthentication.detailsの取得方法は?

{ 
"authorities": [ 
    { 
     "authority": "ROLE_USER" 
    } 
], 
"details": { 
    "remoteAddress": "127.0.0.1", 
    "sessionId": "B0FAAFBFEBCEE85852963FD2EDB49142", 
    "tokenValue": "bcc006f486f8788728d12167fccdee8c8e35fdb2", 
    "tokenType": "bearer", 
    "decodedDetails": null 
}, 
"authenticated": true, 
"userAuthentication": { 
    "authorities": [ 
     { 
      "authority": "ROLE_USER" 
     } 
    ], 
    "details": { 
     "login": "cciradih", 
     "id": 22651384, 
     "avatar_url": "https://avatars0.githubusercontent.com/u/22651384?v=4", 
     "gravatar_id": "", 
     "url": "https://api.github.com/users/cciradih", 
     "html_url": "https://github.com/cciradih", 
     "followers_url": "https://api.github.com/users/cciradih/followers", 
     "following_url": "https://api.github.com/users/cciradih/following{/other_user}", 
     "gists_url": "https://api.github.com/users/cciradih/gists{/gist_id}", 
     "starred_url": "https://api.github.com/users/cciradih/starred{/owner}{/repo}", 
     "subscriptions_url": "https://api.github.com/users/cciradih/subscriptions", 
     "organizations_url": "https://api.github.com/users/cciradih/orgs", 
     "repos_url": "https://api.github.com/users/cciradih/repos", 
     "events_url": "https://api.github.com/users/cciradih/events{/privacy}", 
     "received_events_url": "https://api.github.com/users/cciradih/received_events", 
     "type": "User", 
     "site_admin": false, 
     "name": "Cciradih", 
     "company": null, 
     "blog": "https://www.cciradih.top", 
     "location": "China", 
     "email": "[email protected]", 
     "hireable": true, 
     "bio": "Better to run than curse the road.", 
     "public_repos": 6, 
     "public_gists": 0, 
     "followers": 2, 
     "following": 0, 
     "created_at": "2016-10-06T03:13:28Z", 
     "updated_at": "2017-12-30T06:04:47Z" 
    }, 
    "authenticated": true, 
    "principal": "cciradih", 
    "credentials": "N/A", 
    "name": "cciradih" 
}, 
"clientOnly": false, 
"principal": "cciradih", 
"credentials": "", 
"oauth2Request": { 
    "clientId": "6402963959adfc602705", 
    "scope": [], 
    "requestParameters": {}, 
    "resourceIds": [], 
    "authorities": [], 
    "approved": true, 
    "refresh": false, 
    "redirectUri": null, 
    "responseTypes": [], 
    "extensions": {}, 
    "grantType": null, 
    "refreshTokenRequest": null 
}, 
"name": "cciradih" 
} 

しかし、私は私のJavaプログラムで取得した校長はgetUserAuthenticationと同様の方法を持っていませんでした。代わりに、私は実際に名前だけでなく、内部の詳細を取得しようとしました。

は、だから私たちはJavaプログラムから取得することができますか、お聞きしたい:

"details": { 
    "login": "cciradih", 
    "id": 22651384, 
    "avatar_url": "https://avatars0.githubusercontent.com/u/22651384?v=4", 
    "gravatar_id": "", 
    "url": "https://api.github.com/users/cciradih", 
    "html_url": "https://github.com/cciradih", 
    "followers_url": "https://api.github.com/users/cciradih/followers", 
    "following_url": "https://api.github.com/users/cciradih/following{/other_user}", 
    "gists_url": "https://api.github.com/users/cciradih/gists{/gist_id}", 
    "starred_url": "https://api.github.com/users/cciradih/starred{/owner}{/repo}", 
    "subscriptions_url": "https://api.github.com/users/cciradih/subscriptions", 
    "organizations_url": "https://api.github.com/users/cciradih/orgs", 
    "repos_url": "https://api.github.com/users/cciradih/repos", 
    "events_url": "https://api.github.com/users/cciradih/events{/privacy}", 
    "received_events_url": "https://api.github.com/users/cciradih/received_events", 
    "type": "User", 
    "site_admin": false, 
    "name": "Cciradih", 
    "company": null, 
    "blog": "https://www.cciradih.top", 
    "location": "China", 
    "email": "[email protected]", 
    "hireable": true, 
    "bio": "Better to run than curse the road.", 
    "public_repos": 6, 
    "public_gists": 0, 
    "followers": 2, 
    "following": 0, 
    "created_at": "2016-10-06T03:13:28Z", 
    "updated_at": "2017-12-30T06:04:47Z" 
} 

私は、このJSONはクラスによって主に組み立てていると思いますが、私はどこに全体の組織を見つけることができませんでした。 springframeworkはTRACEから組み立てられました。

@RestController 
@RequestMapping("/api/user") 
public class UserController { 
    private WebAuthenticationDetails webAuthenticationDetails; 
    @GetMapping 
    public Principal get(Principal principal) { 
     return principal; 
    } 
} 

答えて

1

変更get()メソッドは、パラメータとしてOauth2Authenticationが含まれるように次のように私のコードです。

また、あなたは外の詳細情報を取得するためにoauth2Authentication.getDetails()Oauth2AuthenticationDetailsにキャストすることができ、あなたのJWTトークン

@RestController 
@RequestMapping("/api/user") 
public class UserController { 
    private WebAuthenticationDetails webAuthenticationDetails; 
    @GetMapping 
    public Principal get(Oauth2Authentication authentication) { 
     String jwtToken = ((OAuth2AuthenticationDetails) oauth2Authentication.getDetails()).getTokenValue(); 
     // do stuff with jwtToken 
     return authentication.getPrincipal(); 
    } 
} 
関連する問題