Spring MVC - 将spring-security导入pom.xml会导致@Autowired问题

时间:2015-03-04 09:51:49

标签: spring spring-mvc spring-security autowired proxy-classes

首先,这是我的帖子,我想谦虚地向SO社区打招呼 - 我已经找到了很多针对各种问题的答案,并在这里解决了一大堆问题。

但我偶然发现了一个问题,我无法找到解决办法。我想在这里指出这个事实,我已经开发了一个月,所以我在春季初级。

我正在开发一个后端RESTful Web服务,它处理和处理来自专用Android和iOS应用程序的请求。我在服务器端使用Spring MVC模板(版本4.1.4.RELEASE),使用Wildfly 8.2作为容器。

现在,我必须实现哈希算法来编码用户激活密码和唯一设备标识符(当前这些值以原始明文存储在数据库中),并将输入与散列值匹配。

Spring Security提供了一个方便的BCrypt密码编码器/匹配器类,完全满足我的需求(我不需要其他Spring Security功能,如过滤器链接等,但我最终会使用它们,当我熟悉Spring并掌握时安全概念)。

将Spring-Security实现到我的项目的第一步已经是showstopper - 导入spring-security-config和/或spring-security-core pom.xml依赖项并尝试部署我的应用程序,导致在上下文中出现代理错误在我的服务类中初始化:

    10:02:00,845 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./atwork: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./atwork: Failed to start service
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_25]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_25]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_25]
Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pl.atwork.dao.EmployeeDao pl.atwork.service.AuthenticationService.empDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.atwork.dao.EmployeeDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:222)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:87)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.start(UndertowDeploymentService.java:72)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
    ... 3 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pl.atwork.dao.EmployeeDao pl.atwork.service.AuthenticationService.empDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.atwork.dao.EmployeeDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:173)
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:193)
    ... 7 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pl.atwork.dao.EmployeeDao pl.atwork.service.AuthenticationService.empDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.atwork.dao.EmployeeDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.atwork.dao.EmployeeDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 24 more

10:02:00,875 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "atwork.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.undertow.deployment.default-server.default-host./atwork" => "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./atwork: Failed to start service
    Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pl.atwork.dao.EmployeeDao pl.atwork.service.AuthenticationService.empDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.atwork.dao.EmployeeDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pl.atwork.dao.EmployeeDao pl.atwork.service.AuthenticationService.empDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.atwork.dao.EmployeeDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pl.atwork.dao.EmployeeDao pl.atwork.service.AuthenticationService.empDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.atwork.dao.EmployeeDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.atwork.dao.EmployeeDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}"}}
10:02:01,022 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "atwork.war" (runtime-name : "atwork.war")
10:02:01,089 INFO  [org.jboss.as.controller] (Controller Boot Thread) JBAS014774: Service status report
JBAS014777:   Services which failed to start:      service jboss.undertow.deployment.default-server.default-host./atwork: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./atwork: Failed to start service

这是导致问题的pom.xml依赖(版本为3.2.6.RELEASE)

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

当我注释掉spring-security依赖项时,autowiring正确执行,我的app成功部署。

的web.xml

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

    <display-name>ATWork Webservice</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml 
        /WEB-INF/spring/security-root-context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

我有两个上下文XML文件 - root-context和servlet-context

根context.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:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <!-- PROPERTY FILES -->
    <context:property-placeholder location="classpath:database.properties"
        ignore-unresolvable="true" />

    <!-- DATABASE BEANS -->

    <context:component-scan base-package="pl.atwork" />

    <!-- Enable annotation based transaction management (@Transactional) -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- Datasource -->

    <jee:jndi-lookup id="dataSource" jndi-name="${database.jndi-name}"
        expected-type="javax.sql.DataSource" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="pl.atwork.model" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${database.hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.dialect">${database.hibernate.dialect}</prop>
            </props>
        </property>
    </bean>

    <!-- Exception translation bean post processor: translates database specific 
        HibernateException or SQLExceptions into Spring exceptions that can be understood 
        by the application context. -->
    <bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <!-- Hibernate Transaction Manager bean: controls the transactions as well 
        as roll-backs. -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

servlet的context.xml中

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

    <context:component-scan base-package="pl.atwork.service" />
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <!-- Configure bean to convert JSON to POJO and vice versa -->
    <beans:bean id="jsonMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </beans:bean>

    <!-- Configure to plugin JSON as request and response in method handler -->
    <beans:bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:ref bean="jsonMessageConverter" />
            </beans:list>
        </beans:property>
    </beans:bean>
</beans:beans>

我的项目结构似乎没问题,这可以通过有效的初始化证明,当不导入Spring-Security依赖项时:

pl.atwork.dao
---EmployeeDao.java
...
pl.atwork.model
---Employee.java
...
pl.atwork.service
---AuthenticationService.java
...
pl.atwork.service.controller
---RestController.java

RestController类,Autowires服务:

    @Controller
    public class RestController {

        @Autowired
        private AuthenticationService authService;

... other Autowired services fields and controller methods

AuthenticationService类,其中代理错误从第一个@Autowired注释开始:

    @Service
    public class AuthenticationService implements Serializable {

        @Autowired
        private EmployeeDao empDao;

... other Autowired fields and @Transactional methods

EmployeeDao类,在自动装配时无法解决:

@Repository
public class EmployeeDao implements Serializable {

    @Autowired
    private SessionFactory sessionFactory;

    ... typical DAO methods

我尝试在上下文文件中强制执行GGLib代理,但它对我不起作用。我还尝试按类型使用@Qualifier并为我的@Autowired字段命名注释,但它也不起作用。

似乎导入spring-security依赖项完全弄乱了我的@Autowired配置,此时我已经没有想法了。我的最新线索是,我的组件扫描上下文文件可能有点尴尬,但我不知道如何让它看起来更好看。此外,我不太确定我是否应该在我的项目中使用接口,并自动装配它们而不是具体实现。我没有找到在我的项目中使用接口的充分理由,因为它不太宽泛的范围是不值得的。

任何帮助都会非常感激 - 我现在已经坚持了几天。

2 个答案:

答案 0 :(得分:0)

尝试使用限定符:

@Repository("employeeDao")
public class EmployeeDao implements Serializable {

然后

    @Autowired
    @Qualifier("employeeDao")
    private EmployeeDao empDao;

答案 1 :(得分:0)

问题解决了!我经历了每一个pom.xml导入,发现它很奇怪,我没有将spring-aop依赖项添加到我的项目中。我认为AspectJ或其他Spring核心依赖项都很简单,但事实证明Spring Security没有解析没有Spring-AOP依赖的代理bean。

如果您能提供全面的解释,我将很高兴知道为什么会这样。