From 5e64056ceb79f609510da277395242ea71866328 Mon Sep 17 00:00:00 2001 From: Nour Eldien Ayman Date: Sat, 10 May 2025 00:41:41 +0300 Subject: [PATCH 1/4] Add Dockerfile and spring-boot-starter-actuator dependency with actuator endpoints --- Dockerfile | 9 +++++++++ pom.xml | 5 +++++ .../java/com/podzilla/auth/security/SecurityConfig.java | 2 ++ 3 files changed, 16 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e407c6c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +# Use the specified base image +FROM openjdk:25-ea-4-jdk-oraclelinux9 + +WORKDIR /app + +COPY target/auth-0.0.1-SNAPSHOT.jar /app/auth-0.0.1-SNAPSHOT.jar + +# Define the command to run your application +CMD [ "java", "-jar", "/app/auth-0.0.1-SNAPSHOT.jar" ] \ No newline at end of file diff --git a/pom.xml b/pom.xml index 84202a5..1b564ac 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,11 @@ org.springframework.boot spring-boot-starter-data-jpa + + org.springframework.boot + spring-boot-starter-actuator + 3.4.5 + org.springframework.boot spring-boot-starter-data-redis diff --git a/src/main/java/com/podzilla/auth/security/SecurityConfig.java b/src/main/java/com/podzilla/auth/security/SecurityConfig.java index 8da1d06..3418366 100644 --- a/src/main/java/com/podzilla/auth/security/SecurityConfig.java +++ b/src/main/java/com/podzilla/auth/security/SecurityConfig.java @@ -51,6 +51,8 @@ SecurityFilterChain securityFilterChain(final HttpSecurity http) .requestMatchers("/swagger-ui/**", "/v3/api-docs/**") .permitAll() + .requestMatchers("/actuator/**") + .permitAll() .anyRequest().authenticated() ) .sessionManagement(s -> s From cbafcb7b49d6e1c416764515eeb001cc44cc0710 Mon Sep 17 00:00:00 2001 From: Nour Eldien Ayman Date: Sat, 10 May 2025 11:06:44 +0300 Subject: [PATCH 2/4] Add JitPack repository and mq-utils-lib dependency --- pom.xml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pom.xml b/pom.xml index 1b564ac..477a44a 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,12 @@ 23 + + + jitpack.io + https://jitpack.io + + org.springframework.boot @@ -39,6 +45,11 @@ spring-boot-starter-actuator 3.4.5 + + com.github.Podzilla + mq-utils-lib + v1.1.0 + org.springframework.boot spring-boot-starter-data-redis From 54892fdc851117abf0484677b9e23a3dd92d9135 Mon Sep 17 00:00:00 2001 From: Nour Eldien Ayman Date: Sun, 11 May 2025 10:17:29 +0300 Subject: [PATCH 3/4] Refactor authentication to add user details in response headers --- .../controller/AuthenticationController.java | 8 +++---- .../auth/service/AuthenticationService.java | 22 +++++++++++++++++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/podzilla/auth/controller/AuthenticationController.java b/src/main/java/com/podzilla/auth/controller/AuthenticationController.java index 1c45c26..0d1c85f 100644 --- a/src/main/java/com/podzilla/auth/controller/AuthenticationController.java +++ b/src/main/java/com/podzilla/auth/controller/AuthenticationController.java @@ -12,7 +12,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -113,9 +112,8 @@ public ResponseEntity refreshToken( responseCode = "200", description = "User details fetched successfully" ) - public UserDetails getCurrentUser() { - UserDetails userDetails = authenticationService.getCurrentUserDetails(); - LOGGER.info("Fetched details for user {}", userDetails.getUsername()); - return userDetails; + public void addUserDetailsInHeader(final HttpServletResponse response) { + authenticationService.addUserDetailsInHeader(response); + LOGGER.info("Fetching current user details and adding to header"); } } diff --git a/src/main/java/com/podzilla/auth/service/AuthenticationService.java b/src/main/java/com/podzilla/auth/service/AuthenticationService.java index 038a35c..f31fca6 100644 --- a/src/main/java/com/podzilla/auth/service/AuthenticationService.java +++ b/src/main/java/com/podzilla/auth/service/AuthenticationService.java @@ -122,19 +122,37 @@ public String refreshToken(final HttpServletRequest request, } } - public UserDetails getCurrentUserDetails() { + public void addUserDetailsInHeader( + final HttpServletResponse response) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Object principal = authentication.getPrincipal(); if (principal instanceof UserDetails) { - return (UserDetails) principal; + UserDetails userDetails = (UserDetails) principal; + String email = userDetails.getUsername(); + StringBuilder roles = new StringBuilder(); + userDetails.getAuthorities().forEach((authority) -> { + if (!roles.isEmpty()) { + roles.append(", "); + } + roles.append(authority.getAuthority()); + }); + setRoleAndEmailInHeader(response, email, roles.toString()); } else { throw new InvalidActionException( "User details not saved correctly."); } } + private void setRoleAndEmailInHeader( + final HttpServletResponse response, + final String email, + final String roles) { + response.setHeader("X-User-Email", email); + response.setHeader("X-User-Roles", roles); + } + private void checkNotNullValidationException(final String value, final String message) { if (value == null || value.isEmpty()) { From 0308c35844a984474693300d708f38f78277b1b1 Mon Sep 17 00:00:00 2001 From: Nour Eldien Ayman Date: Sun, 11 May 2025 13:40:48 +0300 Subject: [PATCH 4/4] Update component scanning and dependency version for podzilla-utils-lib --- pom.xml | 9 ++------- src/main/java/com/podzilla/auth/AuthApplication.java | 2 ++ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 477a44a..c06b865 100644 --- a/pom.xml +++ b/pom.xml @@ -40,15 +40,10 @@ org.springframework.boot spring-boot-starter-data-jpa - - org.springframework.boot - spring-boot-starter-actuator - 3.4.5 - com.github.Podzilla - mq-utils-lib - v1.1.0 + podzilla-utils-lib + v1.1.5 org.springframework.boot diff --git a/src/main/java/com/podzilla/auth/AuthApplication.java b/src/main/java/com/podzilla/auth/AuthApplication.java index 920bdd3..1cca17a 100644 --- a/src/main/java/com/podzilla/auth/AuthApplication.java +++ b/src/main/java/com/podzilla/auth/AuthApplication.java @@ -3,9 +3,11 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @EnableCaching +@ComponentScan(basePackages = { "com.podzilla" }) public class AuthApplication { public static void main(final String[] args) {