본문 바로가기

Server/Spring

[우당탕탕 개발일지] WebSecurityConfigurerAdapter 지원 불가

반응형
SMALL

이번 주부터 Spring Security 관련 강의 듣기를 시작했다. ((두근두근)) 어려움의 프레임으로 돌돌 쌓인 보안 관련 친구라 설렘 반 두려움 반 입니다!

 

그리고 설정에서부터 막혔다. ㅇㅁㅇ !! 강사님은 WebSecurityConfigurerAdapter을 상속해서 쓰셨는데, 찾아보니 지원 중지 됐다고 한다. (WHAT ?!) 2020년까지도 멀쩡했던 친구 같은데,,, 왜,,, ㅠㅅㅠ

 

해결방법은 따로 Bean을 등록해서 사용해주는 것이었다. 복잡시러워라 ㅇㅅㅇ

 

https://spring.io/blog/2022/02/21/spring-security-without-the-websecurityconfigureradapter

 

Spring Security without the WebSecurityConfigurerAdapter

<p>In Spring Security 5.7.0-M2 we <a href="https://github.com/spring-projects/spring-security/issues/10822">deprecated</a> the <code>WebSecurityConfigurerAdapter</code>, as we encourage users to move towards a component-based security configuration.</p> <p

spring.io

 

상속해서 사용하는 방법은 천지차이니까 다른 글 보면 괜히 더 헷갈렸다. ((물론 이것도 다른 글 중 하나지만)) 그러니 위에 공식 홈페이지를 적극 참고하자!!

 

나와 같은 경우는 본래 코드가,

 

@Configuration
@EnableWebSecurity
public class WebSecurityConfigure extends WebSecurityConfigurerAdapter {

  @Override
  public void configure(WebSecurity web) {
    web.ignoring().antMatchers("/assets/**");
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeRequests()
        .antMatchers("/me").hasAnyRole("USER", "ADMIN")
        .anyRequest().permitAll()
        .and()
      .formLogin()
        .defaultSuccessUrl("/")
        .permitAll()
        .and()
    ;
  }

이러하였다. 복붙하면 빨간 줄 쥬르륵 뜨고 난리가 나요!!

 

그래서 공식 홈페이지를 참고해서 ...

 

@Configuration
@EnableWebSecurity
public class WebSecurityConfigure {

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().requestMatchers("/assets/**");
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests()
                .requestMatchers("/me").hasAnyRole("USER", "ADMIN")
                .anyRequest().permitAll()
                .and()
                .formLogin()
                .defaultSuccessUrl("/")
                .permitAll()
                .and();

        return http.build();
    }
}

Bean을 등록해주는 방식으로 고쳤다! 강의 보고 냅다 따라서 설정하는 중이라 뭐가 뭔지도 모르겠음,, 하지만 얼레벌레 해봤음..

 

또!!!!

 

authorizeRequests와 antMatchers도 일정 버전 이상부터 지원하지 않는 것 같았다. 그래서 authorizeHttpRequests와 requestMathcers로 대체하여 사용해줬다.

 

설정까지 힘겹게 왔으니 마저 강의 들으러 가야지 ㅇㅅㅇ ..

 

 

빠이!

반응형
LIST