我有一个使用 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;
}
}
项目结构的其余部分包括以下内容:-
请帮忙