I am trying to implement Spring Security 5.0.0.RELEASE in an existing Spring MVC project. Note that it is entirely Annotation Based.
Following 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("/"); } }
Following 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 per the Spring Security Docs I am trying to configure like following -
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 am trying to start the server I am getting 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 quite sure I am not missing any steps mentioned in the Srping setup. I have googled several times but of no use. I am also tried removing the SecurityWebAppInitializer
and manually adding the filter in the WebAppInitializer
like the following way.
FilterRegistration.Dynamic springSecurityFilterChain = container.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class); springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*");
And I am still getting the exception during startup. Any clues or solutions are highly appreciated.
1 Answers
Answers 1
It might be due to an incompatibility of your spring-web-X.X.X.RELEASE.jar , Spring Data Commons jar. Please check spring jar versions using following command
mvn dependency:tree.
0 comments:
Post a Comment