簡體   English   中英

從Spring后端(auth0)調用/ userinfo

[英]Call /userinfo from spring backend (auth0)

我正在嘗試從我的Spring后端api進行此調用 我已經有客戶端發送給我的訪問令牌。 該代碼的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>

來自客戶端的訪問令牌中的詳細信息(我刪除了一些詳細信息,然后將其替換為方括號):

~~~~~~~~~ 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調用(加上訪問令牌作為授權承載頭),而無需特殊的庫。

這里是使用Node.js從服務器端進行此操作的示例。

使用OkHttp的基本Java大綱為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM