没有提供id,另一个bean已经注册为org.springframework.security.userDetailsS​​ervice

时间:2015-08-31 15:57:12

标签: spring hibernate spring-mvc jpa

我正在尝试使用以下技术连接到mySQL数据库:

JPA 2: Tomcat 8.0.17: Java 8: 休眠:

我收到以下错误,我真的不明白:

No id supplied and another bean is already registered as org.springframework.security.userDetailsService

这是代码,我能够登录到网站(硬编码弹簧安全)之前在这里做某事一定是问题,或者至少我怀疑。

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="persistenceUnit">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <!--Hibernate properties-->
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

        </properties>

    </persistence-unit>
</persistence>

我将补充一点,我收到一条警告,我将在下面显示:

1

但是我的POM文件中有以下内容:

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>ejb3-persistence</artifactId>
            <version>1.0.2.GA</version>
        </dependency>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">


    <display-name>SpringMVCpractice</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <!-- ========================================================== -->
    <!-- Servlet -->
    <!-- ========================================================== -->

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/dispatcher-servlet.xml
                /WEB-INF/spring-security.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <welcome-file-list>
        <welcome-file></welcome-file>
    </welcome-file-list>

    <!-- ========================================================== -->
    <!-- Security Filter-->
    <!-- ========================================================== -->

    <filter>
        <display-name>springSecurityFilterChain</display-name>
        <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>

调度-servlet.xml中

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- Enable annotation-based Spring MVC controllers (eg: @Controller annotation) -->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- Classpath scanning of @Component, @Service, etc annotated class -->
    <context:component-scan base-package="com.practice.controller"></context:component-scan>

    <mvc:resources mapping="/resources/**" location="/resources/"/>

    <mvc:default-servlet-handler/>

    <!-- Resolve view name into jsp file located on /WEB-INF -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- MySQL Datasource with Commons DBCP connection pooling -->
    <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>

    <!-- EntityManagerFactory -->
    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- Transaction Manager -->
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <!-- Enable @Transactional annotation -->
    <tx:annotation-driven/>

</beans>

弹簧security.xml文件

<?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:security="http://www.springframework.org/schema/security"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

       <context:component-scan base-package="com.practice.controller"></context:component-scan>

    <security:authentication-manager alias="authenticationManager">
        <!--DEFAULT FOR ADMIN-->
        <security:authentication-provider>
            <security:user-service>
                <security:user name="drew" authorities="admin" password="letmein" />
                <security:user name="mike" authorities="admin" password="eagles" />
            </security:user-service>
        </security:authentication-provider>

        <!--mySQL USER AUTHENTICATION AGAINST DATABASE-->
        <security:authentication-provider>
            <security:jdbc-user-service data-source-ref="dataSource"/>
        </security:authentication-provider>

    </security:authentication-manager>

    <security:http use-expressions="true" auto-config='true'>
        <security:intercept-url pattern="/resources/**" access="permitAll" />
        <security:intercept-url pattern="/login" access="permitAll" />
        <security:intercept-url pattern="/index" access="isAuthenticated()" />
        <security:intercept-url pattern="/form" access="isAuthenticated()" />
        <security:intercept-url pattern="/favicon.ico" access="permitAll" />
        <security:intercept-url pattern="/" access="isAuthenticated()" />
        <security:intercept-url pattern="/**" access="denyAll" />
        <security:form-login
                login-page="/login"
                authentication-failure-url="/login?error=true"/>
    </security:http>

</beans>

编辑:

现在(从Spring Config中删除一个用户详细信息后)我得到一个例外:

org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected  exception parsing XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.NoSuchMethodError: org.springframework.aop.config.AopNamespaceUtils.registerAutoProxyCreatorIfNeces‌​sary(Lorg/springframework/beans/factory/xml/ParserContext;Ljava/lang/Object;)

1 个答案:

答案 0 :(得分:0)

您有两个身份验证提供程序,每个都希望创建一个UserDetailsS​​ervice类型的bean。其中一个是内存(第一个),另一个是基于jdbc(第二个) - 请参阅XML中的注释。

    <!--DEFAULT FOR ADMIN-->
    <security:authentication-provider>
        <!-- FIRST UserDetailsService -->
        <security:user-service>
            <security:user name="drew" authorities="admin" password="letmein" />
            <security:user name="mike" authorities="admin" password="eagles" />
        </security:user-service>
    </security:authentication-provider>

    <!--mySQL USER AUTHENTICATION AGAINST DATABASE-->
    <security:authentication-provider>
        <!-- SECOND UserDetailsService -->
        <security:jdbc-user-service data-source-ref="dataSource"/>
    </security:authentication-provider>

根据我的看法猜测,你不需要第一个(非jdbc)。

编辑:NoSuchMethodException

现在看来你在某个类路径上有一个Spring Security 3.x.当Spring迁移到4.0版时,此方法('AopNamespaceUtils.registerAutoProxyCreatorIfNecessary')已被删除(请参阅已删除的方法/类的完整列表:http://docs.spring.io/spring-framework/docs/3.2.4.RELEASE_to_4.0.0.RELEASE/)。所以它必须与类路径相关。

如果您从IntelliJ运行代码,请确保您还阅读了对此问题的回复:IntelliJ IDEA validates toward wrong XSD