简体   繁体   English

Spring Security:创建bean时出错/未定义bean

[英]Spring Security: Error creating bean/No bean is defined

I try to write a simple Spring application with Spring Security but I'm facing with errors. 我试图用Spring Security编写一个简单的Spring应用程序,但是我遇到了错误。 Here's exception I'm receiving: 我收到的例外情况是:

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'org.springframework.security.filterChains': 
  Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0'
  while setting bean property 'sourceList' with key [0]; 
nested exception is org.springframework.beans.factory.BeanCreationException: 

Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#0': 
  Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' 
  while setting constructor argument with key [5]; 
nested exception is org.springframework.beans.factory.BeanCreationException: 

Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': 
  Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0' 
  while setting bean property 'authenticationManager';
nested exception is org.springframework.beans.factory.BeanCreationException: 

Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': 
  Cannot resolve reference to bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' 
  while setting constructor argument; 
nested exception is org.springframework.beans.factory.BeanCreationException: 

Error creating bean with name 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0': 
FactoryBean threw exception on object creation; 
nested exception is org.springframework.beans.factory.BeanCreationException: 

Error creating bean with name 'org.springframework.security.authenticationManager': 
  Cannot resolve reference to bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' 
  while setting constructor argument with key [0]; 
nested exception is org.springframework.beans.factory.BeanCreationException: 

Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': 
  Cannot resolve reference to bean 'UserDao' 
  while setting bean property 'userDetailsService'; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No bean named 'UserDao' is defined

I can see, that there's problem with bean UserDao which is unvisible, although I have: <context:component-scan base-package="service, controller, dao" /> . 我可以看到,Bean UserDao一个不可见的问题,尽管我有: <context:component-scan base-package="service, controller, dao" />

The rest of my configuration: 我其余的配置:

pom.xml 的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany</groupId>
    <artifactId>MyLibrary</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>MyLibrary</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>4.3.1.Final</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.0.1.RELEASE</version>
        </dependency>

        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.0.1.RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

web.xml web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Spring Security -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-security.xml</param-value>
    </context-param>

    <filter>  
        <filter-name>springSecurityFilterChain</filter-name>  
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
    </filter>  

    <filter-mapping>  
        <filter-name>springSecurityFilterChain</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>
</web-app>

dispatcher-servlet.xml 调度员servlet.xml中

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />


    <context:component-scan base-package="service, controller, dao" />


    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="LibraryPU" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>
    <bean id="jpaVendorAdapter"
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="HSQL" />
        <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
    </bean>

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
        <property name="url" value="jdbc:derby://localhost:1527/Library" />
        <property name="username" value="app" />
        <property name="password" value="app" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

and spring-security.xml 和spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-4.0.xsd">

    <http auto-config="true">

        <intercept-url pattern="/index" access="hasRole('ROLE_USER')" />

        <access-denied-handler error-page="/login" />

        <form-login 
            login-page="/login" 
            default-target-url="/login" 
            authentication-failure-url="/login?error" 
            username-parameter="username"
            password-parameter="password" />
        <logout logout-success-url="/login?logout"  />
        <csrf/>
    </http>

    <authentication-manager>
            <authentication-provider user-service-ref="UserDao" />
    </authentication-manager>

</beans:beans>

I'm doing something wrong so please help me. 我做错了,请帮助我。 Thanks! 谢谢!

I think what your problem: 我认为您的问题是什么:

 <authentication-manager>
      <authentication-provider user-service-ref="UserDao" />
 </authentication-manager>

I try to find UserDao in any Spring project, however I found just in sample (sample.dao.UserDAO). 我尝试在任何Spring项目中找到UserDao,但是我只是在sample (sample.dao.UserDAO)中找到了。 Do you really know that UserDao class is part of any Spring framework? 您真的知道UserDao类是任何Spring框架的一部分吗? As I know, you need to create authentication-provider class by yourself. 据我所知,您需要自己创建身份验证提供程序类。

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

相关问题 Spring 安全性:创建名为 springsecurityfilterchain 的 bean 时出错 - Spring Security: error creating bean with name springsecurityfilterchain Spring 安全性 - 过滤器链错误 - 在 class 路径资源中定义名称为“filterChain”的 bean 创建错误 - Spring Security - Filter Chain Error - Error creating bean with name 'filterChain' defined in class path resource 将Spring Security和JSF 2结合在一起:在ServletContext资源中创建名称为&#39;dataSource&#39;的bean时出错 - Combining Spring security and JSF 2 : Error creating bean with name 'dataSource' defined in ServletContext resource 在实现 spring 安全性时,在类路径资源中创建名为“springSecurityFilterChain”的 bean 时出错 - Error creating bean with name 'springSecurityFilterChain' defined in class path resource while implementing spring security Spring 安全问题创建 bean - Spring Security Issue creating bean Spring错误创建bean - Spring Error creating bean 在spring中创建bean时出错 - Error creating bean in spring 在Spring中创建bean时出错 - Error creating bean in Spring Spring - 创建 bean 时出错 - Spring - Error creating bean 具有Spring Security的Spring Boot:创建名称为&#39;securityConfiguration&#39;的bean时出错 - Spring boot with spring security: Error creating bean with name 'securityConfiguration'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM