如何使用注册/登录适配器为Spring Boot配置Facebook注册/登录?

时间:2019-02-17 14:13:56

标签: spring-boot spring-social

除了我现有的使用jwt的注册/登录功能之外,我还想实现社交注册/登录功能。

我遵循了本教程https://www.baeldung.com/facebook-authentication-with-spring-security-and-social,但是与自动生成有关的社会保障相关类:ConnectionFactoryLocator,UsersConnectionRepository似乎不起作用,即使该示例表明应如此。

如何以及在哪里定义这些bean?我似乎找不到任何好的例子。

我想使用在UserService中调用我现有功能的注册/登录适配器结束。

https://www.baeldung.com/facebook-authentication-with-spring-security-and-social

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

import java.math.BigInteger;

public class Main extends Application {

private ToggleGroup group;
private TextField txtfield;
private TextField output;
private int base = 10;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    BorderPane root = new BorderPane();
    StackPane title = new StackPane();
    Text t = new Text("Gib die Basis und die Zahl ein!");
    t.setTextAlignment(TextAlignment.CENTER);
    title.getChildren().add(t);
    FlowPane center = new FlowPane();
    center.setPrefSize(400, 300);
    HBox input = new HBox();
    VBox base1 = new VBox();
    VBox base2 = new VBox();
    VBox base3 = new VBox();
    input.setPrefSize(400, 100);
    base1.setPrefSize(50, 150);
    base2.setPrefSize(50, 150);
    base3.setPrefSize(50, 150);
    group = new ToggleGroup();
    for (int i = 2; i < 35; i++) {
        RadioButton b = new RadioButton(String.valueOf(i));
        if (i == 10) {
            b.setSelected(true);
        }
        b.setToggleGroup(group);
        b.setOnMouseClicked(mouseEvent -> {
            base = Integer.valueOf(b.getText());
            update();
        });

        if (i % 3 == 2) {
            base1.getChildren().add(b);
            b.setTranslateX(110);
        } else if (i % 3 == 0) {
            base2.getChildren().add(b);
            b.setTranslateX(120);
        } else {
            base3.getChildren().add(b);
            b.setTranslateX(130);
        }
    }
    output = new TextField();
    txtfield = new TextField();
    txtfield.setTranslateY(50);
    txtfield.setTranslateX(100);
    txtfield.setOnKeyTyped(keyEvent -> {
        update();
    });
    input.getChildren().add(txtfield);
    center.getChildren().add(input);
    center.getChildren().add(base1);
    center.getChildren().add(base2);
    center.getChildren().add(base3);
    root.setCenter(center);
    root.setTop(title);
    root.setBottom(output);
    primaryStage.setScene(new Scene(root, 400, 400));
    primaryStage.show();
}

private void update() {

    try {
        output.setText(String.valueOf(new BigInteger(txtfield.getText(), base)));
    } catch (NumberFormatException e) {
        output.setText("Nan");
    }
}
}

预期:使登录/注册适配器正常工作,而这些适配器仅在UserService中调用signin()/ signup()方法。

实际:由于社会保障金币未自动连线而当前无法正常工作。(ConnectionFactoryLocator,UsersConnectionRepository)

自动装配失败

autowiring failing

1 个答案:

答案 0 :(得分:0)

您使用的配置应该可以在Spring Boot 1.x上很好地工作。

但是,如果您使用的是Boot 2.x,则需要在SecurityConfig中定义ConnectionFactoryLocatorUsersConnectionRepository

private ConnectionFactoryLocator connectionFactoryLocator() {
    ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
    registry.addConnectionFactory(new FacebookConnectionFactory(appId, appSecret));
    return registry;
}

private UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
    return new InMemoryUsersConnectionRepository(connectionFactoryLocator);
} 

在这里,appIdappSecret来自application.properties。

您还需要更改ProviderSignInController bean的实现:

@Bean
public ProviderSignInController providerSignInController() {
    ConnectionFactoryLocator connectionFactoryLocator = connectionFactoryLocator();
    UsersConnectionRepository usersConnectionRepository = getUsersConnectionRepository(connectionFactoryLocator);
    ((InMemoryUsersConnectionRepository) usersConnectionRepository).setConnectionSignUp(facebookConnectionSignup);
    return new ProviderSignInController(connectionFactoryLocator, usersConnectionRepository, new FacebookSignInAdapter());
}

有关更多详细信息,请参阅您提到的Baeldung文章,该文章已更新。