Your application requires stateless authentication for RESTful services. How would you implement JSON Web Token (JWT) authentication using Spring Security? Describe the flow from user login to accessing protected resources.

Stateless JWT authentication replaces server-side sessions with a self-contained, signed token that the client stores and presents on every request, letting Spring Security authenticate and authorize without storing any session state.

Key Points

Example

A mobile app logs in once, stores the returned JWT, and attaches it to every API call for the rest of the session; if the token expires, the app calls a refresh endpoint instead of forcing the user to log in again.

// 1. Login and issue token
@PostMapping("/login")
public ResponseEntity<String> login(@RequestBody LoginRequest req) {
    Authentication auth = authManager.authenticate(
        new UsernamePasswordAuthenticationToken(req.username(), req.password()));
    String token = jwtService.generateToken(auth);
    return ResponseEntity.ok(token);
}

// 2. Filter validates token on protected requests
public class JwtFilter extends OncePerRequestFilter {
    protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
            throws ServletException, IOException {
        String token = jwtService.extractToken(req);
        if (token != null && jwtService.isValid(token)) {
            SecurityContextHolder.getContext().setAuthentication(jwtService.getAuthentication(token));
        }
        chain.doFilter(req, res);
    }
}

Interview Tip

A concise interview answer is

I'd validate credentials at login and issue a signed JWT holding identity and roles, then return it to the client. Every subsequent request carries that token in the Authorization header, and a custom filter validates it and populates the SecurityContext, so the whole flow stays stateless and scales horizontally.

Last updated: 16 July 2026

Previous question: You are developing a web application where users can have different roles (e.g., ADMIN, USER). How would you implement role-based access control using Spring Security to ensure that only users with the ADMIN role can access certain endpoints?

Next question: You are building a web application that requires secure forms to prevent Cross-Site Request Forgery (CSRF) attacks. How would you configure CSRF protection in Spring Security, and what additional measures would you take to ensure form security?

More Spring Security - Level II interview questions

All Spring Security - Level II questions