简体   繁体   中英

keycloak spring boot - redirection to token endpoint

I am consuming the endpoints of keycloak, I work with OAuth resource server, I want to use http://localhost:port/auth/realms/myrealm/protocol/openid-connect/token to get token but I got i don't know how to de redirection from my spring endpoint to keycloak endpoint

Here is my trial but it's not working

public LoginData login(@RequestBody LoginData logindata){
   MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
   Map map = new HashMap<String, String>();
   map.put("Content-Type", "application/x-www-form-urlencoded");

   headers.setAll(map);

    HttpEntity<LoginData> entity = new HttpEntity<LoginData>(logindata,headers);
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<LoginData> response = restTemplate.exchange(
            "http://localhost:port/auth/realms/myrealm/protocol/openid-connect/token", HttpMethod.POST, entity, LoginData.class);

            System.out.println(response);
            return response.getBody();

    }

Why do you need to call this endpoint. Just integrate with Spring security oauth2-client lib

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-client</artifactId>
    </dependency>

Or If you want to integrate with proprietary Keycloak lib, use Keycloak adapter for spring

With spring oauth2, you will need to use additional configuration like spring.security.oauth2.client.registration
spring.security.oauth2.client.provider.keycloak
This is not a complete answer but just a direction

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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