How can JWT (JSON Web Token) be integrated into Spring Boot for API security?

Integrating JWT into Spring Boot means replacing session-based login with a stateless flow where a signed token, generated at login, is sent on every subsequent request and validated by a custom filter before Spring Security grants access.

Key Points

Example

After a successful login to /api/login, the client stores the returned JWT and sends it as Authorization: Bearer <token> on every subsequent API call, which the filter verifies before letting the request reach the controller.

public class JwtAuthFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                     HttpServletResponse response,
                                     FilterChain chain) throws ServletException, IOException {
        String header = request.getHeader("Authorization");
        if (header != null && header.startsWith("Bearer ")) {
            String token = header.substring(7);
            if (jwtService.isValid(token)) {
                Authentication auth = jwtService.getAuthentication(token);
                SecurityContextHolder.getContext().setAuthentication(auth);
            }
        }
        chain.doFilter(request, response);
    }
}

Interview Tip

A concise interview answer is

I integrate JWT by adding a login endpoint that issues a signed token, then writing a custom filter that extracts the token from the Authorization header, validates its signature and expiration, and sets the SecurityContext for the request. This keeps the API stateless since no session needs to be stored server-side.

Last updated: 16 July 2026

Previous question: What are the options for securing a REST API in Spring Boot?

Next question: You are tasked with designing a secure REST API for a banking application. What security practices would you implement in Spring Boot?

More Spring Security - Level II interview questions

All Spring Security - Level II questions