SecurityFilterChain 서블릿 매핑 오류 관련...
1235
작성한 질문수 21
@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();
}그래서 검색 결과 위와 같이 작성하게 되면 정상적으로 실행되게 됩니다.
- 질문
제가 코드를 기존에 SecurityFilterChain 외 잘못 작성한 부분이 존재해서 위와 같이 코드를 작성해야 하는 것인지, 아니면 그 사이에 이렇게 작성되도록 변경된 것인지 앞으로도 지속적으로 검색해보겠지만... 현재까진 답을 찾지 못해 질문드립니다.
2. h2-console의 경우 데이터베이스 관련 서블릿이고, DispatcherServlet은 웹 애플리케이션의 컨트롤러 역할을 하는 서블릿인걸로 알고 있는데 용도가 다른 두 서블릿의 매핑 혼동이 일어나는 이유가 궁금합니다...
*무지한 한 생명체의 질문은 천천히 쾌차하시고 삶의 여유를 되찾으신 다음 답변해주시면 감사하겠습니다!
답변 1
0
안녕하세요. 호돌맨입니다.
질문을 남겨주셔서 감사합니다.
위 코드를 사용해본 결과 저는 오류가 발생하지 않습니다.
그래서 관련 코드를 git으로 제출 해주시면 clone 받아서 실행 해보겠습니다.
spring boot 버젼을 알려주시면 감사하겠습니다.
그리고 마지막으로
AntPathRequestMatcher와 MvcRequestMatcher의 충돌로 예상됩니다.
왜 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();
}감사합니다.
2
빠른 답변 감사드립니다!
우선 답변주신 코드의 경우 제가 작성한 코드는 변경 후 다시 원본을 올리는 상황에서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 버전으로 열심히 수강해보겠습니다!
Deprecated 관련 사항들
0
100
2
깃헙 collaboator 초대 관련
0
87
1
강의 듣다가 도커 이미지 생성시 각각도 가능하나 그렇게 사용하는데가 많은지 모르겠다라는 말을 듣고 남김니다
0
157
2
logout 후에 login 페이지 이동은 어디서 시켜주는건가요?
0
229
1
다중 데이터를 삭제 할 때
0
270
2
querydsl Q class 이슈
0
414
2
Windows WSL Vue 설정
2
246
1
Dip, @transactional
0
187
1
[vite] http proxy error: /auth/login
0
1042
2
로그인 하고 나서 GET요청으로 메인페이지 요청
0
234
2
GitHub Collaborator 초대 관련
0
256
2
Window에서 Vue.js 설정
0
321
2
(솔루션 수정)'tsyringe' Error: TypeInfo not known for "클래스명"
0
713
2
collaboator로 초대받을 수 있을까요??
0
283
2
SecurityMockContext 로부터 유저 정보를 가져오기
0
261
1
given 부분이 길어질 때 어떻게 처리하면 좋을까요?
0
317
1
섹션9 프론트의 코드를 보고싶습니다,,,
0
423
1
Spring Security - defaultSuccessUrl 질문
0
625
1
강의 화면이 나오지 않습니다. 음성과 자막만 나와요
0
302
1
JPAQueryFactory(em)의 객체 생성자 오류에 대해서 질문이 있습니다ㅜㅜ
0
689
2
ExceptionHandler가 AccessDeniedHandler(Http403Handler)를 먹어버리는 현상
0
1179
2
섹션10 언제 나오나요?
0
484
1
CommentService에서 Repository를 호출하지 않는데도
0
342
1
Editor....를 활용한 패턴에 질문있습니다.
0
494
1





