Spring Security Initialization Throwing UnsatisfiedDependencyException in mvcContentNegotiationManager

I am trying to implement Spring Security 5.0.0.RELEASE in an existing Spring MVC project. Please note that this is completely annotation-based.

Below is the code for my WebAppInitializer :

 package com.abc.webapp.core; public class WebAppInitializer implements WebApplicationInitializer{ @Override public void onStartup(ServletContext container) throws ServletException { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation("com.abc.webapp.config"); container.addListener(new ContextLoaderListener(context)); ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcherServlet", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } } 

Below is the WebMVCConfig file -

 package com.abc.webapp.config; @EnableWebMvc @Configuration @ComponentScan(basePackages = { "com.abc.webapp.controller" }) public class AppContextWebConfig extends WebMvcConfigurerAdapter { @Bean public InternalResourceViewResolver resolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setViewClass(JstlView.class); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/css/**").addResourceLocations("/WEB-INF/css/"); registry.addResourceHandler("/resources/js/**").addResourceLocations("/WEB-INF/js/"); } } 

Now, as Spring Security Documents , I'm trying to configure as shown below:

 package com.abc.webapp.config; @EnableWebSecurity @Configuration public class AppContextSecurityConfig extends WebSecurityConfigurerAdapter{ @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication(). withUser(User.withDefaultPasswordEncoder() .username("user") .password("password") .roles("USER") ); } @Override protected void configure(HttpSecurity http) throws Exception { // Code for Login URL and Logout URL } } 

AND

 package com.abc.webapp.core; public class SecurityWebAppInitializer extends AbstractSecurityWebApplicationInitializer{ } 

When I try to start the server, I get the following stacktrace -

 [ERROR][2018-01-30 01:40:13 ContextLoader:351] - Context initialization failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appContextSecurityConfig': Unsatisfied dependency expressed through method 'setContentNegotationStrategy' parameter 0: Error creating bean with name 'mvcContentNegotiationManager' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.accept.ContentNegotiationManager]: Factory method 'mvcContentNegotiationManager' threw exception; nested exception is java.lang.AbstractMethodError; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcContentNegotiationManager' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.accept.ContentNegotiationManager]: Factory method 'mvcContentNegotiationManager' threw exception; nested exception is java.lang.AbstractMethodError at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:651) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:350) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 

I am pretty sure that I will not miss any of the steps mentioned in setting up Srping. I searched Google several times, but to no avail. I also tried removing SecurityWebAppInitializer and manually adding a filter to WebAppInitializer as follows.

 FilterRegistration.Dynamic springSecurityFilterChain = container.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class); springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*"); 

And I still get an exception at startup time. Any hints or solutions are greatly appreciated.

+5
source share
1 answer

Perhaps this is due to the incompatibility of your banner spring -web-XXXRELEASE.jar, Spring Data Commons. Please check Spring jar versions using the following command

 mvn dependency:tree. 
0
source

Source: https://habr.com/ru/post/1275027/


All Articles