[Spring Error] Spring Security Deprecated 해결
시큐리티 강의를 들으면서 SecurityConfig을 다음과 같이 설정해줬다.
@Configuration // IoC 빈(bean)을 등록
@EnableWebSecurity // 필터 체인 관리 시작 어노테이션
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Bean
public BCryptPasswordEncoder encodePwd() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/user/**").authenticated()
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER')")
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN') and hasRole('ROLE_USER')")
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.anyRequest().permitAll()
.and()
.formLogin()
.loginPage("/loginForm")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/");
}
}
하지만 스프링 버전이 업데이트 되면서 extends한 WebSecurityConfigurerAdapter와 그 외의 것들이 Deprecated되었다.
formlogin() is deprecated and marked for removal
csrf() is deprecated and marked for removal
라며 오류가 발생했다. 또한 WebSecurityConfigurerAdapter은 아예 사용할 수 없었다.
WebSecurityConfigurerAdapter의 공식 문서를 살펴보면,
Deprecated. Use a SecurityFilterChain Bean to configure HttpSecurity or a WebSecurityCustomizer Bean to configure WebSecurity.
기존에는 WebSecurityConfigurerAdapter를 상속받아 오버라이딩 하는 방식이었는데, 현재는 모두 Bean으로 등록해야 한다.
공식문서를 살펴보면 어떻게 수정해야 하는지 나와있다.
Spring Security without the WebSecurityConfigurerAdapter
In Spring Security 5.7.0-M2 we deprecated the WebSecurityConfigurerAdapter, as we encourage users to move towards a component-based security configuration. To assist with the transition to this new style of configuration, we have compiled a list of common
spring.io
강의자료가 업데이트되지 않아서 애먹고 있었는데, 다음과 같이 수정하니 시큐리티 로그인도 잘 되고 프로젝트가 잘 작동했다!!
@Configuration // IoC 빈(bean)을 등록
@EnableWebSecurity // 필터 체인 관리 시작 어노테이션
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf(csrf -> csrf.disable())
.authorizeRequests()
.requestMatchers("/user/**").authenticated()
.requestMatchers("/manager/**").hasAnyRole("MANAGER", "ADMIN")
.requestMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().permitAll()
.and()
.formLogin(form -> form
.loginPage("/loginForm")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/")
.permitAll());
return http.build();
}
}
다음과 같이 수정하면 잘 실행된다!
시큐리티 공부할게 늘어났다...😂