In a Spring Boot application, you usually do not use @RequestMapping to process a login. Spring Security's formLogin() filter handles the POST /login credential check for you. You use @GetMapping("/login") (a shortcut for @RequestMapping(method = GET)) only to show a custom login page, and let the security filter chain do authentication.
The original idea of writing a POST /login handler that compares a username and password yourself is both unnecessary and risky. This article shows the correct setup with Spring Security 6 and later.
Login involves two requests:
GET /login shows the form. This is a normal controller method, and this is where @RequestMapping or @GetMapping is used.POST /login submits the credentials. Spring Security's UsernamePasswordAuthenticationFilter intercepts it before it reaches any controller, authenticates the user and redirects.If you also declare a controller method for POST /login, it is never reached for a successful or failed form login, because the filter chain ends the request first.
With Maven:
You need spring-boot-starter-web, spring-boot-starter-security and spring-boot-starter-thymeleaf (for the page). Spring Boot 3 and later uses the jakarta.* namespace and Spring Security 6 or later, which uses the lambda DSL shown below.
Declare a SecurityFilterChain bean. The older WebSecurityConfigurerAdapter was removed in Spring Security 6, so do not use it.
loginPage("/login") tells Spring Security to use your page and keep the same URL for the form submission. The in-memory user is for demonstration only. In production, load users from a database through a UserDetailsService and store passwords hashed with BCrypt or the delegating encoder.
The controller only returns the view name:
An equivalent version is @RequestMapping(value = "/login", method = RequestMethod.GET). The GET shortcut is shorter and clearer.
Put this in src/main/resources/templates/login.html:
Thymeleaf's th:action adds the CSRF token automatically, and CSRF protection is on by default for form posts. The field names username and password are the defaults the filter reads.
For a JSON or token based API you do not post to the form filter. You expose an endpoint and call the AuthenticationManager:
Here @RequestMapping("/api/auth") sets the prefix. You must expose an AuthenticationManager bean, permit /api/auth/login in the security rules, and return a token (for example a JWT) or establish a session yourself.
home after a manual check without creating a security context.Secure, HttpOnly and SameSite session cookies.No. With formLogin(), Spring Security processes the POST /login submission. You only need a GET method to render the page.
The URL must be permitted for anonymous users with permitAll(), and the controller must map the same path as loginPage().
Yes, with usernameParameter("email") and passwordParameter("pass") in the formLogin configuration.
Use an endpoint that calls AuthenticationManager, or configure an OAuth2 resource server with tokens. Avoid hand-written credential checks.
@RequestMapping (or @GetMapping) only serves the login page. Spring Security's SecurityFilterChain performs the authentication, so configure formLogin() with your loginPage, keep CSRF on, hash passwords, and use an AuthenticationManager endpoint for API logins.