如何使用多个Facebook帐户登录?

时间:2015-01-13 10:05:43

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

我目前正在努力熟悉Spring Social框架。不幸的是,我所关注的tutorial代码只允许一次全局登录(从Web应用程序的角度来看)。

例如,我正在处理笔记本电脑上的Facebook连接,但在另一个浏览器中打开应用程序后,我仍然看到了我首先使用的用户的详细信息。

是否有一个很好的教程,说明如何一次可以向多个用户进行身份验证?

"问题"似乎在HelloController。一旦用户被授权,它就被全局授权用于整个应用程序。如何更改它以使其适用于多次登录?

package hello;

import javax.inject.Inject;

import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.PagedList;
import org.springframework.social.facebook.api.Post;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class HelloController {

    private Facebook facebook;

    @Inject
    public HelloController(Facebook facebook) {
        this.facebook = facebook;
    }

    @RequestMapping(method=RequestMethod.GET)
    public String helloFacebook(Model model) {
        if (!facebook.isAuthorized()) {
            return "redirect:/connect/facebook";
        }

        model.addAttribute(facebook.userOperations().getUserProfile());
        PagedList<Post> homeFeed = facebook.feedOperations().getHomeFeed();
        model.addAttribute("feed", homeFeed);

        return "hello";
    }

}

修改

将Facebook实例作为请求作用域的bean不会带来任何变化。这是我的代码。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.env.Environment;
import org.springframework.social.UserIdSource;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.web.ConnectController;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;

import my.social.StaticUserIdSource;

@Configuration
@EnableSocial
@PropertySource("classpath:application.properties")
public class SocialConfig extends SocialConfigurerAdapter {

    @Configuration
    public static class FacebookConfiguration extends SocialConfigurerAdapter {

        @Override
        public void addConnectionFactories(
                ConnectionFactoryConfigurer connectionFactoryConfigurer,
                Environment environment) {
            connectionFactoryConfigurer
                    .addConnectionFactory(new FacebookConnectionFactory(
                            environment.getRequiredProperty("facebook.appId"),
                            environment
                                    .getRequiredProperty("facebook.appSecret")));
        }

        @Bean
        public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
            ConnectController connectController = new ConnectController(connectionFactoryLocator, connectionRepository);
            return connectController;
        }

        @Bean
        @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
        public Facebook facebookTemplate(ConnectionRepository connectionRepository) {
            Connection<Facebook> connection = connectionRepository.findPrimaryConnection(Facebook.class);
            return connection != null ? connection.getApi() : null;
        }
    }

    @Override
    public UserIdSource getUserIdSource() {
        return new StaticUserIdSource();
    }
}

1 个答案:

答案 0 :(得分:1)

在您的配置中,您使用StaticUserIdSource判断它使用预定义用户ID的名称。只要您注册了具有给定ID的用户(在首次使用Facebook进行身份验证之后),该用户将用于所有其他用户。