StackAsk is a community where developers ask questions, share answers and learn from each others real-world experience, so every solved problem helps the next person who runs into it.
private void setTokenCookie(String tokenType, String token, long ttl, HttpServletResponse servletResponse) { if (servletResponse == null) { throw new AppException(ErrorCode.INTERNAL_SERVER_ERROR, "HttpServletResponse is null"); } ResponseCookie tokenCookie = ResponseCookie.from(tokenType, token) .httpOnly(true) .secure(true) .path("/") .maxAge(Duration.ofSeconds(ttl)) .sameSite("None") .build(); servletResponse.addHeader("Set-Cookie", tokenCookie.toString()); }
const response = await fetch(logoutApiUrl, { method: "POST", credentials: "include", headers: { "Content-Type": "application/json", Authorization: `Bearer `, }, });
private String extractTokenFromCookie(HttpServletRequest request, String cookieName) { if (request.getCookies() == null) { throw ExceptionUtils.appException(ErrorCode.UNAUTHORIZED, "Cannot find cookies in request header or cookie " + (cookieName.equals(TokenType.ACCESS_TOKEN) ? "access token" : "refresh token")); } for (Cookie cookie : request.getCookies()) { System.out.println(cookie.getName()); if (cookieName.equals(cookie.getName())) { return cookie.getValue(); } } throw ExceptionUtils.appException(ErrorCode.UNAUTHORIZED, "Cannot find cookie with name: %s", cookieName); }
addCookie
servletResponse.addHeader("Set-Cookie", tokenCookie.toString());
public static void decorateToAddUserAccessToken( HttpServletResponse response, UserAccessTokenModel accessToken ) { response.addCookie(newAccessTokenCookie(accessToken)); response.addCookie(newAccessTokenExpiredAtCookie(accessToken)); }
2.2K Point(s)
1.2K Point(s)
313 Point(s)
178 Point(s)
138 Point(s)
Your account is signed in on the device(s) below. Sign out of at least one of them to continue signing in on this device.
Your account is signed in on the device(s) below. Sign out directly on one of those devices, or wait until the sign-in again time shown below.
If you cant access those devices, please contact the administrator to sign them out for you.
Input your email to receive reset password link, or if you remember your password, you can click