From bca5ffe971a6bc16790806943112798b194e36bb Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Sat, 29 Oct 2022 13:49:55 +0530 Subject: [PATCH] JAVA-14890 Update spring-security-oauth2-sso module under spring-security-modules to remove usage of deprecated WebSecurityConfigurerAdapter (#12913) * JAVA-14890 Update spring-security-oauth2-sso module under spring-security-modules to remove usage of deprecated WebSecurityConfigurerAdapter * JAVA-14890 Commit remaining code --- .../intro/config/WebSecurityConfig.java | 38 ++++++----- .../server/config/WebSecurityConfig.java | 66 +++++++++++-------- 2 files changed, 63 insertions(+), 41 deletions(-) diff --git a/spring-security-modules/spring-security-oauth2-sso/spring-security-sso-kerberos/src/main/java/com/baeldung/intro/config/WebSecurityConfig.java b/spring-security-modules/spring-security-oauth2-sso/spring-security-sso-kerberos/src/main/java/com/baeldung/intro/config/WebSecurityConfig.java index cc694a3b83..368d6e471f 100644 --- a/spring-security-modules/spring-security-oauth2-sso/spring-security-sso-kerberos/src/main/java/com/baeldung/intro/config/WebSecurityConfig.java +++ b/spring-security-modules/spring-security-oauth2-sso/spring-security-sso-kerberos/src/main/java/com/baeldung/intro/config/WebSecurityConfig.java @@ -6,39 +6,47 @@ import org.springframework.core.io.FileSystemResource; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; import org.springframework.security.kerberos.authentication.KerberosAuthenticationProvider; import org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider; import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosClient; import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator; import org.springframework.security.kerberos.web.authentication.SpnegoAuthenticationProcessingFilter; import org.springframework.security.kerberos.web.authentication.SpnegoEntryPoint; +import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; import com.baeldung.intro.security.DummyUserDetailsService; @Configuration -public class WebSecurityConfig extends WebSecurityConfigurerAdapter { +public class WebSecurityConfig extends AbstractHttpConfigurer { - @Override - protected void configure(HttpSecurity http) throws Exception { - http.authorizeRequests() - .anyRequest() - .authenticated() - .and() - .addFilterBefore(spnegoAuthenticationProcessingFilter(authenticationManagerBean()), BasicAuthenticationFilter.class); + public static WebSecurityConfig securityConfig() { + return new WebSecurityConfig(); } @Override + public void configure(HttpSecurity http) throws Exception { + AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class); + http.addFilterBefore(spnegoAuthenticationProcessingFilter(authenticationManager), BasicAuthenticationFilter.class); + } + @Bean - public AuthenticationManager authenticationManagerBean() throws Exception { - return super.authenticationManagerBean(); + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + http.authorizeRequests() + .anyRequest() + .authenticated() + .and() + .apply(securityConfig()); + return http.build(); } - @Override - protected void configure(AuthenticationManagerBuilder auth) throws Exception { - auth.authenticationProvider(kerberosAuthenticationProvider()) - .authenticationProvider(kerberosServiceAuthenticationProvider()); + @Bean + public AuthenticationManager authManager(HttpSecurity http) throws Exception { + return http.getSharedObject(AuthenticationManagerBuilder.class) + .authenticationProvider(kerberosAuthenticationProvider()) + .authenticationProvider(kerberosServiceAuthenticationProvider()) + .build(); } @Bean diff --git a/spring-security-modules/spring-security-oauth2-sso/spring-security-sso-kerberos/src/main/java/kerberos/server/config/WebSecurityConfig.java b/spring-security-modules/spring-security-oauth2-sso/spring-security-sso-kerberos/src/main/java/kerberos/server/config/WebSecurityConfig.java index 5d241c5823..80d81dd268 100644 --- a/spring-security-modules/spring-security-oauth2-sso/spring-security-sso-kerberos/src/main/java/kerberos/server/config/WebSecurityConfig.java +++ b/spring-security-modules/spring-security-oauth2-sso/spring-security-sso-kerberos/src/main/java/kerberos/server/config/WebSecurityConfig.java @@ -9,18 +9,19 @@ import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 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.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; import org.springframework.security.kerberos.authentication.KerberosAuthenticationProvider; import org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider; import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosClient; import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator; import org.springframework.security.kerberos.web.authentication.SpnegoAuthenticationProcessingFilter; import org.springframework.security.kerberos.web.authentication.SpnegoEntryPoint; +import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; @Configuration @EnableWebSecurity -class WebSecurityConfig extends WebSecurityConfigurerAdapter { +class WebSecurityConfig extends AbstractHttpConfigurer { @Value("${app.service-principal}") private String servicePrincipal; @@ -28,32 +29,45 @@ class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Value("${app.keytab-location}") private String keytabLocation; - @Override - protected void configure(HttpSecurity http) throws Exception { - http.exceptionHandling() - .authenticationEntryPoint(spnegoEntryPoint()) - .and() - .authorizeRequests().antMatchers("/", "/home").permitAll() - .anyRequest().authenticated() - .and() - .formLogin().loginPage("/login").permitAll() - .and() - .logout().permitAll() - .and() - .addFilterBefore(spnegoAuthenticationProcessingFilter(authenticationManagerBean()), - BasicAuthenticationFilter.class); - } + public static WebSecurityConfig securityConfig() { + return new WebSecurityConfig(); + } - @Bean - public AuthenticationManager anAuthenticationManager() throws Exception { - return authenticationManager(); - } + @Override + public void configure(HttpSecurity http) throws Exception { + AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class); + http.addFilterBefore(spnegoAuthenticationProcessingFilter(authenticationManager), BasicAuthenticationFilter.class); + } - @Override - protected void configure(AuthenticationManagerBuilder auth) throws Exception { - auth.authenticationProvider(kerberosAuthenticationProvider()) - .authenticationProvider(kerberosServiceAuthenticationProvider()); - } + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + http.exceptionHandling() + .authenticationEntryPoint(spnegoEntryPoint()) + .and() + .authorizeRequests() + .antMatchers("/", "/home") + .permitAll() + .anyRequest() + .authenticated() + .and() + .formLogin() + .loginPage("/login") + .permitAll() + .and() + .logout() + .permitAll() + .and() + .apply(securityConfig()); + return http.build(); + } + + @Bean + public AuthenticationManager authManager(HttpSecurity http) throws Exception { + return http.getSharedObject(AuthenticationManagerBuilder.class) + .authenticationProvider(kerberosAuthenticationProvider()) + .authenticationProvider(kerberosServiceAuthenticationProvider()) + .build(); + } @Bean public KerberosAuthenticationProvider kerberosAuthenticationProvider() {