Skip to content

Commit 7ae846e

Browse files
Merge pull request #162 from ravishanigarapu/develop
Cors issue resolved
2 parents e70fc25 + d2ae95f commit 7ae846e

1 file changed

Lines changed: 33 additions & 9 deletions

File tree

src/main/java/com/iemr/common/utils/CookieUtil.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,39 @@ public Optional<String> getCookieValue(HttpServletRequest request, String cookie
2525
}
2626

2727
public void addJwtTokenToCookie(String Jwttoken, HttpServletResponse response, HttpServletRequest request) {
28-
// Create a new cookie with the JWT token
29-
Cookie cookie = new Cookie("Jwttoken", Jwttoken);
30-
cookie.setHttpOnly(true); // Prevent JavaScript access for security
31-
cookie.setMaxAge(60 * 60 * 24); // 1 day expiration time
32-
cookie.setPath("/"); // Make the cookie available for the entire application
33-
if ("https".equalsIgnoreCase(request.getScheme())) {
34-
cookie.setSecure(true); // Secure flag only on HTTPS
35-
}
36-
response.addCookie(cookie); // Add the cookie to the response
28+
// Create a new cookie with the JWT token
29+
Cookie cookie = new Cookie("Jwttoken", Jwttoken);
30+
31+
// Make the cookie HttpOnly to prevent JavaScript access for security
32+
cookie.setHttpOnly(true);
33+
34+
// Set the Max-Age (expiry time) in seconds (1 day)
35+
cookie.setMaxAge(60 * 60 * 24); // 1 day expiration
36+
37+
// Set the path to "/" so the cookie is available across the entire application
38+
cookie.setPath("/");
39+
40+
// Set the SameSite attribute for cross-site request handling (if needed)
41+
String sameSite = "None"; // Allow cross-site cookies (can be 'Strict', 'Lax', or 'None')
42+
cookie.setSecure(true);
43+
// Add the cookie to the response
44+
response.addCookie(cookie);
45+
46+
// Build the Set-Cookie header manually (to add SameSite attribute support)
47+
StringBuilder cookieHeader = new StringBuilder();
48+
cookieHeader.append(cookie.getName()).append("=").append(cookie.getValue())
49+
.append("; Path=").append(cookie.getPath())
50+
.append("; Max-Age=").append(cookie.getMaxAge())
51+
.append("; HttpOnly");
52+
53+
// Add SameSite and Secure attributes manually if needed
54+
cookieHeader.append("; SameSite=").append(sameSite);
55+
if (cookie.getSecure()) {
56+
cookieHeader.append("; Secure");
57+
}
58+
59+
// Set the custom Set-Cookie header
60+
response.addHeader("Set-Cookie", cookieHeader.toString());
3761
}
3862

3963
public String getJwtTokenFromCookie(HttpServletRequest request) {

0 commit comments

Comments
 (0)