본문 바로가기
ETC

[Keycloak] Spring에서 유저 생성하기

by palbokdev 2021. 11. 18.
<dependency>
    <groupId>org.keycloak</groupId>
    <artifactId>keycloak-admin-client</artifactId>
    <version>${keycloak.version}</version>
</dependency>

키클락 버전에 맞춰 의존성을 추가해준다.

 

Keycloak kc = KeycloakBuilder.builder()
            .serverUrl("http://your.keycloak.domain/auth")
            .realm("master")
            .username("admin")
            .password("secret")
            .clientId("admin-cli")
            .resteasyClient(
                new ResteasyClientBuilder()
                    .connectionPoolSize(10).build()
            ).build();

인증서버 url, 렐름, 클라이언트 id, 사용자 계정과 비밀번호를 입력한다.

 

UserRepresentation user = new UserRepresentation();
user.setUsername("johndoe");
user.set... // all the user attributes to set
kc.realm("demo").users().create(user);

키클락에서 렐름을 설정하고 유저를 생성해주면 완료된다. (201)

 

 

ref : https://www.n-k.de/2016/08/keycloak-admin-client.html