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

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

Spring OAuth2 pass Client-Id to get UserInfoUri

问题

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

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

Authorization: bearer token
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:

Authorization: bearer token
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

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

private CompletableFuture<String> getTokenFromAuthServer() {
    return CompletableFuture.supplyAsync(() -> {
        String authTokenEndpoint = "http://127.0.0.1:8080/auth-server/oauth/token";

        String credentials = OAUTH_CLIENT_USERNAME + ":" + OAUTH_CLIENT_PASSWORD;
        String encodedCredentials = new String(Base64.encodeBase64(credentials.getBytes()));
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        headers.add("Authorization", "Basic " + encodedCredentials);

        HttpEntity<String> request = new HttpEntity<String>(headers);
        String access_token_url = authTokenEndpoint;
        access_token_url += "?grant_type=" + OAUTH_GRANT_TYPE;

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.exchange(access_token_url, HttpMethod.POST, 
                request, String.class);

        Map<String, String> map = new HashMap<String, String>();
        ObjectMapper mapper = new ObjectMapper();
        try {
            map = mapper.readValue(response.getBody(), Map.class);
            return map.get("access_token");	
        } catch (IOException e1) {
            log.error("getTokenFromAuthServer: Exception.");
            e1.printStackTrace();
        }
    });				
}

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.

private CompletableFuture&lt;String&gt; getTokenFromAuthServer() {
	return CompletableFuture.supplyAsync(() -&gt; {
		String authTokenEndpoint = &quot;http://127.0.0.1:8080/auth-server/oauth/token&quot;;
		
		***String credentials = OAUTH_CLIENT_USERNAME + &quot;:&quot; + OAUTH_CLIENT_PASSWORD;
		String encodedCredentials = new String(Base64.encodeBase64(credentials.getBytes()));
		HttpHeaders headers = new HttpHeaders();
		headers.add(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
		headers.add(&quot;Authorization&quot;, &quot;Basic &quot; + encodedCredentials);***
		
		HttpEntity&lt;String&gt; request = new HttpEntity&lt;String&gt;(headers);
		String access_token_url = authTokenEndpoint;
		access_token_url += &quot;?grant_type=&quot; + OAUTH_GRANT_TYPE;

		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity&lt;String&gt; response = restTemplate.exchange(access_token_url, HttpMethod.POST, 
				request, String.class);

		Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
		ObjectMapper mapper = new ObjectMapper();
		try {
			map = mapper.readValue(response.getBody(), Map.class);
			return map.get(&quot;access_token&quot;);	
		} catch (IOException e1) {
			log.error(&quot;getTokenFromAuthServer: Exception.&quot;);
			e1.printStackTrace();
		}
	});				
}

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:

确定