英文:
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<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();
}
});
}
The Base64 api is from the apache commons-lang3 library.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论