Keycloak API使用admin用户



我正在使用keycloak API来访问离线用户的会话;我注意到一个奇怪的行为,因此我的问题:

。当我使用postman时,我使用以下url获得访问令牌:http://localhost:8080/realms/master/protocol/openid-connect/token

b。从上面,我在postman中使用上述令牌来检索脱机会话:

http://localhost: 8080/admin/领域/主/客户/5729288 b - c789 45 - ac - 8915 da32b7b9fe49/offline-sessions

其中'5729288b-c789-45ac-8915-da32b7b9fe49'为admin-cli ID;用户名和密码都是默认的admin用户,客户端是'admin-cli'

一切工作正常邮差,我能够检索离线会话。然而,当我使用springboot web客户端对Keycloak API做同样的事情时,我得到403 Forbidden

。从下面获取令牌:

private String getToken(){

MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("username", username);
map.add("password", password);
map.add("client_id", clientId);
map.add("grant_type", grantType);
map.add("scope", "openid");
ResponseEntity<LoginResponse> loginResponse = webclient.post()
.uri(uriBuilder -> UriBuilder.fromUri(tokenEndpoint).build())
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromFormData(map))
.retrieve()
.toEntity(LoginResponse.class)
.block();
return loginResponse.getBody().getAccess_token();
}

b。尝试使用上面的访问令牌

检索离线会话
public UserSessionRepresentation[] getMasterOfflineSessions(){
UserSessionRepresentation[] response = webclient.get()
.uri(uriBuilder -> UriBuilder.fromUri(offlineSessionsUrl)
.build(cliId))
.headers(h -> h.setBearerAuth(getToken()))
.retrieve()
.bodyToMono(UserSessionRepresentation[].class)
.block();
return response;
}

offlineSessionsUrl is: http://localhost:8080/admin/realms/master/clients/5729288b-c789-45ac-8915-da32b7b9fe49/offline-sessions

5729288b-c789-45ac-8915-da32b7b9fe49:为admin-cli客户端的id

我不明白的是,我可以检索会话在邮差,但我不能这样做使用API和springboot webclient与所有配置相等。

请帮

回答自己的问题;这里的问题是:webclient spring属性

在springboot中,它使用了指向另一个客户机的配置中的定义。为了使它在admin-cli客户端上工作,我必须使用一个干净的web客户端对象,如下面的代码所示:

public UserSessionRepresentation[] getMasterOfflineSessions(){
UserSessionRepresentation[] response = WebClient.create().get()
.uri(uriBuilder -> UriBuilder.fromUri(offlineSessionsUrl)
.build(cliId))
.headers(h -> h.setBearerAuth(getToken()))
.retrieve()
.bodyToMono(UserSessionRepresentation[].class)
.block();
return response;

}

WebClient.create()是我为解决

问题而修改的代码。

最新更新