Spring Boot services · Chapter 27 of 41
JWT login and the JWT filter
By the end of this chapter you can swap an email and password for a JWT at a login endpoint, and turn that token back into a logged-in user on every request. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Security
- 5 tasks
- Spring Boot
- In the all labs plan
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.
Why you need this
Ravi's counter terminal logs in when the shift starts and places orders until the 6 am rush is over. Today it sends his password with every order, and the server runs BCrypt every time.
JwtService from chapter 26 can make and check tokens, but nothing hands one out, and Spring Security does not know what a Bearer token is. A request that carries one is still anonymous: it gets 403, as if it had no token at all.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
AuthController.java: log in at POST /auth/login and get a Bearer token
Make POST /auth/login read a LoginRequest (email and password), check them with the authenticationManager, and answer 200 with a TokenResponse: a token issued by jwtService for the user's name, tokenType "Bearer", and expiresIn 900 (the configured 15 minute life in seconds).
The token's subject is the email, and its roles claim holds only the roles without the ROLE_ prefix and without the FACTOR_PASSWORD authority: ["CUSTOMER"] for ravi@tapri.in with masala@123, ["ADMIN"] for meera@tapri.in with founder@123.
- Task 2
AuthController.java: 401 and no token for a wrong password or an unknown email
Make POST /auth/login answer 401 Unauthorized with no body (so no token field) for a wrong password (ravi@tapri.in with cutting@123) and for an email with no account (kabir@tapri.in).
- Task 3
JwtAuthFilter.java: a valid Bearer token logs the request in
Make JwtAuthFilter read the Authorization header and, when it is "Bearer " plus a valid token, set a new SecurityContext holding an authenticated UsernamePasswordAuthenticationToken: the token's subject as its name, no credentials, and ROLE_ plus each role in the roles claim as its authorities. Always pass the request on down the chain.
Run it in SecurityConfig's chain, created there with jwtService (not as a bean), before UsernamePasswordAuthenticationFilter. Then GET /me with meera's token answers 200 with email meera@tapri.in and roles ["ROLE_ADMIN"], and with ravi's token, email ravi@tapri.in.
- Task 4
SecurityConfig.java: 401 for a missing, broken or expired token
Make every request that needs a login and has no valid token answer 401 Unauthorized (today it is 403): no Authorization header, "Bearer not-a-token", a token that expired an hour ago, and a token signed with another key. The 401 needs no body.
The filter must not throw for these tokens: catch the parser's JwtException (and IllegalArgumentException) and carry on unauthenticated, or a broken token turns into a 500.
- Task 5
SecurityConfig.java: a stateless API, with no session ever
Make the chain never create an HTTP session: after a request without a token and after one with a valid token there is no session, and no JSESSIONID cookie is sent.
What you will learn
- A login endpoint: Spring Security's AuthenticationManager checks the password, and JwtService issues the token.
- A JWT filter: it reads the token and tells Spring Security who is calling, before any rule is checked.
- A stateless chain with a 401: no session, no JSESSIONID, and the right status for a missing token.
More in Security
Build "JWT login and the JWT filter" yourself
Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.