简体   繁体   English

Facebook使用现有用户访问令牌登录Spring Social

[英]Facebook Login with Spring Social using Existing User Access Token

Here's what I currently have: 这是我现在拥有的:

  • Spring REST service where many of the APIs require the user to be authenticated Spring REST服务,其中许多API要求用户进行身份验证
  • A 'registration' API (/api/v1/register) '注册'API(/ api / v1 / register)
  • A 'login' API that takes username/password (/api/v1/login) 采用用户名/密码的“登录”API(/ api / v1 / login)
  • 'Facebook Login' API that relies on Spring Social and Spring Security to create a User Connection and log my user in (/auth/facebook) 'Facebook Login'API依赖Spring Social和Spring Security创建用户连接并在(/ auth / facebook)中记录我的用户

My problem is that I want these APIs to be used by multiple clients, but the way Facebook Login is right now, it doesn't work well on mobile (works great on a website). 我的问题是我希望这些API被多个客户端使用,但Facebook登录的方式现在,它在移动设备上运行不佳(在网站上运行良好)。

Here's the mobile scenario: 这是移动方案:

  • I use Facebook's iOS SDK to request permission from the user 我使用Facebook的iOS SDK来请求用户的许可
  • Facebook returns a user access token Facebook返回用户访问令牌
  • I want to send my backend service this token and have Spring Social accept it, create the User Connection, etc. 我想发送我的后端服务此令牌并让Spring Social接受它,创建用户连接等。

Can this be done? 可以这样做吗? Or am I going to have to write my own API to persist the User Connection? 或者我是否必须编写自己的API以持久保存用户连接?

Appreciate any help! 感谢任何帮助!

I had the exact same issue and here's how I made it work. 我有完全相同的问题,这就是我如何使它工作。 You probably have a SocialConfigurer somewhere with the following: 您可能在某处具有SocialConfigurer,其中包含以下内容:

@Configuration
@EnableSocial
public class SocialConfig implements SocialConfigurer {

    @Autowired
    private DataSource dataSource;

    @Bean
    public FacebookConnectionFactory facebookConnectionFactory() {
        FacebookConnectionFactory facebookConnectionFactory = new FacebookConnectionFactory("AppID", "AppSecret");
        facebookConnectionFactory.setScope("email");
        return facebookConnectionFactory;
    }

    @Override
    public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
        cfConfig.addConnectionFactory(facebookConnectionFactory());
    }

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

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        return new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
    }

    // Other @Bean maybe ...
}

From here, what you can do is, in a Controller/RestController, add a mapping with a RequestParam for your token that you will send to your server: 从这里开始,您可以在Controller / RestController中添加一个映射,其中包含您将发送到服务器的令牌的RequestParam:

@Autowired
private FacebookConnectionFactory facebookConnectionFactory;

@Autowired
private UsersConnectionRepository usersConnectionRepository;

@RequestMapping(value = "/my-facebook-url", method = RequestMethod.POST)
public String fb(@RequestParam String token) {
    AccessGrant accessGrant = new AccessGrant(token);
    Connection<Facebook> connection = facebookConnectionFactory.createConnection(accessGrant);

    UserProfile userProfile = connection.fetchUserProfile();
    usersConnectionRepository.createConnectionRepository(userProfile.getEmail()).addConnection(connection);

    // ...

    return "Done";
}

Useful references 有用的参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM