4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Springでkeycloakのユーザー情報にアクセスした際のメモ

Last updated at Posted at 2019-03-07

##背景
Keycloakの管理画面にユーザー管理が行えるが、お客様(利用ユーザー)によっては扱いにくい画面であるため、SpringのWEBアプリからKeycloakのユーザー登録や編集が行える画面を提供する必要がでてきた。

keycloakのユーザー情報をSpringで表示・追加・編集する場合に実装した際のメモを残しておく

前提条件

・SpringBoot2(SpringFramework5)
・Keycloak4

keycloak Clientでの接続も可能であるが、今回はKeycloakで提供されるAdmin REST APIを利用する方法する。

実装例

gradle

build.gradle
dependencies {
   implementation('org.keycloak:keycloak-spring-boot-2-starter:4.0.0.Final')
}

properties

application.properties
# KeyCloak
keycloak.realm={レルム名}
keycloak.auth-server-url={keycloak URL}
keycloak.resource={リソース名}
keycloak.credentials.secret={シークレット値}

Config

WebSecurityConfig.java
@KeycloakConfiguration
public class WebSecurityConfig extends KeycloakWebSecurityConfigurerAdapter {

@Autowired
public KeycloakClientRequestFactory keycloakClientRequestFactory;

@bean
public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
    return new KeycloakSpringBootConfigResolver();
}
@Bean
public KeycloakRestTemplate keycloakRestTemplate() {
   return new KeycloakRestTemplate(keycloakClientRequestFactory);
}


Controller

ユーザー取得の実装例

KeycloakUserController.java
@Controller
@RequestMapping("/keycloak/user")
public class KeycloakUserController {
   @Autowired
   private KeycloakRestTemplate template;

   @RequestMapping(path="/list", method=RequestMethod.GET)
   public String list(@ModelAttribute("form") KeycloakUserListForm form, Model model) {
		String url = "http://{keycloakホスト}/auth/admin/realms/{レルム名}/users";
		ResponseEntity<UserRepresentation[]> response = template.getForEntity(url, UserRepresentation[].class);
		model.addAttribute("users", response.getBody());
		return "keycloak/user/list";
	}

ユーザー登録の実装例

String url = "http://{keycloakホスト}/auth/admin/realms/{レルム名}/users";

CredentialRepresentation credentials = new CredentialRepresentation();
credentials.setValue("test"); // パスワード
credentials.setType(CredentialRepresentation.PASSWORD);
credentials.setTemporary(false);

UserRepresentation user = new UserRepresentation();
user.setEnabled(true);
user.setUsername("yamada"); // ユーザー名
user.setLastName("yamada");
user.setFirstName("taro");
user.setCredentials(Arrays.asList(credentials));

template.postForEntity(url, user, Void.class);

ユーザー更新の実装例

String url = "http://{keycloakホスト}/auth/admin/realms/{レルム名}/users/{ユーザーID}";

// ユーザー情報更新
UserRepresentation user = new UserRepresentation();
user.setLastName("sato");
user.setFirstName("ichiro");
template.put(url, user);

ユーザーのロール設定更新

String url = "http://{keycloakホスト}/auth/admin/realms/{レルム名}/users/{ユーザーID}/role-mappings/realm";

List<RoleRepresentation> roleList = new ArrayList<RoleRepresentation>();
RoleRepresentation role = new RoleRepresentation();
role.setId("{設定したいロールのID}");
roleList.add(role);

template.postForEntity(url, roleList, Void.class);

4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?