2017-12-31 82 views
1

Ich verwende Spring Security OAuth, um von Github zu autorisieren, und bin bereit, ein Principal-Objekt auf die Seite zurückzugeben. Wenn Ajax mit dem Auftraggeber zu erhalten, die folgende Ausgabe:Wie bekomme ich die 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" 
} 

Allerdings hat der Auftraggeber die ich in meinem Java-Programm erworben kein Verfahren ähnlich wie getUserAuthentication haben. Stattdessen habe ich versucht, die Details zu erfassen, nicht nur den Namen.

So möchte ich fragen, wie können wir aus dem Java-Programm zu erhalten:

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

Ich denke, das json in einen Haupt durch eine Klasse zusammengesetzt ist, aber ich habe nicht herausfinden, wo die gesamte org. Springframework wurde von TRACE zusammengebaut. Mein Code ist wie folgt:

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

Antwort

1

Ändern der get() Methode Oauth2Authentication als Parameter enthalten.

Sie können aber auch die oauth2Authentication.getDetails()-Oauth2AuthenticationDetails werfen, um mehr Informationen aus Ihrem jwt Token zu erhalten

@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(); 
    } 
}