How do you configure a custom login page in Spring Security?
A) By overriding the default login URL in the security configuration
B) By creating a REST API for login
C) By disabling security altogether
D) By setting up a custom database schema
Answer:
A) By overriding the default login URL in the security configuration
Explanation:
To configure a custom login page in Spring Security, you override the default login URL in the security configuration class. This is typically done by extending the WebSecurityConfigurerAdapter
and overriding the configure(HttpSecurity http)
method.
For example:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/custom-login")
.permitAll();
}
In this configuration, .loginPage("/custom-login")
specifies the URL of the custom login page. Users will be redirected to this page when authentication is required. The .permitAll()
method ensures that all users can access the login page, even if they are not authenticated.