Facebook使用现有用户访问令牌登录Spring Social [英] Facebook Login with Spring Social using Existing User Access Token
问题描述
以下是我目前的情况:
- Spring REST服务,其中许多API要求用户使用经过身份验证
- '注册'API(/ api / v1 / register)
- 带有用户名/密码的登录API(/ api / v1 / login)
- 'Facebook登录'API依赖Spring Social和Spring Security创建用户连接并在(/ auth / facebook)中记录我的用户
- Spring REST service where many of the APIs require the user to be authenticated
- A 'registration' API (/api/v1/register)
- A 'login' API that takes username/password (/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)
我的问题是我希望这些API被多个客户端使用,但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).
以下是移动方案:
- I使用Facebook的iOS SDK请求用户许可
- Facebook返回用户访问令牌
- 我想发送我的后端服务此令牌并让Spring Social接受它,创建用户连接等。
- I use Facebook's iOS SDK to request permission from the user
- Facebook returns a user access token
- I want to send my backend service this token and have Spring Social accept it, create the User Connection, etc.
这可以做完了?或者我是否必须编写自己的API以保持用户连接?
Can this be done? Or am I going to have to write my own API to persist the User Connection?
感谢任何帮助!
推荐答案
我有完全相同的问题,这就是我的工作方式。你可能有一个SocialConfigurer,其中包含以下内容:
I had the exact same issue and here's how I made it work. You probably have a SocialConfigurer somewhere with the following:
@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 ...
}
从这里开始,您可以在Controller / RestController中添加一个映射,其中包含您将发送到服务器的令牌的RequestParam:
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:
@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";
}
有用的参考资料
- UsersConnectionRepository
- ConnectionRepository
这篇关于Facebook使用现有用户访问令牌登录Spring Social的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!