spring-security
disable-login-screen
authentication
web-security
spring-boot

How to disable the Spring Security login screen

Codemia Knowledge Hub ·

Call .formLogin(AbstractHttpConfigurer::disable) in your SecurityFilterChain bean. Spring Security then stops generating the /login page. Spring Boot 3.x uses this component-based SecurityFilterChain approach; older 2.x projects may still extend the now-deprecated WebSecurityConfigurerAdapter.

Where the login screen comes from

Adding spring-boot-starter-security to the classpath makes Spring Boot auto-configure a security filter chain with form-based login. That default chain:

  • Protects every endpoint with authentication
  • Generates a login page at /login
  • Creates a single user with a random password printed to the console at startup
  • Enables session-based authentication

That is handy for prototyping, but most production applications use something else: JWT tokens, OAuth2, API keys, or a custom login UI served by a frontend framework.

Spring Boot 3.x / Spring Security 6.x

WebSecurityConfigurerAdapter was removed in Spring Security 6. Define a @Bean method that returns a SecurityFilterChain:

java
1import org.springframework.context.annotation.Bean;
2import org.springframework.context.annotation.Configuration;
3import org.springframework.security.config.annotation.web.builders.HttpSecurity;
4import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
5import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
6import org.springframework.security.web.SecurityFilterChain;
7
8@Configuration
9@EnableWebSecurity
10public class SecurityConfig {
11
12    @Bean
13    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
14        http
15            .authorizeHttpRequests(auth -> auth
16                .anyRequest().authenticated()
17            )
18            .formLogin(AbstractHttpConfigurer::disable);
19
20        return http.build();
21    }
22}

This disables the generated login page while keeping all other security features active. Requests to /login now return a 403 instead of rendering a form.

Spring Boot 2.x / Spring Security 5.x

Older projects may still use the adapter pattern:

java
1import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
3import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
4
5@EnableWebSecurity
6public class SecurityConfig extends WebSecurityConfigurerAdapter {
7
8    @Override
9    protected void configure(HttpSecurity http) throws Exception {
10        http
11            .authorizeRequests()
12                .anyRequest().authenticated()
13            .and()
14            .formLogin().disable();
15    }
16}

WebSecurityConfigurerAdapter is deprecated since Spring Security 5.7 and removed in 6.0, so it will not compile in a Spring Boot 3.x project. Use the SecurityFilterChain bean for anything new.

Replace form login with another mechanism

Disabling form login alone does not open your endpoints. They still require authentication, and with no alternative mechanism every request fails with 401 or 403. Always pair the disable with the authentication your application actually uses.

HTTP Basic

Suitable for internal services, CLI tools, or APIs behind a gateway:

java
1@Bean
2public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3    http
4        .authorizeHttpRequests(auth -> auth
5            .anyRequest().authenticated()
6        )
7        .formLogin(AbstractHttpConfigurer::disable)
8        .httpBasic(Customizer.withDefaults());
9
10    return http.build();
11}

Stateless JWT

For REST APIs where clients send a Bearer token with each request:

java
1@Bean
2public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3    http
4        .authorizeHttpRequests(auth -> auth
5            .requestMatchers("/api/auth/**").permitAll()
6            .anyRequest().authenticated()
7        )
8        .formLogin(AbstractHttpConfigurer::disable)
9        .sessionManagement(session -> session
10            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
11        )
12        .csrf(AbstractHttpConfigurer::disable)
13        .addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
14
15    return http.build();
16}

Stateless APIs disable both form login and session creation. CSRF protection is also typically disabled here, because the browser-cookie attack vector does not apply when tokens travel in headers. Do not disable CSRF if the application still uses session-based authentication; that opens a real vulnerability.

OAuth2 resource server

For services that validate tokens issued by an external identity provider:

java
1@Bean
2public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3    http
4        .authorizeHttpRequests(auth -> auth
5            .anyRequest().authenticated()
6        )
7        .formLogin(AbstractHttpConfigurer::disable)
8        .oauth2ResourceServer(oauth2 -> oauth2
9            .jwt(Customizer.withDefaults())
10        );
11
12    return http.build();
13}

Permit all requests

For local development, or when an API gateway handles security entirely:

java
1@Bean
2public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3    http
4        .authorizeHttpRequests(auth -> auth
5            .anyRequest().permitAll()
6        )
7        .formLogin(AbstractHttpConfigurer::disable)
8        .csrf(AbstractHttpConfigurer::disable);
9
10    return http.build();
11}

This is the nuclear option. Never deploy it to production.

Session management policy

Once form login is gone, review the session policy. The default IF_REQUIRED creates sessions when needed, which may not fit your new authentication model.

PolicyBehaviorUse Case
STATELESSNo session created or usedJWT APIs, microservices
IF_REQUIREDSession created only when neededTraditional web apps with custom login UI
ALWAYSSession always createdLegacy apps that depend on session state
NEVERSpring Security never creates a session but uses one if it existsServlet-managed sessions

Set it in the filter chain:

java
.sessionManagement(session -> session
    .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
)

Turning off security auto-configuration completely

To remove Spring Security's auto-configuration entirely (not just the login page), exclude it at the application level:

java
1@SpringBootApplication(exclude = {
2    SecurityAutoConfiguration.class,
3    ManagementWebSecurityAutoConfiguration.class
4})
5public class MyApplication {
6    public static void main(String[] args) {
7        SpringApplication.run(MyApplication.class, args);
8    }
9}

Or in application.properties:

properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration

This removes all security behavior: no authentication, no authorization, no CSRF. In production it is almost always a mistake; prefer the targeted .formLogin(AbstractHttpConfigurer::disable).

Spring Boot Actuator has its own security auto-configuration (ManagementWebSecurityAutoConfiguration). If you forget it, actuator endpoints may still show a login prompt after you configure your main filter chain.

Comparing the approaches

GoalMethodSide Effects
Remove login page only.formLogin(AbstractHttpConfigurer::disable)Endpoints still require authentication; need alternate auth mechanism
Replace with HTTP Basic.httpBasic(Customizer.withDefaults())Browser shows native credential dialog
Replace with JWTCustom filter + STATELESS sessionMust implement token validation; CSRF disabled
Replace with OAuth2.oauth2ResourceServer()Requires external identity provider configuration
Remove all security@SpringBootApplication(exclude = ...)No authentication, no authorization, no CSRF

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design