【问题标题】:Call /userinfo from spring backend (auth0)从 spring 后端调用 /userinfo (auth0)
【发布时间】:2018-07-22 15:44:06
【问题描述】:

我正在尝试从我的 spring 后端 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"}

【问题讨论】:

    标签: javascript java android spring auth0


    【解决方案1】:

    这只是一个标准的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();
    

    【讨论】:

      猜你喜欢
      • 2018-06-10
      • 2021-09-05
      • 2017-02-12
      • 2017-09-29
      • 2016-11-05
      • 2017-02-27
      • 2016-05-14
      • 2020-11-25
      • 2015-10-09
      相关资源
      最近更新 更多