Java Spring MVC安全性:创建名为';springSecurityFilterChain';

Java Spring MVC安全性:创建名为';springSecurityFilterChain';,java,spring,spring-security,Java,Spring,Spring Security,我正在尝试学习spring安全性,并查看了一些教程,但我似乎无法让应用程序运行。我有所有的依赖项和配置类,但它给了我一个错误,我无法找到错误的原因 WebSecurityConfig.java: @EnableWebSecurity public class WebSecurityConfig implements WebMvcConfigurer { @Bean public UserDetailsService userDetailsService() throws Excep

我正在尝试学习spring安全性,并查看了一些教程,但我似乎无法让应用程序运行。我有所有的依赖项和配置类,但它给了我一个错误,我无法找到错误的原因

WebSecurityConfig.java:

@EnableWebSecurity

public class WebSecurityConfig implements WebMvcConfigurer {

 @Bean
    public UserDetailsService userDetailsService() throws Exception {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(User.withDefaultPasswordEncoder().username("user").password("password").roles("USER").build());
        return manager;
    }
}
SecurityWebAppInitializer.java:

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {


}
错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration$1': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setContentNegotationStrategy(org.springframework.web.accept.ContentNegotiationStrategy); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcContentNegotiationManager' defined in class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: 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: com.packt.webstore.config.WebSecurityConfig$$EnhancerBySpringCGLIB$$9d7fee63.configureContentNegotiation(Lorg/springframework/web/servlet/config/annotation/ContentNegotiationConfigurer;)V
    org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:535)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2555)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2544)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:748)

您的配置不应实现用于定制Spring MVC内容的
WebMVCConfiguer
。相反,您应该扩展
websecurityConfigureAdapter
,以自定义与安全相关的设置:

@EnableWebSecurity
公共类WebSecurityConfig扩展了WebSecurityConfigureAdapter{
}

您的配置不应实现用于定制Spring MVC内容的
WebMVCConfiguer
。相反,您应该扩展
websecurityConfigureAdapter
,以自定义与安全相关的设置:

@EnableWebSecurity
公共类WebSecurityConfig扩展了WebSecurityConfigureAdapter{
}

我从错误工厂方法“mvcContentNegotiationManager”引发的异常中发现了这一点;嵌套异常为java.lang.AbstractMethodError:

之所以出现java.lang.AbstractMethodError,是因为抽象方法在被实现时被调用,或者某些依赖项必须丢失

可能您应该将代码更改为

@EnableWebSecurity

public class WebSecurityConfig implements WebSecurityConfigurerAdapter{
}


我从错误工厂方法“mvcContentNegotiationManager”引发的异常中发现了这一点;嵌套异常为java.lang.AbstractMethodError:

之所以出现java.lang.AbstractMethodError,是因为抽象方法在被实现时被调用,或者某些依赖项必须丢失

可能您应该将代码更改为

@EnableWebSecurity

public class WebSecurityConfig implements WebSecurityConfigurerAdapter{
}


请将此添加到pom文件中

<dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0.1</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.1</version>
    </dependency>

com.sun.xml.bind
jaxb内核
2.3.0.1
javax.xml.bind
jaxb api
2.3.1
com.sun.xml.bind
jaxb impl
2.3.1

请将此添加到pom文件中

<dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0.1</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.1</version>
    </dependency>

com.sun.xml.bind
jaxb内核
2.3.0.1
javax.xml.bind
jaxb api
2.3.1
com.sun.xml.bind
jaxb impl
2.3.1

请在定义名为mvcContentNegotiationManager的bean的地方添加该部分代码。请在定义名为mvcContentNegotiationManager的bean的地方添加该部分代码