Disabling OAuth2 Security for Integration Tests with @TestConfiguration
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
@TestConfiguration
public class IntegrationTestConfig {
/**
* Configures a security filter chain to disable OAuth2 security for integration tests.
*
* @param http the {@link HttpSecurity} to configure
* @return the {@link SecurityFilterChain} configured to disable OAuth2 security
* @throws Exception if an error occurs while configuring security
*/
@Bean
public SecurityFilterChain disableOAuth2Security(HttpSecurity http) throws Exception {
return http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(authorize -> authorize
.anyRequest().permitAll())
.build();
}
}Last updated