Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does JWT stand for and what is its main purpose?
JWT stands for JSON Web Token. It is used to securely transmit information between parties as a JSON object, often for authentication and authorization.
Click to reveal answer
beginner
In Spring Boot, which library is commonly used to create and verify JWT tokens?
The 'jjwt' library (io.jsonwebtoken) is commonly used in Spring Boot to create and verify JWT tokens easily.
Click to reveal answer
beginner
What are the three parts of a JWT token?
A JWT token has three parts separated by dots: Header (token type and algorithm), Payload (claims or data), and Signature (to verify token integrity).
Click to reveal answer
intermediate
Which Spring Boot component typically holds the secret key used for signing JWT tokens?
The secret key is usually stored in application.properties or environment variables and injected into the JWT utility class for signing tokens.
Click to reveal answer
beginner
What is the purpose of the 'claims' in JWT generation?
Claims are pieces of information (like user ID, roles, expiration) stored in the JWT payload to convey user identity and permissions.
Click to reveal answer
Which part of the JWT contains the user's data like username or roles?
AHeader
BPayload
CSignature
DSecret key
✗ Incorrect
The Payload part of the JWT contains the claims, which hold user data like username or roles.
In Spring Boot, which method is commonly used to sign a JWT token?
Aencode()
Bencrypt()
Chash()
DsignWith()
✗ Incorrect
The signWith() method from the jjwt library is used to sign the JWT token with a secret key.
What does the JWT signature ensure?
AThe token is valid and untampered
BThe token is encrypted
CThe token expires
DThe token contains user roles
✗ Incorrect
The signature ensures the token has not been altered and is valid.
Where is the secret key for JWT signing usually stored in a Spring Boot app?
AIn application.properties or environment variables
BIn the JWT header
CIn the JWT payload
DHardcoded in the controller
✗ Incorrect
The secret key is stored securely in application.properties or environment variables, not in the token itself.
Which algorithm is commonly used to sign JWT tokens in Spring Boot?
AAES
BMD5
CHS256
DSHA1
✗ Incorrect
HS256 (HMAC with SHA-256) is a common algorithm used to sign JWT tokens.
Explain the process of generating a JWT token in a Spring Boot application.
Think about the steps from user info to a signed token string.
You got /5 concepts.
Describe the structure of a JWT token and the role of each part.
Remember the token has three parts separated by dots.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of generating a JWT (JSON Web Token) in a Spring Boot application?
easy
A. To securely identify users without storing session data on the server
B. To store user passwords in the database
C. To create HTML pages dynamically
D. To manage database connections
Solution
Step 1: Understand JWT purpose
JWTs are used to securely identify users by encoding user info and signing it.
Step 2: Compare options
Only To securely identify users without storing session data on the server describes JWT's role in stateless authentication without server sessions.
Final Answer:
To securely identify users without storing session data on the server -> Option A
Quick Check:
JWT purpose = secure user identity without sessions [OK]
Hint: JWTs identify users without server sessions [OK]
Common Mistakes:
Confusing JWT with session storage
Thinking JWT stores passwords
Assuming JWT creates web pages
2. Which of the following code snippets correctly initializes a JWT builder using the jjwt library in Spring Boot?
easy
A. JwtBuilder().setSubject("user").sign(secretKey).build();
B. Jwts.builder().subject("user").sign(secretKey).compact();
C. Jwts.create().subject("user").signWith(secretKey).generate();
D. Jwts.builder().setSubject("user").signWith(secretKey).compact();
Solution
Step 1: Recall jjwt syntax
The correct method chain starts with Jwts.builder(), uses setSubject(), signWith(), then compact().
Step 2: Check each option
Jwts.builder().setSubject("user").signWith(secretKey).compact(); matches the correct method names and order. Others use incorrect method names or chaining.
Final Answer:
Jwts.builder().setSubject("user").signWith(secretKey).compact(); -> Option D
signWith() expects a java.security.Key or SecretKey, not a plain String.
Step 2: Verify other methods
setSubject() accepts String, compact() is correctly called last, and Jwts.builder() is valid.
Final Answer:
signWith() requires a Key object, not a String -> Option C
Quick Check:
signWith() needs Key, not String [OK]
Hint: Use Key object with signWith(), not plain String [OK]
Common Mistakes:
Passing String directly to signWith()
Calling compact() too early
Misunderstanding setSubject() input
5. You want to generate a JWT in Spring Boot that expires in 10 minutes. Which code snippet correctly sets the expiration time using jjwt?
hard
A. Jwts.builder().setSubject("user").setExpiration(new Date(System.currentTimeMillis() + 600000)).signWith(secretKey).compact();
B. Jwts.builder().setSubject("user").setExpiry(600000).signWith(secretKey).compact();
C. Jwts.builder().setSubject("user").setExpiration(600000).signWith(secretKey).compact();
D. Jwts.builder().setSubject("user").setExpiresAt(new Date(600000)).signWith(secretKey).compact();
Solution
Step 1: Understand expiration setting in jjwt
setExpiration() expects a Date object representing the expiration time.
Step 2: Calculate expiration time
Use current time plus 600000 milliseconds (10 minutes) to set expiration correctly.
Step 3: Check options
Only Jwts.builder().setSubject("user").setExpiration(new Date(System.currentTimeMillis() + 600000)).signWith(secretKey).compact(); correctly uses setExpiration() with new Date(System.currentTimeMillis() + 600000).
Final Answer:
Jwts.builder().setSubject("user").setExpiration(new Date(System.currentTimeMillis() + 600000)).signWith(secretKey).compact(); -> Option A