gpt4 book ai didi

java - 如何获取principal.userAuthentication.details?

转载 作者:行者123 更新时间:2023-11-30 06:21:33 33 4
gpt4 key购买 nike

我正在使用 Spring Security OAuth 从 Github 进行授权,并准备将委托(delegate)对象返回到页面。使用Ajax获取Principal时,输出如下:

{
"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": "mountain@cciradih.top",
"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程序中获取的Principal并没有类似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": "mountain@cciradih.top",
"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是由一个类组装成Principal的,但是我没有从TRACE中找到整个org.springframework是在哪里组装的。我的代码如下:

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

最佳答案

更改 get() 方法以包含 Oauth2Authentication 作为参数。

此外,您还可以将 oauth2Authentication.getDetails() 转换为 Oauth2AuthenticationDetails 以从 jwt token 中获取更多信息

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

关于java - 如何获取principal.userAuthentication.details?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48041066/

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