spring社交版1.0.3:为什么我的字段或属性'name'在null错误上找不到

时间:2014-03-10 15:53:56

标签: spring spring-mvc spring-social

我使用Spring Social 1.0.3在我的登录页面上添加“使用Facebook登录”和“使用Twitter登录”。这是我的登录视图页面:

登录视图:

 <form action="<c:url value="/connect/twitter" />" method="POST">
  <p><button type="submit"><img src="<c:url value="/resources/images/social/twitter/sign-in-with-twitter-d.png" />"/>
    </button></p>
</form>

  <form action="<c:url value="/connect/facebook" />" method="POST">
  <p><button type="submit"><img src="<c:url value="/resources/images/social/facebook/sign-in-with-facebook.png" />"/>
    </button></p>
</form>

Spring社交xml配置:

<context:property-placeholder location="classpath:application.properties" />

    <bean id="connectionFactoryLocator" 
      class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
    <property name="connectionFactories">
        <list>
            <bean class="org.springframework.social.twitter.connect.TwitterConnectionFactory">
                <constructor-arg value="${twitter.consumerKey}" />
                <constructor-arg value="${twitter.consumerSecret}" />               
            </bean>
            <bean class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
                <constructor-arg value="${facebook.clientId}" />
                <constructor-arg value="${facebook.clientSecret}" />                
            </bean>
        </list>
    </property>
</bean>

<bean id="usersConnectionRepository" 
      class="org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository">
    <constructor-arg ref="dataSource" />
    <constructor-arg ref="connectionFactoryLocator" />
    <constructor-arg ref="textEncryptor" />
</bean>

<bean id="connectionRepository" factory-method="createConnectionRepository" 
      factory-bean="usersConnectionRepository" scope="request">
    <constructor-arg value="#{request.userPrincipal.name}" />
    <aop:scoped-proxy proxy-target-class="false" />
</bean>

 <bean id="textEncryptor" class="org.springframework.security.crypto.encrypt.Encryptors" 
                factory-method="noOpText">

        </bean>

<bean class="org.springframework.social.connect.web.ConnectController">
    <!-- relies on by-type autowiring for the constructor-args -->
    <property name="applicationUrl" value="${application.url}" />
</bean>
</beans>

应用程序属性文件:

#Facebook
facebook.clientId=ideletetherealvalue
facebook.clientSecret=ideletetherealvalue

#Twitter
twitter.consumerKey=ideletetherealvalue
twitter.consumerSecret=ideletetherealvalue

application.url=http://localhost:8080/myapp

网址应用http://localhost:8080/myapp已在Facebook应用中获得授权。

当我点击“使用Twitter登录”或“使用Facebook登录”时,我将重定向到Twitter或Facebook以提交我的登录名和密码。

1.Twitter当我提交我的凭据然后另一页要求我允许申请   我允许应用程序,然后我收到以下错误:

DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] [] http-bio-8080-exec-10 Resolving exception from handler [public java.lang.String org.springframework.social.connect.web.ConnectController.connectionStatus(java.lang.String,org.springframework.web.context.request.NativeWebRequest,org.springframework.ui.Model)]: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'name' cannot be found on null
    DEBUG [org.springframework.web.servlet.DispatcherServlet] [] http-bio-8080-exec-10 Could not complete request
    org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'name' cannot be found on null


The complete error is here http://pastebin.com/hyRicyFu

2.Facebook当我提交我的凭据时,我收到以下错误:

org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'name' cannot be found on null
DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] [] http-bio-8080-exec-2 Resolving exception from handler [public java.lang.String org.springframework.social.connect.web.ConnectController.connectionStatus(java.lang.String,org.springframework.web.context.request.NativeWebRequest,org.springframework.ui.Model)]: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'name' cannot be found on null
DEBUG [org.springframework.web.servlet.DispatcherServlet] [] http-bio-8080-exec-2 Could not complete request
org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'name' cannot be found on null

完整错误在http://pastebin.com/hyRicyFu

问题: 根据我的理解,这个错误来自

<bean id="connectionRepository" factory-method="createConnectionRepository" 
      factory-bean="usersConnectionRepository" scope="request">
    <constructor-arg value="#{request.userPrincipal.name}" />
    <aop:scoped-proxy proxy-target-class="false" />
</bean>

错误消息:无法在null

上找到字段或属性“name”

我需要一些确认: a-我是否在用户提交时在数据库中创建用户?我必须扩展哪个控制器?如果某人有一个例子,它将有所帮助

b-如果我错了,发生了什么,为什么我会收到错误?

1 个答案:

答案 0 :(得分:2)

是的,你是对的 - 问题在于配置。

<bean id="connectionRepository" factory-method="createConnectionRepository" 
      factory-bean="usersConnectionRepository" scope="request">
    <constructor-arg value="#{request.userPrincipal.name}" />
    <aop:scoped-proxy proxy-target-class="false" />
</bean>

上面创建了一个连接存储库,其构造函数的参数为​​request.userPrincipal.name - 这只有在您的用户已经登录时才会起作用(这是尝试为主要用户实例化每个请求的存储库 - 所以需要将该存储库的ID绑定到)。

检查专门处理社交登录的文档:http://docs.spring.io/spring-social/docs/1.0.3.RELEASE/reference/html/signin.html

您会看到社交注册有一种不同的方法(与在社交第三方集成的用户签名相比)