0
0
Spring Bootframework~20 mins

Refresh token pattern in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Refresh Token Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a refresh token is expired in this Spring Boot code?
Consider this simplified Spring Boot controller method that handles refresh tokens. What will be the HTTP response status if the refresh token is expired?
Spring Boot
public ResponseEntity<?> refreshToken(@RequestBody TokenRefreshRequest request) {
    String requestRefreshToken = request.getRefreshToken();
    return refreshTokenService.findByToken(requestRefreshToken)
        .map(refreshTokenService::verifyExpiration)
        .map(RefreshToken::getUser)
        .map(user -> {
            String token = jwtUtils.generateTokenFromUsername(user.getUsername());
            return ResponseEntity.ok(new TokenRefreshResponse(token, requestRefreshToken));
        })
        .orElseThrow(() -> new TokenRefreshException(requestRefreshToken, "Refresh token is not in database!"));
}
AReturns HTTP 200 with a new access token
BReturns HTTP 500 Internal Server Error due to null pointer
CReturns HTTP 401 Unauthorized without a new token
DThrows TokenRefreshException leading to HTTP 403 Forbidden
Attempts:
2 left
💡 Hint
Look at what happens when verifyExpiration fails or token is not found.
state_output
intermediate
2:00remaining
What is the value of the new access token after refresh?
Given this snippet from a refresh token service, what will be the value of the new access token returned?
Spring Boot
public String generateTokenFromUsername(String username) {
    return Jwts.builder()
        .setSubject(username)
        .setIssuedAt(new Date())
        .setExpiration(new Date(System.currentTimeMillis() + 600000))
        .signWith(SignatureAlgorithm.HS512, jwtSecret)
        .compact();
}

// Assume username = "alice" and jwtSecret = "secretKey"
AA plain string 'alice' without any token structure
BA JWT token string with subject 'alice' and expiration 10 minutes from now
CAn empty string because jwtSecret is invalid
DA token string with subject 'secretKey' instead of 'alice'
Attempts:
2 left
💡 Hint
The method builds a JWT with the username as subject and signs it.
📝 Syntax
advanced
2:30remaining
Which option correctly defines a RefreshToken entity with expiration date?
Choose the correct Java entity class definition for a RefreshToken with fields: id (Long), token (String), expiryDate (Instant), and user (User).
A
public class RefreshToken {
  @Id
  private Long id;
  private String token;
  private Instant expiryDate;
  @OneToOne
  private User user;
}
B
public class RefreshToken {
  private Long id;
  private String token;
  private Date expiryDate;
  @OneToOne
  private User user;
}
C
public class RefreshToken {
  @Id
  private Long id;
  private String token;
  private Instant expiryDate;
  @ManyToOne
  private User user;
}
D
public class RefreshToken {
  @Id
  private Long id;
  private String token;
  private LocalDate expiryDate;
  @ManyToOne
  private User user;
}
Attempts:
2 left
💡 Hint
Check the correct annotation for user relationship and the type for expiryDate.
🔧 Debug
advanced
2:00remaining
Why does this refresh token validation code throw NullPointerException?
Examine this code snippet that validates a refresh token. Why does it throw NullPointerException sometimes?
Spring Boot
public RefreshToken verifyExpiration(RefreshToken token) {
    if (token.getExpiryDate().compareTo(Instant.now()) < 0) {
        refreshTokenRepository.delete(token);
        throw new TokenRefreshException(token.getToken(), "Refresh token expired");
    }
    return token;
}

// token can be null if not found in DB
ABecause token is null and token.getExpiryDate() causes NullPointerException
BBecause Instant.now() returns null causing NullPointerException
CBecause refreshTokenRepository.delete(token) is null causing NullPointerException
DBecause token.getExpiryDate() is null causing NullPointerException
Attempts:
2 left
💡 Hint
Consider what happens if the token argument is null.
🧠 Conceptual
expert
2:30remaining
What is the main security benefit of using a refresh token pattern in Spring Boot?
Why do applications use refresh tokens instead of just long-lived access tokens?
ARefresh tokens allow short-lived access tokens, reducing risk if access token is stolen
BRefresh tokens eliminate the need for user authentication entirely
CRefresh tokens store user passwords securely on the client side
DRefresh tokens allow unlimited access without expiration
Attempts:
2 left
💡 Hint
Think about what happens if an access token is stolen and how refresh tokens help.