首页 > 解决方案 > 无法通过 Keycloak 中的 refreshToken 获取刷新 accessToken

问题描述

我可以通过 Postman 获得刷新 accessToken。但无法在 java 中使用 restTemplate 获取 accessToken。我收到了 400 错误请求。

    public String fetchAccessToken(String refreshToken) throws Exception {

    String url = "https://localhost:8080/auth/realms/sso/protocol/openid-connect/token";

    Map<String, String> map = new HashMap<String, String>();
    map.put("client_id", "keycloak-client");
    map.put("grant_type", "refresh_token");
    map.put("refresh_token", refreshToken);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

    HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(map, headers);
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);

    return response.getBody();
}

标签: javaspring-bootkeycloakkeycloak-services

解决方案


推荐阅读