Spring, Unable to build project with authentication manager bean provided

Viewed 25

I'm trying to build a project using gradle version 7.5, but it always gives failed result. There's no error log showing I even try with --debug and --stacktrace option, but still don't find anything. So I try remove some code to get clue and this is what I've found. This is how I config the security for spring.

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig {

    @Autowired
    private UserDetailServiceImpl userDetailService;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http.csrf().disable()
                   .authorizeHttpRequests((requests) -> requests.antMatchers("/**").permitAll())
                   .logout(LogoutConfigurer::permitAll)
                   .build();
    }
}

If I remove this bean. The project will be built successfully, but If I bring it back it will be failed (without any errors log).

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

I wonder what happens here? I use spring boot version 2.7.3 (WebSecurityConfigurerAdapter is deprecated) and gradle version 7.5

All the error messages that I could gather

> Task :test FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///C:/Users/patrick/Desktop/WidePeepoHappy/build/reports/tests/test/index.html

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.5/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 21s
10 actionable tasks: 3 executed, 7 up-to-date
1 Answers

The issue is That some tests Fails, its the test task that gradle automatically execute with each build/run.

The error log is clear, You have to check

file:///C:/Users/patrick/Desktop/WidePeepoHappy/build/reports/tests/test/index.html

To see the test results, and check whats fails.

If you want to build/run a Gradle project without tests, You can use gradle build -x test for building with test exclusion, or gradle bootrun -x test for running without having the need to execute test task.

But this is highly not recommended, But it might be helpful in development progress to build/run before you update the tests.

Related