Form-based login lets users enter their username and password on a webpage to access a secure area. It makes login easy and user-friendly.
Form-based login configuration in Spring Boot
Start learning this pattern below
Jump into concepts and practice - no test required
http
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/home")
.failureUrl("/login?error")
.permitAll()formLogin() enables form-based login in Spring Security.
loginPage() sets the URL of your custom login page.
http .formLogin()
/custom-login and allows everyone to access it.http
.formLogin()
.loginPage("/custom-login")
.permitAll()http
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/dashboard")
.failureUrl("/login?error")
.permitAll()This example shows how to configure Spring Security to use a custom login page at /login. The SecurityConfig class sets up the form login with success and failure URLs. The LoginController serves the login page. The login form posts credentials to /login for authentication.
import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class LoginController { @GetMapping("/login") public String login() { return "login"; // returns login.html view } } @Configuration public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(auth -> auth .requestMatchers("/login", "/css/**").permitAll() .anyRequest().authenticated() ) .formLogin(form -> form .loginPage("/login") .defaultSuccessUrl("/home", true) .failureUrl("/login?error") .permitAll() ); return http.build(); } } // login.html (Thymeleaf template example) // <html> // <body> // <form action="/login" method="post"> // <label for="username">Username:</label> // <input type="text" id="username" name="username" /> // <label for="password">Password:</label> // <input type="password" id="password" name="password" /> // <button type="submit">Log In</button> // <div th:if="${param.error}">Invalid username or password.</div> // </form> // </body> // </html>
Always allow everyone to access the login page using permitAll() so users can reach it.
Use defaultSuccessUrl with true as second argument to always redirect after login.
Customize the login page HTML to improve user experience and accessibility.
Form-based login lets users sign in via a webpage you control.
Configure it in Spring Security with formLogin() and set your login page URL.
Handle success and failure redirects to guide users after login attempts.
Practice
formLogin() in Spring Security?Solution
Step 1: Understand formLogin() role
TheformLogin()method in Spring Security enables users to log in using a web form.Step 2: Compare with other options
Other options like disabling login or configuring database are unrelated toformLogin().Final Answer:
To enable form-based login for user authentication -> Option AQuick Check:
formLogin() enables form login [OK]
- Confusing formLogin() with database setup
- Thinking formLogin() disables login
- Mixing formLogin() with API configuration
Solution
Step 1: Identify correct method for login page URL
The method to set a custom login page URL isloginPage()used afterformLogin().Step 2: Verify syntax correctness
Only http.formLogin().loginPage("/custom-login") uses the correct method name and syntax:loginPage("/custom-login").Final Answer:
http.formLogin().loginPage("/custom-login") -> Option BQuick Check:
loginPage() sets custom login URL [OK]
- Using incorrect method names like setLoginUrl()
- Confusing loginPage() with other methods
- Missing parentheses or quotes
/login?
http
.authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
.formLogin(form -> form.loginPage("/login").permitAll());Solution
Step 1: Analyze authorizeHttpRequests configuration
All requests require authentication because ofanyRequest().authenticated().Step 2: Analyze formLogin configuration
The login page is customized to/loginandpermitAll()allows everyone to access it without login.Final Answer:
Users see a custom login page at /login and can access it without authentication -> Option DQuick Check:
Custom login page with permitAll() means public access [OK]
- Assuming /login requires authentication
- Thinking default login page is used
- Ignoring permitAll() effect
http
.formLogin()
.loginPage("/my-login")
.permitAll();Solution
Step 1: Check method chaining correctness
In Spring Security,permitAll()is used on authorization rules, not directly onformLogin().Step 2: Understand correct usage
To allow public access to the login page,permitAll()should be called on the authorization configuration for the login page URL.Final Answer:
permitAll() should be called on authorizeHttpRequests, not formLogin -> Option AQuick Check:
permitAll() belongs to authorization, not formLogin [OK]
- Calling permitAll() on formLogin()
- Misplacing loginPage() call
- Assuming permitAll() is invalid
/user-login
- The login page is accessible without authentication
- All other pages require login
Which configuration snippet correctly achieves this?Solution
Step 1: Permit access to the login page
UserequestMatchers("/user-login").permitAll()to allow unauthenticated access to the login page.Step 2: Require authentication for all other requests
UseanyRequest().authenticated()to protect all other endpoints.Step 3: Configure form login with custom login page
formLogin(form -> form.loginPage("/user-login"))sets the custom login page.Final Answer:
http.authorizeHttpRequests(auth -> auth.requestMatchers("/user-login").permitAll().anyRequest().authenticated()) .formLogin(form -> form.loginPage("/user-login")); -> Option CQuick Check:
Properly permits login page and protects others [OK]
- Not permitting the login page URL (option A)
- Invalid chaining after formLogin.permitAll() (option B)
- Permitting all requests (option D)
