gpt4 book ai didi

javascript - 从 spring 后端调用/userinfo (auth0)

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:53 24 4
gpt4 key购买 nike

我正在尝试做this call来 self 的 Spring 后端 api。我已经拥有客户端发送给我的访问 token 。此代码的 java 等效项是什么?:

// Script uses auth0.js. See Remarks for details.
<script src="https://cdn.auth0.com/js/auth0/9.0.1/auth0.min.js"></script>
<script type="text/javascript">
// Initialize the Auth0 client
var webAuth = new auth0.WebAuth({
domain: '{domain}',
clientID: '{clientId}'
});

// Parse the URL and extract the access_token
webAuth.parseHash(window.location.hash, function(err, authResult) {
if (err) {
return console.log(err);
}
webAuth.client.userInfo(authResult.accessToken, function(err, user) {
// This method will make a request to the /userinfo endpoint
// and return the user object, which contains the user's information,
// similar to the response below.
});
});
</script>

来自客户端的访问 token 的详细信息(我删除了一些详细信息并用方括号替换它们):

~~~~~~~~~ JWT Header ~~~~~~~
JWT Header : {"typ":"JWT","alg":"RS256","kid":"[kid]"}
~~~~~~~~~ JWT Body ~~~~~~~
JWT Body : {"iss":"https://demo.auth0.com/","sub":"google-oauth2|[my id here]","aud":["[api audience]","https://demo.auth0.com/userinfo"],"iat":[number],"exp":[expiry],"azp":"[azp]","scope":"openid"}

最佳答案

这只是一个标准的 https 调用(加上添加访问 token 作为授权承载 header ) - 不需要特殊的库。

使用 Node.js 从服务器端进行此操作的示例是 here .

基本的 Java 概要(这里使用 OkHttp)是:

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
.url("https://mytenant.auth0.com/userinfo")
.get()
.addHeader("authorization", "Bearer {{access_token}}")
.addHeader("cache-control", "no-cache")
.build();

Response response = client.newCall(request).execute();

关于javascript - 从 spring 后端调用/userinfo (auth0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48747027/

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