Performance: Refresh token pattern
This pattern affects the responsiveness and load on the backend server during user authentication refresh cycles.
Jump into concepts and practice - no test required
Use silent background refresh of access tokens before expiry with asynchronous calls, allowing user actions to continue.
On every API request, the client sends the refresh token to get a new access token synchronously before proceeding.
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous token refresh on every request | Minimal | 0 | 0 | [X] Bad |
| Asynchronous background token refresh | Minimal | 0 | 0 | [OK] Good |
| Storing refresh token in localStorage | N/A | N/A | N/A | [X] Bad |
| Storing refresh token in HttpOnly cookie | N/A | N/A | N/A | [OK] Good |
What is the main purpose of using a refresh token in a Spring Boot authentication system?
Which of the following is the correct way to define a method in a Spring Boot controller to handle refresh token requests?
@PostMapping("/refresh")
public ResponseEntity<TokenResponse> refreshToken(@RequestBody RefreshRequest request) {
// method body
}Given the following Spring Boot service method, what will be the output if the refresh token is invalid?
public TokenResponse refreshAccessToken(String refreshToken) {
if (!tokenRepository.existsByToken(refreshToken)) {
throw new RuntimeException("Invalid refresh token");
}
// generate new access token
return new TokenResponse("newAccessToken");
}Identify the error in this Spring Boot refresh token controller method:
@PostMapping("/refresh")
public ResponseEntity<TokenResponse> refreshToken(@RequestParam String refreshToken) {
TokenResponse token = authService.refreshAccessToken(refreshToken);
return ResponseEntity.ok(token);
}What is the problem?
You want to implement a refresh token mechanism in Spring Boot that invalidates the old refresh token after use and issues a new one along with the access token. Which approach below correctly achieves this?