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
4 changes: 4 additions & 0 deletions nginx_file
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ server {
proxy_pass http://localhost:8587;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;

# CORS Headers
if ($request_method = OPTIONS) {
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/com/open/spring/security/JwtApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,15 @@ public ResponseEntity<?> createAuthenticationToken(@RequestBody Person authentic
return new ResponseEntity<>("Token generation failed", HttpStatus.INTERNAL_SERVER_ERROR);
}

boolean secureFlag = cookieSecure && request.isSecure();
String sameSite = secureFlag ? cookieSameSite : "Lax";
// Build cookie with development-friendly settings
// For localhost: allow HTTP and SameSite=Lax
// For production: require HTTPS and SameSite=None; Secure
ResponseCookie tokenCookie = ResponseCookie.from("jwt_java_spring", token)
.httpOnly(true)
.secure(secureFlag)
.secure(cookieSecure)
.path("/api")
.maxAge(cookieMaxAge) // Configured via jwt.cookie.max-age in application.properties
.sameSite(sameSite)
.sameSite(cookieSameSite)
.build();

return ResponseEntity.ok().header(HttpHeaders.SET_COOKIE, tokenCookie.toString()).body(resolvedUid + " was authenticated successfully");
Expand Down Expand Up @@ -141,24 +139,21 @@ public String performLogout(Authentication authentication, HttpServletRequest re
// Perform logout using SecurityContextLogoutHandler
logoutHandler.logout(request, response, authentication);

boolean secureFlag = cookieSecure && request.isSecure();
String sameSite = secureFlag ? cookieSameSite : "Lax";

// Expire the JWT token immediately by setting a past expiration date
ResponseCookie jwtCookie = ResponseCookie.from("jwt_java_spring", "")
.httpOnly(true)
.secure(secureFlag)
.secure(cookieSecure)
.path("/api")
.maxAge(0) // Set maxAge to 0 to expire the cookie immediately
.sameSite(sameSite)
.sameSite(cookieSameSite)
.build();

ResponseCookie sessionCookie = ResponseCookie.from(sessionCookieName, "")
.httpOnly(true)
.secure(secureFlag)
.secure(cookieSecure)
.path("/")
.maxAge(0)
.sameSite(sameSite)
.sameSite(cookieSameSite)
.build();

// Set the cookies in the response to effectively "remove" them
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/com/open/spring/security/MvcSecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,12 @@ public SecurityFilterChain mvcSecurityFilterChain(HttpSecurity http) throws Exce
return;
}

boolean secureFlag = cookieSecure && request.isSecure();
String sameSite = secureFlag ? cookieSameSite : "Lax";
ResponseCookie jwtCookie = ResponseCookie.from("jwt_java_spring", token)
.httpOnly(true)
.secure(secureFlag)
.secure(cookieSecure)
.path("/api")
.maxAge(-1)
.sameSite(sameSite)
.sameSite(cookieSameSite)
.build();

response.addHeader(HttpHeaders.SET_COOKIE, jwtCookie.toString());
Expand All @@ -138,21 +136,19 @@ public SecurityFilterChain mvcSecurityFilterChain(HttpSecurity http) throws Exce
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutSuccessHandler((request, response, authentication) -> {
boolean secureFlag = cookieSecure && request.isSecure();
String sameSite = secureFlag ? cookieSameSite : "Lax";
ResponseCookie sessionCookie = ResponseCookie.from(sessionCookieName, "")
.httpOnly(true)
.secure(secureFlag)
.secure(cookieSecure)
.path("/")
.maxAge(0)
.sameSite(sameSite)
.sameSite(cookieSameSite)
.build();
ResponseCookie jwtCookie = ResponseCookie.from("jwt_java_spring", "")
.httpOnly(true)
.secure(secureFlag)
.secure(cookieSecure)
.path("/api")
.maxAge(0)
.sameSite(sameSite)
.sameSite(cookieSameSite)
.build();
response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie.toString());
response.addHeader(HttpHeaders.SET_COOKIE, jwtCookie.toString());
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ server.port=8585
socket.port=8589
socket.host=0.0.0.0

# Trust X-Forwarded-* headers from reverse proxy (Nginx)
# Fixes Mixed Content errors: ensures request.isSecure()=true behind HTTPS proxy
server.forward-headers-strategy=framework

# Disable default error page
server.error.whitelabel.enabled=false

Expand Down