본문 바로가기

intellij +springboot/오류정리

For each MvcRequestMatcher, call MvcRequestMatcher#setServletPath to indicate the servlet path. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filterChain' defined in class path resource [com/raresoft/vc

For each MvcRequestMatcher, call MvcRequestMatcher#setServletPath to indicate the servlet path.
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.resolveMethodArguments(AutowiredAnnotationBeanPostProcessor.java:875) ~[spring-beans-6.0.13.jar:6.0.13]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:828) ~[spring-beans-6.0.13.jar:6.0.13]
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145) ~[spring-beans-6.0.13.jar:6.0.13]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:492) ~[spring-beans-6.0.13.jar:6.0.13]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1416) ~[spring-beans-6.0.13.jar:6.0.13]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-6.0.13.jar:6.0.13]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.13.jar:6.0.13]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325) ~[spring-beans-6.0.13.jar:6.0.13]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.13.jar:6.0.13]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323) ~[spring-beans-6.0.13.jar:6.0.13]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-6.0.13.jar:6.0.13]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.13.jar:6.0.13]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:950) ~[spring-context-6.0.13.jar:6.0.13]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:616) ~[spring-context-6.0.13.jar:6.0.13]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.5.jar:3.1.5]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:738) ~[spring-boot-3.1.5.jar:3.1.5]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:440) ~[spring-boot-3.1.5.jar:3.1.5]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-3.1.5.jar:3.1.5]
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:174) ~[spring-boot-3.1.5.jar:3.1.5]
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:154) ~[spring-boot-3.1.5.jar:3.1.5]
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:96) ~[spring-boot-3.1.5.jar:3.1.5]
        at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:171) ~[spring-web-6.0.13.jar:6.0.13]
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5168) ~[catalina.jar:10.0.10]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[catalina.jar:10.0.10]
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726) ~[catalina.jar:10.0.10]
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:698) ~[catalina.jar:10.0.10]
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747) ~[catalina.jar:10.0.10]
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1027) ~[catalina.jar:10.0.10]
        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:2001) ~[catalina.jar:10.0.10]
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[na:na]
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-util.jar:10.0.10]
        at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:123) ~[na:na]
        at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:828) ~[catalina.jar:10.0.10]
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:478) ~[catalina.jar:10.0.10]
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1708) ~[catalina.jar:10.0.10]
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:320) ~[catalina.jar:10.0.10]
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123) ~[catalina.jar:10.0.10]
        at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423) ~[catalina.jar:10.0.10]
        at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:366) ~[catalina.jar:10.0.10]
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:946) ~[catalina.jar:10.0.10]
        at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:886) ~[catalina.jar:10.0.10]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[catalina.jar:10.0.10]
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396) ~[catalina.jar:10.0.10]
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386) ~[catalina.jar:10.0.10]
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-util.jar:10.0.10]
        at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:145) ~[na:na]
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919) ~[catalina.jar:10.0.10]
        at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:263) ~[catalina.jar:10.0.10]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[catalina.jar:10.0.10]
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:432) ~[catalina.jar:10.0.10]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[catalina.jar:10.0.10]
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:927) ~[catalina.jar:10.0.10]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[catalina.jar:10.0.10]
        at org.apache.catalina.startup.Catalina.start(Catalina.java:795) ~[catalina.jar:10.0.10]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:345) ~[bootstrap.jar:10.0.10]
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:476) ~[bootstrap.jar:10.0.10]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filterChain' defined in class path resource [com/raresoft/vc/configuration/SecurityConfiguration.class]: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'filterChain' threw exception with message: This method cannot decide whether these patterns are Spring MVC patterns or not. If this endpoint is a Spring MVC endpoint, please use requestMatchers(MvcRequestMatcher); otherwise, please use requestMatchers(AntPathRequestMatcher).

 

에러가 난 후에 찾아보니

 

 

 

spring security 3점대 버전에서 h2-console enable true로 옵션을 주니 해당 오류가 발생했다. 문제를 읽어보자면 무슨 servlet을 사용할지 명시해주라고 한다. 2점대 버전에서는 명시하지 않아도 문제가 발생하지 않았는데 3점대에서는 기재해줘야 하나보다.

 


h2-console 설정은 만지지도 안았는데 이런 에러가 났다 로컬에서는 문제없이 실행 되던게 서버쪽에 war 로 배포했더니 생긴 에러

 

 

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().
                requestMatchers(new AntPathRequestMatcher("/h2-console/**"))
                .requestMatchers(new AntPathRequestMatcher( "/favicon.ico"))
                .requestMatchers(new AntPathRequestMatcher( "/css/**"))
                .requestMatchers(new AntPathRequestMatcher( "/js/**"))
                .requestMatchers(new AntPathRequestMatcher( "/img/**"))
                .requestMatchers(new AntPathRequestMatcher( "/lib/**"));
    }


    @Bean
    protected SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
        http.csrf(AbstractHttpConfigurer::disable);
        http.authorizeHttpRequests(authorize ->
                        authorize.requestMatchers(new MvcRequestMatcher(introspector, "/user-service/**")).permitAll()
                .anyRequest().authenticated())
                .httpBasic(Customizer.withDefaults());
        return http.build();
    }

}

 

이런식으로 filterChain 에 requestMatchers 에   MvcRequestMatcher  로 명시를 해줘야 한다고 한다 경우에 따라 MVC 패턴이 아닌 경우에는 AntPathRequestMatcher  로 명시

 

EX )  

 

.requestMatchers("/security/**").permitAll()
-->
.requestMatchers(new MvcRequestMatcher(introspector,"/security/**")).permitAll()

 

 

 

출처 : https://velog.io/@dktlsk6/Spring-Security-3.x.x-h2-console-enable-%EC%97%90%EB%9F%AC