使用InMemoryTokenStore bean创建问题的Spring OAuth2实现

时间:2015-01-30 09:50:39

标签: maven spring-mvc spring-security oauth-2.0 spring-bean

我尝试使用此blog

将Oauth2实现到我的项目中

我是Spring框架的新手,因此发生了一个异常,例如ClassNotFoundException,尽管所有兼容的类存在于正确的包下。 可以在github上看到来源(maven项目) 谢谢

错误始于:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.trafficalarm.rest.security.HierarchicalJsr250Voter] for bean with name 'roleVoter' defined in class path resource [spring/security/security-configuration.xml]; nested exception is java.lang.ClassNotFoundException: com.trafficalarm.rest.security.HierarchicalJsr250Voter
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.trafficalarm.rest.security.HierarchicalJsr250Voter] for bean with name 'roleVoter' defined in class path resource [spring/security/security-configuration.xml]; nested exception is java.lang.ClassNotFoundException: com.trafficalarm.rest.security.HierarchicalJsr250Voter
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.trafficalarm.rest.filter.spring.SpringCrossOriginResourceSharingFilter] for bean with name 'corsFilter' defined in class path resource [spring/oauth/oauth2-configuration.xml]; nested exception is java.lang.ClassNotFoundException: com.trafficalarm.rest.filter.spring.SpringCrossOriginResourceSharingFilter
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.trafficalarm.rest.security.OAuthRestEntryPoint] for bean with name 'oauthRestEntryPoint' defined in class path resource [spring/oauth/oauth2-configuration.xml]; nested exception is java.lang.ClassNotFoundException: com.trafficalarm.rest.security.OAuthRestEntryPoint
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    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)

2 个答案:

答案 0 :(得分:1)

确保web.xml正确初始化spring上下文: [UPDATE]

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:app-config.xml
    </param-value>
</context-param>

必须是直接的孩子。 然后,在您的app-config中,您可以使用

扫描所有组件
<context:component-scan base-package="your.component.package.here"/>

然后导入所有弹簧配置文件

<import resource="classpath:your-resource.xml"/>

我还注意到PropertiesPlaceholderConfigurer bean没有正确配置,所以我不得不在我的一个spring配置文件上执行类似的操作,因为spring没有找到文件属性,所以它到处抛出异常:

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

有些课程你实际上并不需要,但这取决于你。希望你能用这些提示搞清楚。

此致

答案 1 :(得分:0)

关于异常的基本问题是构建路径问题。当我将security-configuration.xml文件内容移动到business-config.xml时,ide警告我有关构建路径问题的信息。所以我检查了项目的构建路径,maven dependecies似乎未经检查。我以前更改了JDK的版本,所以我认为这导致了未经检查的情况。不幸的是,注意了一段时间......