gpt4 book ai didi

java - Spring Security 初始化在 mvcContentNegotiationManager 上抛出 UnsatisfiedDependencyException

转载 作者:搜寻专家 更新时间:2023-11-01 00:54:23 25 4
gpt4 key购买 nike

我正在尝试在现有的 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{

}

当我尝试启动服务器时,我得到以下 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)

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

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

而且我在启动期间仍然遇到异常。非常感谢任何线索或解决方案。

最佳答案

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

mvn dependency:tree.

关于java - Spring Security 初始化在 mvcContentNegotiationManager 上抛出 UnsatisfiedDependencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48510902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com