Access Tokens
Access Token Flow
1. User logs in
2. Server generates short-lived access token (15 min)
3. Client sends token with requests
4. Server validates token
5. When expired, use refresh token to get new access token
Token Validation
@GetMapping("/protected")
public ResponseEntity<?> protectedEndpoint(
@RequestHeader("Authorization") String authHeader) {
String token = authHeader.replace("Bearer ", "");
if (!tokenService.validate(token)) {
return ResponseEntity.status(401).build();
}
Claims claims = tokenService.parse(token);
return ResponseEntity.ok("Hello " + claims.getSubject());
}
Token Management
Token Lifecycle
- Generation: Create with short expiry
- Validation: Verify signature and claims
- Refresh: Exchange refresh token for new access token
- Revocation: Invalidate on logout/security events
Storage
- Access tokens: Memory or short-lived storage
- Refresh tokens: Secure HTTP-only cookies
- Revoked tokens: Redis blacklist with TTL
Best Practices
- Rotate signing keys regularly
- Implement token binding
- Monitor token usage patterns
Key Points
- Understanding Access Tokens is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for Access Tokens in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Access Tokens implementation
// Key aspects: validation, error handling, logging, testing
public class AccessTokens {
// Production-ready implementation
} Identify and handle edge cases for Access Tokens. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotency Write a testing strategy for Access Tokens. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injection Quiz
1. Access token lifetime should be?
2. How is token usually sent?
3. What is a common mistake when implementing Access Tokens?
Flashcards
Question
Access token lifetime?
Click to reveal answer
Answer
Short-lived: 15-30 minutes
Question
How to send token?
Click to reveal answer
Answer
Authorization: Bearer <token>
Question
Access Tokens best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1. Access tokens should be short-lived
- 2. Sent via Authorization header
- 3. Server validates on each request
- 4. Expired tokens need refresh flow
Interview Tips
- • Explain token lifecycle
- • Know how tokens are sent
Cheat Sheet
Access Tokens
- Short-lived: 15-30 min
- Header: Authorization: Bearer
- Validate on each request
- Expired: use refresh token