2017-12-31 128 views
1

저는 Spring Security OAuth를 사용하여 Github에서 권한을 부여하고 Principal 객체를 페이지에 반환 할 준비가되었습니다. 교장, 다음과 같은 출력을 얻기 위해 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" 
} 

그러나, 나는 내 자바 프로그램에서 획득 한 교장은 getUserAuthentication 방법과 유사한 방법이 없었다. 대신, 실제로 이름이 아니라 내부의 세부 정보를 얻으려고했습니다. 우리는 자바 프로그램에서 얻을 수있는 방법

그래서 내가 물어보고 싶은 :

"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은별로 교장으로 조립 생각하지만 어디서 전체 조직을 찾지 못했습니다. 스프링 프레임 워크는 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(); 
    } 
}