Spring OAuth2将Client-Id传递以获取UserInfoUri

huangapple go评论85阅读模式
英文:

Spring OAuth2 pass Client-Id to get UserInfoUri

问题

我正在尝试使用Spring OAuth2对用户进行身份验证;然而服务器要求将客户端ID作为头部 client-id 以便获取用户信息。

换句话说,请求用户信息端点需要以下头部:

  1. Authorization: bearer token
  2. Client-ID: myClientId

我该如何使Spring在请求中添加 client-id 头部,以便从服务器获取用户信息?

英文:

I'm trying to authenticate users with Spring OAuth2; however the server requires client id as a header client-id in order to get user info.

In other words request to userinfo endpoint requires the following headers:

  1. Authorization: bearer token
  2. Client-ID: myClientId

How can I make Spring add the client-id header to the request in order to get user info from server?

答案1

得分: 1

这是一些示例代码,展示了如何使用客户端凭证授权来检索访问令牌,并演示了如何将客户端凭证设置在授权标头中。

  1. private CompletableFuture<String> getTokenFromAuthServer() {
  2. return CompletableFuture.supplyAsync(() -> {
  3. String authTokenEndpoint = "http://127.0.0.1:8080/auth-server/oauth/token";
  4. String credentials = OAUTH_CLIENT_USERNAME + ":" + OAUTH_CLIENT_PASSWORD;
  5. String encodedCredentials = new String(Base64.encodeBase64(credentials.getBytes()));
  6. HttpHeaders headers = new HttpHeaders();
  7. headers.add("Content-Type", "application/x-www-form-urlencoded");
  8. headers.add("Authorization", "Basic " + encodedCredentials);
  9. HttpEntity<String> request = new HttpEntity<String>(headers);
  10. String access_token_url = authTokenEndpoint;
  11. access_token_url += "?grant_type=" + OAUTH_GRANT_TYPE;
  12. RestTemplate restTemplate = new RestTemplate();
  13. ResponseEntity<String> response = restTemplate.exchange(access_token_url, HttpMethod.POST,
  14. request, String.class);
  15. Map<String, String> map = new HashMap<String, String>();
  16. ObjectMapper mapper = new ObjectMapper();
  17. try {
  18. map = mapper.readValue(response.getBody(), Map.class);
  19. return map.get("access_token");
  20. } catch (IOException e1) {
  21. log.error("getTokenFromAuthServer: Exception.");
  22. e1.printStackTrace();
  23. }
  24. });
  25. }

Base64 API来自于Apache Commons Lang3库。

英文:

Here's some sample code showing how I retrieved an access token using the client credentails grant. It shows how you can set the client credentials in the Authorization header.

  1. private CompletableFuture&lt;String&gt; getTokenFromAuthServer() {
  2. return CompletableFuture.supplyAsync(() -&gt; {
  3. String authTokenEndpoint = &quot;http://127.0.0.1:8080/auth-server/oauth/token&quot;;
  4. ***String credentials = OAUTH_CLIENT_USERNAME + &quot;:&quot; + OAUTH_CLIENT_PASSWORD;
  5. String encodedCredentials = new String(Base64.encodeBase64(credentials.getBytes()));
  6. HttpHeaders headers = new HttpHeaders();
  7. headers.add(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
  8. headers.add(&quot;Authorization&quot;, &quot;Basic &quot; + encodedCredentials);***
  9. HttpEntity&lt;String&gt; request = new HttpEntity&lt;String&gt;(headers);
  10. String access_token_url = authTokenEndpoint;
  11. access_token_url += &quot;?grant_type=&quot; + OAUTH_GRANT_TYPE;
  12. RestTemplate restTemplate = new RestTemplate();
  13. ResponseEntity&lt;String&gt; response = restTemplate.exchange(access_token_url, HttpMethod.POST,
  14. request, String.class);
  15. Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  16. ObjectMapper mapper = new ObjectMapper();
  17. try {
  18. map = mapper.readValue(response.getBody(), Map.class);
  19. return map.get(&quot;access_token&quot;);
  20. } catch (IOException e1) {
  21. log.error(&quot;getTokenFromAuthServer: Exception.&quot;);
  22. e1.printStackTrace();
  23. }
  24. });
  25. }

The Base64 api is from the apache commons-lang3 library.

huangapple
  • 本文由 发表于 2020年10月6日 11:19:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64218875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定