Spring安全性初始化在mvcContentNegotiationManager上抛出UnsatisfiedDependencyException

时间:2018-01-29 22:16:20

标签: java spring spring-mvc spring-security

我正在尝试在现有的 Spring MVC项目中实现 Spring Security 5.0.0.RELEASE 。请注意,它完全基于注释。

以下是我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("/");
    }
}

以下是WebMVCConfig文件 -

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/");
    }
}

现在按照Spring Security Docs我正在尝试配置如下 -

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 
    }
}

并且

package com.abc.webapp.core;

public class SecurityWebAppInitializer extends AbstractSecurityWebApplicationInitializer{

}

当我尝试启动服务器时,我得到以下堆栈跟踪 -

[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)

我很确定我没有错过Srping设置中提到的任何步骤。我用Google搜索过几次但没有用过。我也尝试删除SecurityWebAppInitializer并在WebAppInitializer中手动添加过滤器,如下所示。

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

我在启动时仍然遇到异常。任何线索或解决方案都受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

这可能是由于你的spring-web-X.X.X.RELEASE.jar,Spring Data Commons jar不兼容。请使用以下命令检查spring jar版本

mvn dependency:tree.