如何通过Spring Social Facebook注册新用户?

dis*_*ame 5 spring spring-social spring-social-facebook spring-boot

我有一个网站(Angular App),我可以通过https:// localhost:4200https:// localhost:8443上的服务器(纯REST API)访问.

由于REST端点是安全的,因此用户必须在我的服务器上登录.显然,我希望用户能够使用Facebook注册,并在登录后进一步与我的服务器通信.


根据文件

POST /signin/{providerId} - 启动登录流程.

这就是为什么有一个按钮可以做到这一点:

<form ngNoForm name="fb_signin" id="fb_signin" action="https://localhost:8443/signin/facebook" method="POST">
  <input type="hidden" name="scope" value="email">
  <button type="submit">SIGNING</button>
</form>
Run Code Online (Sandbox Code Playgroud)

从那里,一切都运行正常一段时间.用户被重定向到Facebook的授权页面,其中点击了授权按钮.之后,用户被重定向到https:// localhost:8443/signin/facebook.


似乎默认情况下,如果用户未知,将会有另一个重定向到https:// localhost:8443/signup(参见docs)

如果提供者用户ID与多个现有连接匹配,ProviderSignInController则会重定向到应用程序的登录URL,以便为用户提供通过其他提供商或用户名和密码登录的机会.对登录URL的请求将具有error设置为" multiple_users" 的" "查询参数以指示问题,以便页面可以将其传达给用户.默认登录URL为" /signin"(相对于应用程序根目录),但可以通过设置signInUrl属性进行自定义.

在我的服务器上,这看起来像这样(从示例应用程序中借用):

@RequestMapping(value = "/signup", method = RequestMethod.GET)
public SignUpForm signupForm(WebRequest request) {
    Connection<?> connection = providerSignInUtils.getConnectionFromSession(request);
    if (connection != null) {
        request.setAttribute("message", new Message(MessageType.INFO, "Your " + StringUtils.capitalize(connection.getKey().getProviderId()) + " account is not associated with a Spring Social Showcase account. If you're new, please sign up."), WebRequest.SCOPE_REQUEST);
        return SignUpForm.fromProviderUser(connection.fetchUserProfile());
    } else {
        return new SignUpForm();
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是我的问题:

首先,我需要知道我应该在这个端点做什么.用户授权我的应用程序,但我的服务器不知道用户,所以我也是

  • connection.fetchUserProfile() 并将新用户保存到我的数据库
  • 别的什么?

其次,我不知道我应该如何从这里重定向回我的网站,正如所解释的那样,它位于https:// localhost:4200.但是,当然,我的服务器不知道这一点.

是否有人可以指导我完成这项工作?


这是 SocialConfig

@Configuration
@EnableSocial
public class SocialConfig implements SocialConfigurer {

    private final static Logger LOGGER = LogManager.getLogger(SocialConfig.class);

    @Value("${spring.social.facebook.appId}")
    String facebookAppId;

    @Value("${spring.social.facebook.appSecret}")
    String facebookSecret;

    @Autowired
    private DataSource dataSource;

    @Bean
    @Scope(value="singleton", proxyMode = ScopedProxyMode.INTERFACES)
    public ConnectionFactoryLocator connectionFactoryLocator() {
        ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();

        registry.addConnectionFactory(new FacebookConnectionFactory(
                facebookAppId,
                facebookSecret
        ));

        return registry;
    }

    @Bean
    @Scope(value="singleton", proxyMode=ScopedProxyMode.INTERFACES)
    public UsersConnectionRepository usersConnectionRepository() {
        return new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator(), Encryptors.noOpText());
    }

    @Override
    public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
        LOGGER.debug("Adding connection factories");
        cfConfig.addConnectionFactory(new FacebookConnectionFactory(
                env.getProperty("facebook.clientId"),
                env.getProperty("facebook.clientSecret")));

    }

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

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

    @Bean
    public ProviderSignInController providerSignInController(
            ConnectionFactoryLocator connectionFactoryLocator,
            UsersConnectionRepository usersConnectionRepository) {
        ProviderSignInController controller = new ProviderSignInController(
                connectionFactoryLocator,
                usersConnectionRepository,
                new SimpleSignInAdapter(new HttpSessionRequestCache()));

        return controller;
    }

    @Bean
    public RequestCache requestCache() {
        return new HttpSessionRequestCache();
    }

    @Bean
    public SignInAdapter signInAdapter() {
        return new SimpleSignInAdapter(new HttpSessionRequestCache());
    }

}
Run Code Online (Sandbox Code Playgroud)

与Spring Social相关的Maven依赖项:

<dependency>
    <groupId>org.springframework.social</groupId>
    <artifactId>spring-social-facebook</artifactId>
    <version>3.0.0.M1</version>
</dependency>
<dependency>
    <groupId>org.springframework.social</groupId>
    <artifactId>spring-social-security</artifactId>
    <version>2.0.0.M4</version>
</dependency>
<dependency>
    <groupId>org.springframework.social</groupId>
    <artifactId>spring-social-core</artifactId>
    <version>2.0.0.M2</version>
</dependency>
<dependency>
    <groupId>org.springframework.social</groupId>
    <artifactId>spring-social-config</artifactId>
    <version>2.0.0.M2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

thi*_*_vm 0

\n

之后用户被重定向到https://localhost:8443/signin/facebook

\n
\n\n

我认为还有另一种最好的方法。由于您的角度 webapp\n( https://localhost:4200 ) 由 webLayer 组成,因此通过表单操作直接重定向并不是更好的方法。您可以使用代理路由 单击此处

\n\n

现在,如果您正在使用代理路由,则需要通过命令配置代理端口(8443)。

\n\n
\n

ngserve \xe2\x80\x93proxy-config proxy.conf.json
\n 其中 proxy.conf.json 包含您的配置 https://localhost:8443/

\n
\n\n

那么您需要首先向您的服务器(服务层)验证每个用户/新用户的身份,然后通过用户服务层控制器传递每个请求。这将导致每个请求通过您的服务器传递并解决您的问题。

\n\n
\n

用户授权了我的应用程序,但我的服务器还不知道该用户,我也不知道

\n
\n