用twitter spring social

时间:2017-10-05 18:13:39

标签: java spring-social-twitter

我使用spring-social,spring-security(java 8)开发了一个REST应用程序。 前端是在Angular2中开发的。它需要实现oauth2与各种社交网络。我可以将它与linkedin,facebook和google整合,但面临Twitter问题。 一旦“使用twitter登录”成功,应用程序不会重定向到下一页。 我做了以下实现: -

public class CustomTwitterServiceProvider extends AbstractOAuth1ServiceProvider<Twitter>{

    public CustomTwitterServiceProvider(String consumerKey, String consumerSecret) {
        super(consumerKey, consumerSecret, new OAuth1Template(consumerKey, consumerSecret,
            "https://api.twitter.com/oauth/request_token",
            "https://api.twitter.com/oauth/authorize",
            "https://api.twitter.com/oauth/authenticate",           
            "https://api.twitter.com/oauth/access_token",OAuth1Version.CORE_10));
    }

    @Override
    public Twitter getApi(String accessToken, String secret) {
        return new TwitterTemplate(getConsumerKey(), getConsumerSecret(), accessToken, secret);
    }

}


public class CustomTwitterConnectionFactory extends OAuth1ConnectionFactory<Twitter> {

    public CustomTwitterConnectionFactory(String consumerKey, String consumerSecret) {
        super("twitter", new CustomTwitterServiceProvider(consumerKey, consumerSecret), new TwitterAdapter());
    }

}


@Configuration
@EnableSocial
public class StatelessSocialConfig extends SocialConfigurerAdapter {

    private static final Logger logger = Logger.getLogger(StatelessSocialConfig.class);
    @Autowired
    private SocialUserService userService;
    @Autowired
    ConnectionSignUp connectionSignUp;
    @Value(value = "${facebook.appKey}")
    String facebookAppKey;
    @Value(value = "${facebook.appSecret}")
    String facebookAppSecret;
    @Value(value = "${twitter.appId}")
    String twitterAppId;
    @Value(value = "${twitter.appSecret}")
    String twitterAppSecret;
    @Value(value = "${linkedin.appId}")
    String linkedinappId;
    @Value(value = "${linkedin.appSecret}")
    String linkedinappSecret;
    @Value(value = "${google.appId}")
    String googleAppId;
    @Value(value = "${google.appSecret}")
    String googleAppSecret;

    @Autowired
    private CustomUserConnection customConnectionBean;

    @Autowired
    DataSource dataSource;

    @Override
    public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
        logger.info("facebook app key "+facebookAppKey);
        logger.info("facebook app secret "+facebookAppSecret);
        cfConfig.addConnectionFactory(new FacebookConnectionFactory(
                facebookAppKey,
                facebookAppSecret));
        logger.info("twitter access token "+twitterAppId);
        logger.info("twitter access secret "+twitterAppSecret);
        cfConfig.addConnectionFactory(new CustomTwitterConnectionFactory(
                twitterAppId,
                twitterAppSecret));
        logger.info("linkedin appId "+linkedinappId);
        logger.info("linkedin appSecret "+linkedinappSecret);
        cfConfig.addConnectionFactory(new LinkedInConnectionFactory(
                linkedinappId,
                linkedinappSecret));
        logger.info("google appId "+googleAppId);
        logger.info("google appSecret "+googleAppSecret);
        cfConfig.addConnectionFactory(new GoogleConnectionFactory(
                googleAppId,
                googleAppSecret));

    }

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

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        SimpleUsersConnectionRepository usersConnectionRepository =
                new SimpleUsersConnectionRepository(userService, connectionFactoryLocator);
        usersConnectionRepository.setConnectionSignUp(connectionSignUp);
        customConnectionBean.setConnection(usersConnectionRepository.getConnection());
        return usersConnectionRepository;
    }   
    @Bean
    @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
    public Facebook facebook(ConnectionRepository repository) {
        Connection<Facebook> connection = repository.findPrimaryConnection(Facebook.class);
        return connection != null ? connection.getApi() : null;
    }
    @Bean
    @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
    public Twitter twitter(ConnectionRepository repository) {
        Connection<Twitter> connection = repository.findPrimaryConnection(Twitter.class);
        return connection != null ? connection.getApi() : null;
    }
    @Bean
    @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
    public LinkedIn linkedIn(ConnectionRepository repository) {
        Connection<LinkedIn> connection = repository.findPrimaryConnection(LinkedIn.class);
        return connection != null ? connection.getApi() : null;
    }
    @Bean
    @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
    public Google google(ConnectionRepository repository) {
        Connection<Google> connection = repository.findPrimaryConnection(Google.class);
        return connection != null ? connection.getApi() : null;
    }

}

项目结构的其余部分包括以下内容: -

enter image description here

请帮忙

0 个答案:

没有答案