gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:33:32 26 4
gpt4 key购买 nike

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

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

根据 the docs

POST /signin/{providerId} - Initiates the sign in flow.



这就是为什么有一个按钮可以做到这一点:
<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>

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

似乎默认情况下,如果用户未知,将再次重定向到 https://localhost:8443/signup ( see docs )

If the provider user ID matches more than one existing connection, ProviderSignInController will redirect to the application’s sign in URL to offer the user a chance to sign in through another provider or with their username and password. The request to the sign in URL will have an "error" query parameter set to "multiple_users" to indicate the problem so that the page can communicate it to the user. The default sign in URL is "/signin" (relative to the application root), but can be customized by setting the signInUrl property.



在我的服务器上,这看起来像这样( borrowed from a sample application ):
@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();
}
}

这是我的问题:

首先,我需要知道我应该在这个端点做什么。用户授权了我的应用程序,但我的服务器还不知道该用户,我也不知道
  • 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());
    }

    }

    与 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>

    最佳答案

    After that the user gets redirected to https://localhost:8443/signin/facebook.



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

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

    ng serve –proxy-config proxy.conf.json
    where proxy.conf.json contain your configuration https://localhost:8443/



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

    The user authorized my app, but my server does not know the user yet so do I

    关于spring - 如何通过 Spring Social Facebook 注册新用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50222417/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com