在实现oauth2时抛出异常

时间:2015-03-30 15:06:32

标签: spring oauth-2.0

我正在尝试将Oauth2实现到Eclipse STS 3.6.2上的spring hibernate项目中。部署期间遇到异常。项目源代码为here。可能是什么问题? 此致

弹簧-config.xml中:

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:security="http://www.springframework.org/schema/security"
           xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/security
            http://www.springframework.org/schema/security/spring-security.xsd">

        <context:component-scan base-package="com.structure"/>
        <context:component-scan base-package="com.trafficalarm.core.services.impl"/>
        <context:component-scan base-package="com.trafficalarm.rest.filter"/>
        <context:component-scan base-package="com.trafficalarm.rest.configuration"/>
        <context:component-scan base-package="com.trafficalarm.rest.resource"/>
        <context:component-scan base-package="com.trafficalarm.rest.api.user"/>
        <context:component-scan base-package="com.trafficalarm.rest.mvc"/>
        <context:component-scan base-package="com.trafficalarm.core.repositories.jpa"/>

        <tx:annotation-driven/>

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/test" />
            <property name="username" value="root" />
            <property name="password" value=""/>
        </bean>
            <!--bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"-->
            <!--property name="username" value="sa"/-->
            <!--property name="password" value="sa"/-->
            <!--property name="url" value="jdbc:h2:mem:test"/-->
            <!--property name="driverClassName" value="org.h2.Driver"/-->
            <!--/bean-->


        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
            </property>
            <property name="jpaProperties">
                <map>
                    <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/><!-- remove when working with mem db-->
                    <entry key="hibernate.hbm2ddl.auto" value="update"/>
                    <entry key="hibernate.show_sql" value="true"/>
                </map>
            </property>
            <property name="packagesToScan" value="com.trafficalarm.core.model.entities"/>
        </bean>

        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>

        <bean id="propertyPlaceholderConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location"  value="WEB-INF/classes/properties/app-production.properties"/>
        </bean>

        <!--mvc:resources mapping="/app/**" location="/app/build/"/>

        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>

        <security:global-method-security pre-post-annotations="enabled">
            <security:protect-pointcut expression="execution(* com.trafficalarm.rest.mvc.*.*(..))"
                                       access="ROLE_DUMMY"/>
        </security:global-method-security> -->

        <mvc:annotation-driven />
        <mvc:default-servlet-handler/>
        <import resource="security/security-configuration.xml"/>
        <import resource="security/authentication-managers.xml"/>
        <import resource="security/endpoint-configuration.xml"/>
        <import resource="oauth/oauth2-configuration.xml"/>
        <import resource="oauth/token-store.xml"/>
        <import resource="oauth/client-details.xml"/>
    </beans>

oauth2-configuration.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:oauth="http://www.springframework.org/schema/security/oauth2"
       xmlns:sec="http://www.springframework.org/schema/security"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
                      http://www.springframework.org/schema/beans/spring-beans.xsd
                      http://www.springframework.org/schema/security/oauth2
                      http://www.springframework.org/schema/security/spring-security-oauth2.xsd
                      http://www.springframework.org/schema/security
                      http://www.springframework.org/schema/security/spring-security.xsd">


    <oauth:authorization-server client-details-service-ref="client-details-service" token-services-ref="tokenServices">
        <oauth:refresh-token/>
        <oauth:password/>
    </oauth:authorization-server>

    <oauth:resource-server id="resourceServerFilter" token-services-ref="tokenServices"/>

    <bean id="clientCredentialsTokenEndpointFilter"
          class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
        <property name="authenticationManager" ref="clientAuthenticationManager"/>
    </bean>

    <bean id="clientAuthenticationEntryPoint"
          class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
        <property name="typeName" value="Basic"/>
    </bean>

    <bean id="oauthAuthenticationEntryPoint"
          class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
        <property name="realmName" value="test"/>
    </bean>

    <bean id="oauthAccessDeniedHandler"
          class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler"/>


    <bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">
        <property name="tokenStore" ref="tokenStore"/>
        <property name="supportRefreshToken" value="true"/>
        <property name="clientDetailsService" ref="client-details-service"/>
    </bean>

    <bean id="corsFilter" class="com.trafficalarm.rest.filter.spring.SpringCrossOriginResourceSharingFilter"/>

    <bean id="oauthRestEntryPoint" class="com.trafficalarm.rest.security.OAuthRestEntryPoint"/>

    <!-- The token endpoint See org.springframework.security.oauth2.provider.endpoint.TokenEndpoint-->
    <http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="clientAuthenticationManager"
          xmlns="http://www.springframework.org/schema/security">
        <anonymous enabled="false"/>
        <http-basic entry-point-ref="oauthRestEntryPoint" />
        <access-denied-handler ref="oauthAccessDeniedHandler"/>
        <custom-filter ref="corsFilter" after="LAST"/>
    </http>


</beans>

以下是发生的异常:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.oauth2.provider.token.DefaultTokenServices com.trafficalarm.rest.configuration.UserConfiguration.tokenServices; nested exception is java.lang.IllegalArgumentException: Can not set org.springframework.security.oauth2.provider.token.DefaultTokenServices field com.trafficalarm.rest.configuration.UserConfiguration.tokenServices to com.sun.proxy.$Proxy67
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    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 org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4760)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5184)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:581)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1686)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.oauth2.provider.token.DefaultTokenServices com.trafficalarm.rest.configuration.UserConfiguration.tokenServices; nested exception is java.lang.IllegalArgumentException: Can not set org.springframework.security.oauth2.provider.token.DefaultTokenServices field com.trafficalarm.rest.configuration.UserConfiguration.tokenServices to com.sun.proxy.$Proxy67
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 26 more
Caused by: java.lang.IllegalArgumentException: Can not set org.springframework.security.oauth2.provider.token.DefaultTokenServices field com.trafficalarm.rest.configuration.UserConfiguration.tokenServices to com.sun.proxy.$Proxy67
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
    at java.lang.reflect.Field.set(Unknown Source)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:504)
    ... 28 more

1 个答案:

答案 0 :(得分:3)

将@EnableAspectJAutoProxy(proxyTargetClass = true)注释添加到UserConfiguration类之后,异常就消失了。

package com.trafficalarm.rest.configuration;

import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;

import com.trafficalarm.core.repositories.AccountRepo;
import com.trafficalarm.core.repositories.VerificationTokenRepo;
import com.trafficalarm.core.services.AccountService;
import com.trafficalarm.core.services.VerificationTokenService;
import com.trafficalarm.core.services.impl.AccountServiceImpl;
import com.trafficalarm.core.services.impl.VerificationTokenServiceImpl;
import com.trafficalarm.core.services.mail.MailSenderService;
import com.trafficalarm.rest.mvc.AccountController;
import com.trafficalarm.rest.mvc.MeResource;

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class UserConfiguration {

    @Autowired
    private AccountRepo userRepository;

    @Autowired
    private VerificationTokenRepo verificationTokenRepo;

    @Autowired
    private MailSenderService mailSenderService;

    @Autowired
    private Validator validator;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private DefaultTokenServices tokenServices;

    @Autowired
    private ClientDetailsService clientDetailsService;

    @Bean
    public VerificationTokenService verificationTokenService() {
        return new VerificationTokenServiceImpl(userRepository, verificationTokenRepo, mailSenderService, validator, passwordEncoder);
    }

    @Bean
    public AccountService userService() {
        return new AccountServiceImpl(userRepository, validator, passwordEncoder);
    } 

    @Bean
    public AccountController userResource() {
        return new AccountController(userService(), verificationTokenService(), tokenServices, passwordEncoder, clientDetailsService);
    }

    @Bean
    public MeResource meResource() {
        return new MeResource();
    }

}