• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

SecurityFilterChain 서블릿 매핑 오류 관련...

23.10.11 12:40 작성 조회수 903

0

@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{return http.authorizeHttpRequests().requestMatchers("/auth/login").permitAll()
.anyRequest().authenticated()
.and().csrf(AbstractHttpConfigurer::disable)
.build();
}
  • 기존의 강의에 나온 위 코드를 실행하게 되면,

This is because there is more than one mappable servlet in your servlet context: {org.springframework.web.servlet.DispatcherServlet=[/], org.h2.server.web.JakartaWebServlet=[/h2-console/*]}.
  • 위와 같은 DispatcherServlet과 h2-console의 서블릿이 하나 이상 매핑되어 나는 오류라고 나옵니다.

@Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception{
        http.authorizeHttpRequests((authz) -> {
            try {
                authz
                        .requestMatchers(new MvcRequestMatcher(introspector,"/auth/login")).permitAll()
                        //애는 권한 없이도 허용
                        .anyRequest().authenticated()
                        //나머지는 인증해
                        .and()
                        //csrf쪽으로는 builder가 이어지지 않기 때문에 and로 이어준다.
                        .csrf(AbstractHttpConfigurer::disable);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        return http.build();
    }
  • 그래서 검색 결과 위와 같이 작성하게 되면 정상적으로 실행되게 됩니다.

- 질문

  1. 제가 코드를 기존에 SecurityFilterChain 외 잘못 작성한 부분이 존재해서 위와 같이 코드를 작성해야 하는 것인지, 아니면 그 사이에 이렇게 작성되도록 변경된 것인지 앞으로도 지속적으로 검색해보겠지만... 현재까진 답을 찾지 못해 질문드립니다.


    2. h2-console의 경우 데이터베이스 관련 서블릿이고, DispatcherServlet은 웹 애플리케이션의 컨트롤러 역할을 하는 서블릿인걸로 알고 있는데 용도가 다른 두 서블릿의 매핑 혼동이 일어나는 이유가 궁금합니다...

*무지한 한 생명체의 질문은 천천히 쾌차하시고 삶의 여유를 되찾으신 다음 답변해주시면 감사하겠습니다!

답변 1

답변을 작성해보세요.

0

안녕하세요. 호돌맨입니다.
질문을 남겨주셔서 감사합니다.

 

위 코드를 사용해본 결과 저는 오류가 발생하지 않습니다.

그래서 관련 코드를 git으로 제출 해주시면 clone 받아서 실행 해보겠습니다.

 

spring boot 버젼을 알려주시면 감사하겠습니다.

 

그리고 마지막으로

AntPathRequestMatcherMvcRequestMatcher의 충돌로 예상됩니다.

MvcRequestMatcher를 사용 하시는건지, AntPathRequestMatcher를 사용해도 동일한지 확인 해보시면 좋을것 같습니다.

  

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{
        return http.authorizeHttpRequests().requestMatchers(new AntPathRequestMatcher("/auth/login")).permitAll()
            .anyRequest().authenticated()
            .and().csrf(AbstractHttpConfigurer::disable)
            .build();
    }

감사합니다.

BBBBB님의 프로필

BBBBB

질문자

2023.10.11

빠른 답변 감사드립니다!

  • 우선 답변주신 코드의 경우 제가 작성한 코드는 변경 후 다시 원본을 올리는 상황에서 new AntPathRequestMatcher

    이 부분을 빼먹는다는 것을 깜빡한 것 같습니다.

     

      -> 답변을 제대로 안읽었네요...

  • 우선 MvcRequestMatcher를 사용한 이유는 기존의

@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{return http.authorizeHttpRequests().requestMatchers("/auth/login").permitAll() .anyRequest().authenticated() .and().csrf(AbstractHttpConfigurer::disable) .build(); }

에서 오류가 발생하여 검색 결과 HandlerMappingIntrospector introspector를 파라미터로 넘기기위해 MvcRequestMatcher 클래스를 사용하라는 얘기만 있기에 사용했었습니다(무지성으로 쓰긴했습니다...)


오류가 발생한 시점 저는 스프링 boot 3.0.11 버전을 사용하였고, 구글 페이지를 계속 넘겨보니
https://marco.dev/spring-boot-h2-error
외국인 아저씨의 글이 나오는데 이와 관련된 것인가 싶기도 합니다.

  • 현재 스프링 boot 3.0.5 버전으로 변경한 뒤에는 오류가 발생하지 않는 것으로 확인됩니다.

     

     

     

     

     

     

     

  •  

  • 아직 초보라... 전 읽어도 아직 이해되지 않는 부분이 많는 것 같습니다... 우선 저는 3.0.5 버전으로 열심히 수강해보겠습니다!