• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

SecurityConfig 파일 작성중,, WebSecurityConfigurerAdapter 가 deprecated 됬다고 해서 extends가 안됩니다.

23.08.18 00:02 작성 조회수 577

0

안녕하세요. 강의를 열심히 들으려고 하는 한 직장인입니다.

 

해당 수업 (회원가입 컨트롤러) 를 듣는도중, SecurityConfig 파일 만드는 부분에서 WebSecurityConfigurerAdapter 가 deprecated 되었다고 extends가 안되고 있습니다.

 

저는 현재 스프링 시큐리티 버전을.. 6 버전대 사용중이에요.

 

정확히는

스크린샷 2023-08-17 오후 11.53.12.png6.1.2 버전 사용하는것 같네요.. (이렇게 버전확인해도되는건지 몰르겠습니다..ㅠㅠ)

 

로그분석과,, 구글링을 좀 해본 결과,

RequestMatchers(MvcRequestMatcher) or

RequestMatchers(AntPathRequestMatcher) 의 패턴으로 사용을 해야한다고 해서,,

 

결국 소스를 수정하여. permitAll()은 해결하였습니다.

그런데 강의에서 프로필 요청 url은 httpMethod중 get만 허용해야 하는 조건에서,

mvcMatchers(HttpMethod.GET, "/profile/*").permitAll()

부분을 도무지 어떻게 대치해야할지를 모르겠습니다.

 

제가 현재 까지 작성한 소스 공유 드립니다.

 

package com.studyolle.config;


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.web.SecurityFilterChain;
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;


@Configuration
@EnableWebSecurity // 시큐리티 활성화 -> 기본 스프링 필터체인에 등록
public class SecurityConfig {

    @Bean
    SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
        MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector);
        http.authorizeHttpRequests((requests) -> requests
                .requestMatchers(
                         mvcMatcherBuilder.pattern("/"),                    mvcMatcherBuilder.pattern("/login"),                mvcMatcherBuilder.pattern("/sign-up")
                        ,mvcMatcherBuilder.pattern("/check-email"),         mvcMatcherBuilder.pattern("/check-email-token"),    mvcMatcherBuilder.pattern("/email-login")
                        ,mvcMatcherBuilder.pattern("/check-email-login"),   mvcMatcherBuilder.pattern("/login-link"),           mvcMatcherBuilder.pattern("/profile/*")
                ).permitAll()
                .anyRequest().authenticated()
        );



        return http.build();
    }


}

 

 

해결방안을 자세하게 알려주시면 감사하겠습니다. 시간이 지남에 따라 스프링 정책은 계속 deprecated 되는 것 같은데,,, 너무 나도 배울게 많다고 생각이 됩니다. 잠깐이라도 놓치면 개발의 세계와 너무 멀어지는 느낌이 드네요. 제가 많이 부족하여 따끔한 쓴소리도 같이 부탁드릴께요.

 

진심 열심히 하려고 노력중입니다.

감사합니다. 빠른 답변 부탁드리겠습니다.

답변 1

답변을 작성해보세요.

1

안녕하세요.

제가 강의를 제작했을 때 사용했던 버전이 아니라서 저도 그렇게 API가 바뀐줄은 몰랐네요. 강의를 수료하기 전까지는 우선 강의에서 사용한 버전 그대로의 코드로 학습을 진행해 주시면 좋겠습니다. 그래야 바뀐 API에 집중하는게 아니라 강의에서 전달하려는 내용에 집중하실 수 있을테니까요.

스프링 시큐리티 6.1의 문서를 보시면 힌트를 찾을 수 있으실 것 같습니다.
https://docs.spring.io/spring-security/reference/servlet/authorization/authorize-http-requests.html
"Matching By Http Method"로 찾아보세요.


그리고 역시 아래 문서를 보시면 이전 방식의 설정을 사용하는 방법에 대한 설명도 있네요.
https://docs.spring.io/spring-security/reference/5.8/migration/servlet/config.html
"If you are having problem with the new requestMatchers methods, you can always switch back to the RequestMatcher implementation that you were using." 이 부분을 찾아서 더 읽어보세요.

감사합니다.

jjeovi님의 프로필

jjeovi

질문자

2023.08.18

감사합니다. 답변이 빨라서 좋습니다.

 

첨부해주신 링크를 참고하여 해결을 했습니다.

 

제가 작성한 소스를 공유드립니다. (문제가 있다면 지적해주세요~)

package com.studyolle.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;


@Configuration
@EnableWebSecurity 
public class SecurityConfig {

    @Bean
    SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
        MvcRequestMatcher.Builder mvcMatcherBuilder = new MvcRequestMatcher.Builder(introspector);
        http.authorizeHttpRequests((requests) -> requests
                .requestMatchers(
                         mvcMatcherBuilder.pattern("/"),                    mvcMatcherBuilder.pattern("/login"),                mvcMatcherBuilder.pattern("/sign-up")
                        ,mvcMatcherBuilder.pattern("/check-email"),         mvcMatcherBuilder.pattern("/check-email-token"),    mvcMatcherBuilder.pattern("/email-login")
                        ,mvcMatcherBuilder.pattern("/check-email-login"),   mvcMatcherBuilder.pattern("/login-link"),           mvcMatcherBuilder.pattern("/profile/*")
                ).permitAll()
                .requestMatchers(mvcMatcherBuilder.pattern(HttpMethod.POST, "/profile/*")).permitAll()
                .anyRequest().authenticated()
        );

        return http.build();
    }

}