简体   繁体   English

Keycloak spring - 不正确的 URI

[英]Keycloak spring - incorrect URI

I implemented a KeyCloak client with the following configuration: keycloak configuration我使用以下配置实现了一个 KeyCloak 客户端: keycloak configuration

And I implemented my callback endpoint like that:我像这样实现了我的回调端点:

@GetMapping("/callback")
    @ResponseBody
    public String getToken(@RequestParam String code) {
        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED.toString());

        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("code", code);
        map.add("client_id", "spring-login-app");
        map.add("client_secret", "");
        map.add("grant_type", "authorization_code");
        map.add("redirect_uri", UriComponentsBuilder.fromHttpUrl("http://127.0.0.1:3002/callback").build().toString());

        HttpEntity formEntity = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        try {
            ResponseEntity<KeycloakTokenResponse> response =
                    restTemplate.exchange("http://127.0.0.1:8080/auth/realms/raroc/protocol/openid-connect/token",
                            HttpMethod.POST,
                            formEntity,
                            KeycloakTokenResponse.class);
            KeycloakTokenResponse resp = response.getBody();
            return resp.getAccess_token();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return "nothing";
    }

The problem is when I tried to get my access token from this callback endpoint, I received a 400 Bad Request error with the following message: 400 Bad Request: "{"error":"invalid_grant","error_description":"Incorrect redirect_uri"}"问题是当我试图从这个回调端点获取我的访问令牌时,我收到一个 400 Bad Request 错误,并显示以下消息: 400 Bad Request: "{"error":"invalid_grant","error_description":"Incorrect redirect_uri" }"

When I test it through postman with the same x-www-form-url-encoded form params, it works fine, but in spring, it's impossible to do it.当我通过 postman 使用相同的 x-www-form-url-encoded 表单参数对其进行测试时,它工作正常,但在 spring 中,这是不可能的。

I tried many scenario for the "redirect_uri" param, just a String, an UriComponentsBuilder.formHttpUrl, some other URL encoder thing but unfortunately I still have this error.我为“redirect_uri”参数尝试了许多场景,只是一个字符串、一个 UriComponentsBuilder.formHttpUrl、其他一些 URL 编码器,但不幸的是我仍然有这个错误。

You can try to specify a: http://localhost:3002/* instead of your actual redirect URI in the KeyCloak configuration but from what I read in your settings, everything looks good.您可以尝试在 KeyCloak 配置中指定: http://localhost:3002/*而不是您的实际重定向 URI,但从我在您的设置中读取的内容来看,一切看起来都不错。

Be careful also sometimes if you are changing the configuration of Keyloak, you need to restart it to take the changes into account.有时也要小心,如果您要更改 Keyloak 的配置,则需要重新启动它以将更改考虑在内。

If you want to test also a full scenario, open an incognito tab with your browser, and it should work.如果您还想测试一个完整的场景,请使用您的浏览器打开一个隐身标签,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM