简体   繁体   English

Spring WebMVC 5 - 创建名为“springSecurityFilterChain”的 bean 时出错

[英]Spring WebMVC 5 - Error creating bean with name 'springSecurityFilterChain'

I am following tutorials to configure spring security login for a Spring Web MVC project with Maven and Tomcat.我正在按照教程为使用 Maven 和 Tomcat 的 Spring Web MVC 项目配置 spring 安全登录。 I've read a number of questions asking about the same issue however none have provided a solution to my problem.我已经阅读了许多关于同一问题的问题,但是没有一个提供解决我的问题的方法。 The issue is with springSecurityFilterChain but I do not understand the what is involved in correcting the error.问题出在 springSecurityFilterChain 上,但我不明白纠正错误所涉及的内容。

The compilation error I get is我得到的编译错误是

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration: 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 java.lang.NoClassDefFoundError: org/springframework/security/web/authentication/ui/DefaultLogoutPageGeneratingFilter


Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/security/web/authentication/ui/DefaultLogoutPageGeneratingFilter

My dependencies are:我的依赖项是:

  <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
<spring.version>5.0.0.RELEASE</spring.version>
<jstl.version>1.2.1</jstl.version>
<tld.version>1.1.2</tld.version>
<servlets.version>3.1.0</servlets.version>
<jsp.version>2.3.1</jsp.version>
<spring-security.version>5.0.6.RELEASE</spring-security.version>
<spring.version>5.0.6.RELEASE</spring.version>
<spring.core.version>5.1.4.RELEASE</spring.core.version>

<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.1.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.1.5.RELEASE</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.1.5.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.1.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>4.3.0.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-orm</artifactId>
  <version>5.1.5.RELEASE</version>
</dependency>


<dependency>
  <groupId>javax.transaction</groupId>
  <artifactId>jta</artifactId>
  <version>1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-dbcp2</artifactId>
  <version>2.6.0</version>
</dependency>

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-web</artifactId>
  <version>${spring-security.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-config</artifactId>
  <version>5.1.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-core</artifactId>
  <version>${spring.core.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-ldap -->
<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-ldap</artifactId>
  <version>5.1.4.RELEASE</version>
</dependency>


<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api</artifactId>
  <version>2.3.0</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>6.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs -->
<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-taglibs</artifactId>
  <version>5.1.4.RELEASE</version>
</dependency>


</dependencies>

My security Config:我的安全配置:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


@Autowired
protected void configure(AuthenticationManagerBuilder auth) throws Exception 
{

    auth.inMemoryAuthentication()
            .withUser("user").password("password").roles("USER")
            .and()
            .withUser("admin").password("password").roles("USER", "ADMIN");

}

// Secure the endpoints with HTTP Basic authentication
@Override
protected void configure(HttpSecurity http) throws Exception {

    http
            .csrf().disable()
            .authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("login");
 }

}





import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {  HibernateConfiguration.class, SecurityConfig.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class[] { WebApplicationContextConfig.class };
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/" };
}


}

In your version properties I see that you are using:在您的版本属性中,我看到您正在使用:

<spring-security.version>5.0.6.RELEASE</spring-security.version>
[…]
<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-web</artifactId>
  <version>${spring-security.version}</version>
</dependency>

Mind that the DefaultLogoutPageGeneratingFilter.java was introduced in the org/springframework/security/web/authentication/ui package starting with the 5.1.x line.请注意,从5.1.x行开始,在org/springframework/security/web/authentication/ui引入了DefaultLogoutPageGeneratingFilter.java

So please take a look into your mvn help:effective-pom and align your Spring Security versions to the 5.1.x line.因此,请查看您的mvn help:effective-pom并将您的Spring Security版本与5.1.x行对齐。

Alternatively you could give Spring Boot a try to make dependency management much easier.或者,您可以尝试使用Spring Boot来简化依赖项管理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 安全性:创建名为 springsecurityfilterchain 的 bean 时出错 - Spring Security: error creating bean with name springsecurityfilterchain Spring Boot:创建名为“springSecurityFilterChain”的 bean 时出错 - Spring Boot: Error creating bean with name 'springSecurityFilterChain' 创建名为“springSecurityFilterChain”的 bean 时出错 - Error creating bean with name 'springSecurityFilterChain' Spring MVC 安全性:创建名为“springSecurityFilterChain”的 bean 时出错 - Spring MVC Security: Error creating bean with name 'springSecurityFilterChain' OAUTH2 spring 创建名为“springSecurityFilterChain”的bean时出错 - OAUTH2 spring Error creating bean with name 'springSecurityFilterChain' 创建在WebSecurityConfiguration中定义的名称为&#39;springSecurityFilterChain&#39;的bean时出错 - Error creating bean with name 'springSecurityFilterChain' defined in WebSecurityConfiguration 在实现 spring 安全性时,在类路径资源中创建名为“springSecurityFilterChain”的 bean 时出错 - Error creating bean with name 'springSecurityFilterChain' defined in class path resource while implementing spring security 创建名为“springSecurityFilterChain”的 bean 在类路径资源 3 中定义时出错 - Error creating bean with name 'springSecurityFilterChain' defined in class path resource 3 org.springframeworkbeans.factory.BeanCreationException:创建名称为“ springSecurityFilterChain”的bean时出错 - org.springframeworkbeans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' Spring Security:没有将名为“ springSecurityFilterChain”的bean定义为错误 - Spring Security : no bean is named “springSecurityFilterChain” defined error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM