JAVA-14875 Update spring-security-web-rest-custom module under spring-security-modules to remove usage of deprecated WebSecurityConfigurerAdapter (#12805)

This commit is contained in:
anuragkumawat 2022-10-05 23:47:54 +05:30 committed by GitHub
parent b95f463a7e
commit 5d5e8121aa
1 changed files with 19 additions and 18 deletions

View File

@ -1,40 +1,41 @@
package com.baeldung.config.parent; package com.baeldung.config.parent;
import com.baeldung.security.CustomAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 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.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.web.SecurityFilterChain;
import com.baeldung.security.CustomAuthenticationProvider;
@Configuration @Configuration
//@ImportResource({ "classpath:webSecurityConfig.xml" }) //@ImportResource({ "classpath:webSecurityConfig.xml" })
@EnableWebSecurity @EnableWebSecurity
@ComponentScan("com.baeldung.security") @ComponentScan("com.baeldung.security")
public class SecurityConfig extends WebSecurityConfigurerAdapter { public class SecurityConfig {
@Autowired @Autowired
private CustomAuthenticationProvider authProvider; private CustomAuthenticationProvider authProvider;
public SecurityConfig() { @Bean
super(); public AuthenticationManager authManager(HttpSecurity http) throws Exception {
AuthenticationManagerBuilder authenticationManagerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class);
authenticationManagerBuilder.authenticationProvider(authProvider);
return authenticationManagerBuilder.build();
} }
@Override @Bean
protected void configure(final AuthenticationManagerBuilder auth) throws Exception { public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
auth.authenticationProvider(authProvider); http.authorizeRequests()
} .anyRequest()
.authenticated()
@Override
protected void configure(final HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests().anyRequest().authenticated()
.and() .and()
.httpBasic(); .httpBasic();
// @formatter:on return http.build();
} }
} }