Learn how Brautcloud uses JWT-based authentication with refresh tokens to secure user sessions
Brautcloud implements a secure authentication system using JWT (JSON Web Tokens) for access control and refresh tokens for maintaining user sessions. This dual-token approach provides both security and a seamless user experience.
When users log in, they receive both an access token and a refresh token. The access token is returned in the response body, while the refresh token is set as an HTTP-only cookie.
Access tokens have a short expiration time for security. When they expire, the client can obtain a new access token using the refresh token without requiring the user to log in again.
public RefreshToken validateRefreshToken(String token) { RefreshToken refreshToken = refreshTokenRepository.findByToken(token) .orElseThrow(() -> new InvalidRefreshTokenException("Refresh token not found")); if (refreshToken.getExpiresAt().isBefore(Instant.now())) { refreshTokenRepository.delete(refreshToken); throw new InvalidRefreshTokenException("Refresh token expired"); } return refreshToken;}
Brautcloud implements refresh token rotation: each time a refresh token is used, it’s replaced with a new one. This enhances security by limiting the lifetime of each token.
Refresh tokens are stored in HTTP-only cookies with the following security attributes:
httpOnly: true - Prevents JavaScript access to the cookie
sameSite: "Strict" - Prevents CSRF attacks
secure: false - Set to true in production for HTTPS-only transmission
path: "/api/auth" - Restricts cookie to authentication endpoints
maxAge: 30 days - Cookie expires after 30 days
Why use HTTP-only cookies for refresh tokens?
HTTP-only cookies cannot be accessed by JavaScript, which protects against XSS (Cross-Site Scripting) attacks. Even if an attacker injects malicious JavaScript into the page, they cannot steal the refresh token. This makes cookies more secure than storing tokens in localStorage or sessionStorage.