来自spring后端的call / userinfo(auth0)

时间:2018-02-12 12:54:52

标签: javascript java android spring auth0

我试图从我的春天后端api做this call。我已经拥有客户端发给我的访问令牌。这段代码的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"}

1 个答案:

答案 0 :(得分:0)

这只是一个标准https调用(加上将访问令牌添加为授权承载头) - 无需特殊库。

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

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