我使用 Spring Social 1.0.3 在我的登录页面上添加“使用 Facebook 登录”和“使用 Twitter 登录”。这是我的登录 View 页面:
登录 View :
<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
url应用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 上找不到字段或属性“名称”
我需要一些确认: a- 当用户提交时,我是否要在数据库中创建用户?我必须扩展哪个 Controller ?如果有人有一个例子会有所帮助
b- 如果我错了,发生了什么,为什么我收到错误?
最佳答案
是的,您是对的 - 问题在于该配置。
<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
- 这仅在您的用户已经登录时才有效(这是尝试实例化每个请求主要用户的存储库 - 因此需要一个用于绑定(bind)该存储库的 ID)。
查看专门用于处理社交登录的文档:http://docs.spring.io/spring-social/docs/1.0.3.RELEASE/reference/html/signin.html
您将看到社交注册有一种完全不同的方法(与与社交第三方集成的登录用户相比)
关于 Spring 社交版本1.0.3 : Why i've Field or property 'name' cannot be found on null error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22304990/