fix logout error
This commit is contained in:
@@ -113,7 +113,7 @@ public class UserController {
|
|||||||
*/
|
*/
|
||||||
@PostMapping("/logout")
|
@PostMapping("/logout")
|
||||||
public ResponseEntity<APIResponse<Void>> logout(
|
public ResponseEntity<APIResponse<Void>> logout(
|
||||||
@CookieValue(name = "refreshToken") String refreshToken,
|
@CookieValue(name = "refreshToken", required = false) String refreshToken,
|
||||||
HttpServletRequest request,
|
HttpServletRequest request,
|
||||||
HttpServletResponse response) {
|
HttpServletResponse response) {
|
||||||
userService.logout(refreshToken);
|
userService.logout(refreshToken);
|
||||||
@@ -155,7 +155,7 @@ public class UserController {
|
|||||||
ResponseCookie refreshTokenCookie = ResponseCookie.from("refreshToken", refreshToken)
|
ResponseCookie refreshTokenCookie = ResponseCookie.from("refreshToken", refreshToken)
|
||||||
.httpOnly(true)
|
.httpOnly(true)
|
||||||
.maxAge(refreshTokenMaxAge)
|
.maxAge(refreshTokenMaxAge)
|
||||||
.sameSite("Strict")
|
.sameSite("Lax")
|
||||||
.secure(request.isSecure())
|
.secure(request.isSecure())
|
||||||
.path(REFRESH_COOKIE_PATH)
|
.path(REFRESH_COOKIE_PATH)
|
||||||
.build();
|
.build();
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import com.ddf.vodsystem.repositories.UserRepository;
|
|||||||
import com.ddf.vodsystem.security.GoogleVerifierService;
|
import com.ddf.vodsystem.security.GoogleVerifierService;
|
||||||
import com.ddf.vodsystem.security.JwtService;
|
import com.ddf.vodsystem.security.JwtService;
|
||||||
import com.ddf.vodsystem.security.TokenService;
|
import com.ddf.vodsystem.security.TokenService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
@@ -20,6 +22,8 @@ import java.util.Optional;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserService {
|
public class UserService {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(UserService.class);
|
||||||
|
|
||||||
private final GoogleVerifierService googleVerifierService;
|
private final GoogleVerifierService googleVerifierService;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final JwtService jwtService;
|
private final JwtService jwtService;
|
||||||
@@ -78,6 +82,8 @@ public class UserService {
|
|||||||
String refreshToken = tokenService.createSession(user);
|
String refreshToken = tokenService.createSession(user);
|
||||||
String jwt = jwtService.generateToken(user.getId());
|
String jwt = jwtService.generateToken(user.getId());
|
||||||
|
|
||||||
|
logger.debug("Access and Refresh tokens issued for User ID {}", user.getId());
|
||||||
|
|
||||||
return new TokenPackage(refreshToken, jwt);
|
return new TokenPackage(refreshToken, jwt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,6 +98,8 @@ public class UserService {
|
|||||||
TokenService.Rotation rotation = tokenService.rotate(refreshToken);
|
TokenService.Rotation rotation = tokenService.rotate(refreshToken);
|
||||||
String jwt = jwtService.generateToken(rotation.userId());
|
String jwt = jwtService.generateToken(rotation.userId());
|
||||||
|
|
||||||
|
logger.debug("New refresh and access token issued for user ID {}", rotation.userId());
|
||||||
|
|
||||||
return new TokenPackage(rotation.newRefreshToken(), jwt);
|
return new TokenPackage(rotation.newRefreshToken(), jwt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,11 +107,16 @@ public class UserService {
|
|||||||
* Ends the session tied to a refresh token by revoking its token family.
|
* Ends the session tied to a refresh token by revoking its token family.
|
||||||
* Already-issued JWT access tokens stay valid until they expire.
|
* Already-issued JWT access tokens stay valid until they expire.
|
||||||
*
|
*
|
||||||
|
* Does nothing if the token is missing, malformed or unknown.
|
||||||
|
*
|
||||||
* @param refreshToken the raw refresh token as a hex string, may be {@code null}
|
* @param refreshToken the raw refresh token as a hex string, may be {@code null}
|
||||||
* @throws NotAuthenticated if the token is not valid hex
|
|
||||||
*/
|
*/
|
||||||
public void logout(String refreshToken) {
|
public void logout(String refreshToken) {
|
||||||
|
try {
|
||||||
tokenService.revoke(refreshToken);
|
tokenService.revoke(refreshToken);
|
||||||
|
} catch (NotAuthenticated e) {
|
||||||
|
logger.info("Malformed or missing token on logout()");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user