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

Deb*_*tra 5 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("/");
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是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/");
    }
}
Run Code Online (Sandbox Code Playgroud)

现在根据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 
    }
}
Run Code Online (Sandbox Code Playgroud)

package com.abc.webapp.core;

public class SecurityWebAppInitializer extends AbstractSecurityWebApplicationInitializer{

}
Run Code Online (Sandbox Code Playgroud)

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

[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)
Run Code Online (Sandbox Code Playgroud)

我很确定我没有错过Srping设置中提到的任何步骤.我用Google搜索过几次但没有用过.我也尝试删除SecurityWebAppInitializerWebAppInitializer以下面的方式手动添加过滤器.

FilterRegistration.Dynamic  springSecurityFilterChain = container.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class);
springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*");
Run Code Online (Sandbox Code Playgroud)

我在启动时仍然遇到异常.任何线索或解决方案都非常受欢迎.

sur*_*rya 0

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

mvn dependency:tree.
Run Code Online (Sandbox Code Playgroud)