1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| CredentialRepresentation credential = new CredentialRepresentation(); credential.setType(CredentialRepresentation.PASSWORD); credential.setValue(password); credential.setTemporary(false); List<CredentialRepresentation> credentials = new ArrayList<>(); credentials.add(credential);
UserRepresentation user = new UserRepresentation(); // 设置用户名 user.setUsername(username); // 设置用户凭据,即密码 user.setCredentials(Collections.singletonList(credential)); // 设置启用该用户,接下来还可以设置角色等等 user.setEnabled(true); user.setFirstName(username);//名称 user.setLastName("jiliapp.cn");//姓 user.setEmail(username+"@jiliapp.cn");
try { RealmResource realm = kc.realm(realmName); UsersResource users = realm.users(); // users.list().stream().forEach(new Consumer<UserRepresentation>() { // @Override // public void accept(UserRepresentation userRepresentation) { // logger.info("userRepresentation:{}",userRepresentation.getUsername()); // } // }); // Create user (requires manage-users role) Response response=users.create(user); //409 已经存在 //201 创建成功 System.out.printf("Repsonse: %s %s%n", response.getStatus(), response.getStatusInfo()); System.out.println(response.getLocation()); if (response.getStatus()==Response.Status.CREATED.getStatusCode()){ String userId = CreatedResponseUtil.getCreatedId(response); return Rsp.success(response.getEntity()); }else{ return Rsp.ret(response.getStatus(),response.getStatusInfo().getReasonPhrase()); } }catch (Exception e) { e.printStackTrace(); return Rsp.failed(e.getMessage()); }
|