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
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,22 @@
<properties>
<java.version>23</java.version>
</properties>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.github.Podzilla</groupId>
<artifactId>podzilla-utils-lib</artifactId>
<version>v1.1.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/podzilla/auth/AuthApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/podzilla/auth/security/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ SecurityFilterChain securityFilterChain(final HttpSecurity http)
.requestMatchers("/swagger-ui/**",
"/v3/api-docs/**")
.permitAll()
.requestMatchers("/actuator/**")
.permitAll()
.anyRequest().authenticated()
)
.sessionManagement(s -> s
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/com/podzilla/auth/service/AuthenticationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down