Spring社交Facebook个人资料仅提供ID和名称

时间:2017-02-16 12:05:24

标签: java spring spring-security spring-social spring-social-facebook

使用id获取namespring-social以外的参数时遇到问题。

依赖关系:

    <!-- Spring Social -->
    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-config</artifactId>
        <version>1.1.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-core</artifactId>
        <version>1.1.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-security</artifactId>
        <version>1.1.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-web</artifactId>
        <version>1.1.2.RELEASE</version>
    </dependency>

    <!-- Spring Social Facebook -->
    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-facebook</artifactId>
        <version>2.0.3.RELEASE</version>
    </dependency>

配置:

<security:http ...>
...
    <security:custom-filter before="PRE_AUTH_FILTER" ref="socialAuthenticationFilter" />
...
</security:http>
<security:authentication-manager alias="authenticationManager">
    ...
    <security:authentication-provider ref="socialAuthenticationProvider" />
</security:authentication-manager>
<bean id="socialAuthenticationProvider" class="org.springframework.social.security.SocialAuthenticationProvider">
    <constructor-arg ref="inMemoryUsersConnectionRepository"/>
    <constructor-arg ref="socialUserDetailsService"/>
</bean>

<bean id="inMemoryUsersConnectionRepository"
      class="org.springframework.social.connect.mem.InMemoryUsersConnectionRepository">
    <constructor-arg name="connectionFactoryLocator" ref="connectionFactoryLocator"/>
    <property name="connectionSignUp" ref="connectionSignUp"/>
</bean>

<bean id="connectionFactoryLocator"
      class="org.springframework.social.security.SocialAuthenticationServiceRegistry">
    <property name="authenticationServices">
        <list>
            <ref bean="facebookAuthenticationService"/>
        </list>
    </property>
</bean>

<bean id="socialAuthenticationFilter" class="org.springframework.social.security.SocialAuthenticationFilter">
    <constructor-arg name="authManager" ref="authenticationManager"/>
    <constructor-arg name="userIdSource" ref="userIdSource"/>
    <constructor-arg name="usersConnectionRepository" ref="inMemoryUsersConnectionRepository"/>
    <constructor-arg name="authServiceLocator" ref="connectionFactoryLocator"/>
    <property name="authenticationSuccessHandler" ref="loginGuidAuthenticationSuccessHandler"/>
</bean>

<bean id="userIdSource" class="org.springframework.social.security.AuthenticationNameUserIdSource"/>

<bean id="facebookAuthenticationService"
      class="org.springframework.social.facebook.security.FacebookAuthenticationService">
    <constructor-arg name="apiKey" value="<myKey>"/>
    <constructor-arg name="appSecret" value="<mySecret>"/>
    <property name="defaultScope" value="email"/>
</bean>

<bean id="connectionSignUp" class="my.package.DefaultConnectionSignUp"/>

我有自己实现的一些必需服务,我认为这些服务并不重要。当我在execute的实现中调试ConnectionSignUp方法时,除了nameid之外,所有用户字段均为空。

@Override
public String execute(Connection<?> connection) {
    Connection<Facebook> fbConnection = (Connection<Facebook>) connection;
    fbConnection.getApi().userOperations().getUserProfile().getEmail(); //null
    fbConnection.getApi().userOperations().getUserProfile().getFirstName(); //null
    fbConnection.fetchUserProfile().getEmail(); //null
    fbConnection.fetchObject("me", User.class); //email == null
    ...
}

我的jsp表格:

    <form name='facebookSocialloginForm'
          action="<c:url value='/auth/facebook' />" method='POST'>
        <input type="hidden" name="scope" 
            value="public_profile,email,user_about_me,user_birthday,user_likes"/>
        ...
    </form>

可以访问用户私有postslikes。在我的Facebook帐户上,它表示应用程序可以访问电子邮件和其他信息。

有什么建议吗?如果我错过了重要的内容,请询问,我会提供更多信息。

2 个答案:

答案 0 :(得分:1)

facebook api的变化可能导致了这一点。请尝试以下代码:

UserOperations userOperations = facebook.userOperations();
            String [] fields = { "id", "email",  "first_name", "last_name" };
            org.springframework.social.facebook.api.User profile = facebook.fetchObject
                    ("me", org.springframework.social.facebook.api.User.class, fields);

答案 1 :(得分:0)

在点击facebook api时添加public_profile,发送电子邮件到您的范围变量,这肯定会返回您所需的数据。