Skip to content

Commit

Permalink
Update Cookie settings, Same Site to None
Browse files Browse the repository at this point in the history
  • Loading branch information
linxiaoxin committed Jul 24, 2024
1 parent b9d8fa6 commit 4bb72de
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.quemistry.auth_ms.model.*;
import com.quemistry.auth_ms.service.AuthenticationService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.server.Cookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -44,10 +46,13 @@ public ResponseEntity<UserProfile> getAccess(@RequestBody TokenRequest request){

//create cookie and return code with cookie session
HttpHeaders headers = new HttpHeaders();
headers.add("Set-Cookie", String.format("%s=%s; Max-Age=%s; Path=/; HttpOnly; SameSite=Lax;"
, COOKIE_NAME
, userProfile.getSessionId()
,sessionTimeout));
ResponseCookie cookie = ResponseCookie.from(COOKIE_NAME, userProfile.getSessionId())
.httpOnly(true).secure(true)
.path("/").maxAge(sessionTimeout)
.sameSite(Cookie.SameSite.NONE.attributeValue())
.build();

headers.add(HttpHeaders.SET_COOKIE, cookie.toString());

return ResponseEntity.status(HttpStatus.OK).headers(headers).body(userProfile);
}
Expand All @@ -57,7 +62,9 @@ public ResponseEntity<String> signOut(@CookieValue(COOKIE_NAME) String cookie, @
authenticationService.signOut(cookie, signOutRequest.getClientId());
//expire cookie to remove from session
HttpHeaders headers = new HttpHeaders();
headers.add("Set-Cookie", String.format("%s=%s; Max-Age=0; Path=/; HttpOnly; SameSite=Lax;", COOKIE_NAME,""));
ResponseCookie deleteCookie = ResponseCookie.from(COOKIE_NAME, "").build();

headers.add(HttpHeaders.SET_COOKIE, deleteCookie.toString());

return ResponseEntity.status(HttpStatus.OK).headers(headers).body(null);
}
Expand Down

0 comments on commit 4bb72de

Please sign in to comment.