In an authentication flow, @RequestBody binds the JSON payload of a login or registration request, such as a username and password, to a Java object in your controller. Spring reads the body, converts it with Jackson and passes the object to your method, where you hand the credentials to Spring Security's AuthenticationManager and return a token or session. The annotation only does the data binding; it does not authenticate anything by itself.
Because credentials travel in the body, a few rules apply: use HTTPS, never log the password, and never put credentials in the URL.
A login endpoint typically receives:
@RequestBody converts that JSON to a request object. Compared with query parameters, a body keeps secrets out of URLs, server access logs, browser history and Referer headers, which is why credentials belong in a POST body.
Java records keep the models short and immutable. Add validation so empty values are rejected before authentication runs:
Limiting the password length protects the password hasher from very large inputs. A record's default toString() prints every component, so never log a LoginRequest object. Override toString() or log only the username.
The controller delegates to Spring Security. AuthenticationManager checks the credentials using your UserDetailsService and PasswordEncoder, and a small service creates the token.
If the credentials are wrong, authenticate throws an AuthenticationException (for example BadCredentialsException), and your method never reaches the return. Map it to a 401 Unauthorized response in a @RestControllerAdvice or the security configuration, and use the same generic message for unknown users and wrong passwords so attackers cannot probe for valid accounts.
Avoid hand-rolling a token class that you instantiate with new. If you use JWTs, Spring Security ships a JwtEncoder (module spring-security-oauth2-jose) that you can inject:
You provide the JwtEncoder bean, for example a NimbusJwtEncoder built from an RSA key pair, and configure the resource server with a matching JwtDecoder. Keep tokens short-lived and load signing keys from a secret store, not from source code.
The login endpoint must be reachable without a token, everything else protected. Expose the AuthenticationManager from the configuration:
Disabling CSRF is acceptable here only because the API is stateless and authenticates with a bearer token in the Authorization header. If you use cookie-based sessions, keep CSRF protection on.
roles.Query and form parameters can end up in logs and URLs, and a JSON body suits API clients. Use @RequestParam or form handling only for classic HTML form logins handled by Spring Security's form login.
No. It only deserializes the body. Authentication happens when you pass the credentials to AuthenticationManager, or when a security filter handles the request.
Bind a RegisterRequest record with validation, hash the password with PasswordEncoder.encode, save the user, and return 201 Created. Do not return the password hash.
A 415 means the Content-Type: application/json header is missing. A 400 means the JSON is malformed or fails validation.
@RequestBody turns a login or registration payload into a typed object, and Spring Security does the actual authentication. Validate the request, authenticate through AuthenticationManager, issue short-lived tokens with a proper encoder, and keep credentials out of URLs and logs.