Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions search-in-keycloak-with-spring-boot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
58 changes: 58 additions & 0 deletions search-in-keycloak-with-spring-boot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>search-in-keycloak-with-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>search-in-keycloak-with-spring-boot</name>
<description>search-in-keycloak-with-spring-boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-client</artifactId>
<version>19.0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.searchinkeycloakwithspringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SearchInKeycloakWithSpringBootApplication {

public static void main(String[] args) {
SpringApplication.run(SearchInKeycloakWithSpringBootApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.searchinkeycloakwithspringboot.config;

import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.keycloak.OAuth2Constants;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.KeycloakBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
public class Config {

@Bean
public Keycloak keycloak() {
return KeycloakBuilder.builder()
.serverUrl("http://localhost:8080/")
.realm("master")
.grantType(OAuth2Constants.PASSWORD)
.username("admin")
.password("admin")
.clientId("admin-cli")
.resteasyClient(new ResteasyClientBuilder()
.connectionPoolSize(10)
.build()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.searchinkeycloakwithspringboot.exception;

public class UserNotFoundInKeycloakException extends Exception{
public UserNotFoundInKeycloakException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.searchinkeycloakwithspringboot.exception.handler;

import com.example.searchinkeycloakwithspringboot.exception.UserNotFoundInKeycloakException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class ExceptionController {



@ExceptionHandler
public ResponseEntity<String> handleUserNotFoundInKeycloakException(UserNotFoundInKeycloakException e){
return ResponseEntity.badRequest().body(e.getMessage());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.example.searchinkeycloakwithspringboot.service;

import com.example.searchinkeycloakwithspringboot.exception.UserNotFoundInKeycloakException;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.resource.RoleResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.UserRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class KeycloakService {


@Autowired
private Keycloak keycloak;


public UserRepresentation searchById(String id) {
UserResource userResource = keycloak.realm("master").users().get(id);
return userResource.toRepresentation();
}
public UserRepresentation searchByEmail(String email) throws UserNotFoundInKeycloakException {
List<UserRepresentation> userRepresentations = keycloak.realm("master").users().search(null, null, null, email, 0, 1);
if (userRepresentations.isEmpty()) {
throw new UserNotFoundInKeycloakException("User with email " + email + " not found");
}
return userRepresentations.get(0);
}
public UserRepresentation searchByUsername(String username) throws UserNotFoundInKeycloakException {
List<UserRepresentation> userRepresentations = keycloak.realm("master").users().search(username, true);
if (userRepresentations.isEmpty()) {
throw new UserNotFoundInKeycloakException("User with username " + username + " not found");
}
return userRepresentations.get(0);
}
public UserRepresentation searchByPhone(String phone) throws UserNotFoundInKeycloakException {
List<UserRepresentation> userRepresentations = keycloak.realm("master").users().searchByAttributes("phone:" + phone);
if (userRepresentations.isEmpty()) {
throw new UserNotFoundInKeycloakException("User with phone number " + phone + " not found");
}
return userRepresentations.get(0);
}
public Set<UserRepresentation> searchByRole(String roleName) {
return keycloak.realm("master").roles().get(roleName).getRoleUserMembers() ;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.searchinkeycloakwithspringboot.web;

import com.example.searchinkeycloakwithspringboot.exception.UserNotFoundInKeycloakException;
import com.example.searchinkeycloakwithspringboot.service.KeycloakService;
import lombok.AllArgsConstructor;
import org.keycloak.representations.idm.UserRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/search")
@AllArgsConstructor
public class SearchController {




private KeycloakService keycloakService ;



@GetMapping("/id/{id}")
public ResponseEntity<UserRepresentation> searchById(@PathVariable String id) {
return ResponseEntity.ok().body(keycloakService.searchById(id));
}
@GetMapping("/email/{email}")
public ResponseEntity<UserRepresentation> searchByEmail(@PathVariable String email ) throws UserNotFoundInKeycloakException {
return ResponseEntity.ok().body(keycloakService.searchByEmail(email));
}
@GetMapping("/username/{username}")
public ResponseEntity<UserRepresentation> searchByUsername(@PathVariable String username) throws UserNotFoundInKeycloakException {
return ResponseEntity.ok().body(keycloakService.searchByUsername(username));
}
@GetMapping("/phone/{phone}")
public ResponseEntity<UserRepresentation> searchByPhoneNumber(@PathVariable String phone) throws UserNotFoundInKeycloakException {
return ResponseEntity.ok().body(keycloakService.searchByPhone(phone));
}
@GetMapping("/role/{role}")
public ResponseEntity<Set<UserRepresentation>> searchByRole(@PathVariable String role) {
return ResponseEntity.ok().body(keycloakService.searchByRole(role));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server.port=9090
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.searchinkeycloakwithspringboot;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SearchInKeycloakWithSpringBootApplicationTests {

@Test
void contextLoads() {
}

}