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..c06b865 100644 --- a/pom.xml +++ b/pom.xml @@ -29,11 +29,22 @@ 23 + + + jitpack.io + https://jitpack.io + + org.springframework.boot spring-boot-starter-data-jpa + + com.github.Podzilla + podzilla-utils-lib + v1.1.5 + org.springframework.boot spring-boot-starter-data-redis 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) { 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/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 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()) {