Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Security and hardening

Spring Security flags formLogin(), exceptionHandling() and and() chains

Your error

formLogin() in HttpSecurity has been deprecated and marked for removal3 more lines it can show

Fix it fast

Most common cause, 1 of 2

Configurer blocks are chained with no-argument calls and .and()

Give every configurer a lambda that receives it, put its options inside, and delete the .and() calls.

Fix
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
        .authorizeHttpRequests((authorize) -> authorize
            .requestMatchers("/blog/**").permitAll()
            .anyRequest().authenticated()
        )
        .formLogin((formLogin) -> formLogin
            .loginPage("/login")
            .permitAll()
        )
        .exceptionHandling((exceptionHandling) -> exceptionHandling
            .accessDeniedPage("/errors/access-denied")
        );
    return http.build();
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

formLogin(), exceptionHandling(), csrf() without arguments and the .and() chain are deprecated for removal in Spring Security 6.1 to 6.5 and are gone in 7.x, so Boot 4 builds stop compiling. Pass a lambda to each method instead, such as formLogin(form -> form.loginPage("/login")), and drop every .and(). The lambda style has worked since 5.2.

  • Applies to Spring Security 7.2.0 (Spring Boot 4.2.0), Spring Security 6.5.11
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Find every old-style call in the security configuration, and check which Spring Security line the build resolves. On 6.x these lines only warn; on 7.x each one is a compile error.

Run this first
grep -rnE "\.and\(\)|(formLogin|exceptionHandling|httpBasic|csrf|cors|logout|rememberMe)\(\)" src/main/java
mvn dependency:tree -Dincludes=org.springframework.security:spring-security-config

Then find the line you see in this list and open its fix:

  1. 1Configurer blocks are chained with no-argument calls and .and()grep finds formLogin(), exceptionHandling() or authorizeHttpRequests() followed later by .and(); spring-security-config is 6.5.x with warnings, or 7.x with cannot be applied to given types
  2. 2One-line switches such as csrf().disable() or rememberMe() use the old formThe grep hits are short calls with no .and(), for example http.csrf().disable() or .rememberMe() at the end of a chain

What does the error look like?

  • formLogin() in HttpSecurity has been deprecated and marked for removal
  • method formLogin in class HttpSecurity cannot be applied to given types;
  • reason: actual and formal argument lists differ in length
  • uses or overrides a deprecated API that is marked for removal.

Cause 1 of 2

Configurer blocks are chained with no-argument calls and .and()

You seegrep finds formLogin(), exceptionHandling() or authorizeHttpRequests() followed later by .and(); spring-security-config is 6.5.x with warnings, or 7.x with cannot be applied to given types

Check

The no-argument configurer methods return a sub-configurer, and .and() was needed to climb back to HttpSecurity. In 6.5.11 the API docs mark each of them For removal in 7.0, and the 7.2.0 HttpSecurity API lists only the versions that take a Customizer. Spring's migration notes say the old style is not valid in Spring Security 7.

Check
grep -rn "\.and()" src/main/java

Fix

Give every configurer a lambda that receives it, put its options inside, and delete the .and() calls. HttpSecurity comes back after each lambda, so the next method chains straight on.

Fix
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
        .authorizeHttpRequests((authorize) -> authorize
            .requestMatchers("/blog/**").permitAll()
            .anyRequest().authenticated()
        )
        .formLogin((formLogin) -> formLogin
            .loginPage("/login")
            .permitAll()
        )
        .exceptionHandling((exceptionHandling) -> exceptionHandling
            .accessDeniedPage("/errors/access-denied")
        );
    return http.build();
}

Verify

The project compiles on Spring Security 7.2.0, and the login page and access-denied page behave as before.

Verify
mvn -q clean compile

Confirmed by docs.spring.io

Did this fix it?

Cause 2 of 2

One-line switches such as csrf().disable() or rememberMe() use the old form

You seeThe grep hits are short calls with no .and(), for example http.csrf().disable() or .rememberMe() at the end of a chain

Check

These calls hit the same removed methods, just without a long chain around them. A feature you want with default settings needs Customizer.withDefaults(), which the migration guide describes as a shortcut for an empty lambda; a feature you switch off is disabled inside its own lambda.

Check
grep -rnE "(csrf|cors|httpBasic|rememberMe|logout)\(\)" src/main/java

Fix

Replace each call with its Customizer version. withDefaults() is a static method on org.springframework.security.config.Customizer.

Fix
import org.springframework.security.config.Customizer;

http
    .csrf((csrf) -> csrf.disable())
    .formLogin(Customizer.withDefaults())
    .rememberMe(Customizer.withDefaults());

Verify

The grep prints nothing: the compile output has no marked for removal line left.

Verify
mvn clean compile 2>&1 | grep -i "marked for removal"

Confirmed by docs.spring.io

Did this fix it?

How do I stop it coming back?

  • Write new security configuration only in the lambda DSL; it has worked since Spring Security 5.2, so no version gate is needed.
  • While still on 6.5, treat every marked for removal line in the compile log as a blocker in review, so nothing old is left when Boot 4 arrives.
  • Keep the whole SecurityFilterChain in one configuration class, so the conversion is a single reviewed change.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Dispatch Pro.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.