我一直在使用 Spring Boot 开发 Spring Social Google 访问。当我配置
应用程序属性
我在春季社交中找不到任何谷歌的元数据。
就像 facebook、twitter 和linkedin 有元数据一样。
然后我将如何使用连接控制器连接到谷歌所需的 Client-ID 和 Client-Secret 进行配置。
我一直在使用 Spring Boot 开发 Spring Social Google 访问。当我配置
应用程序属性
我在春季社交中找不到任何谷歌的元数据。
就像 facebook、twitter 和linkedin 有元数据一样。
然后我将如何使用连接控制器连接到谷歌所需的 Client-ID 和 Client-Secret 进行配置。
具有内置 Spring Boot 支持的最新版本在 spring.social.google 下的 application.properties 中查找 appSecret 和 appId 键。
spring.social.google.appId=
spring.social.google.appSecret=
查看道具文件
旧代码应该在类似的 SocialConfiguration 类中手动初始化它。请注意,在这种情况下,我们可以在 application.properties 中使用我们想要的任何键。
@Configuration
@EnableSocial
public class SocialConfiguration implements SocialConfigurer {
private static final Logger log = LoggerFactory.getLogger(SocialConfiguration.class);
@Autowired
private DataSource dataSource;
/**
* Configures the connection factories for Facebook.
*/
@Override
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
log.debug("init connection factories");
cfConfig.addConnectionFactory(new FacebookConnectionFactory(
env.getProperty("facebook.app.id"),
env.getProperty("facebook.app.secret")
));
GoogleConnectionFactory googleConnectionFactory = new GoogleConnectionFactory(
env.getProperty("google.consumerKey"),
env.getProperty("google.consumerSecret")
);
googleConnectionFactory.setScope("email profile drive"); //drive.readonly drive.metadata.readonly
cfConfig.addConnectionFactory(googleConnectionFactory);
}
/**
* The UserIdSource determines the account ID of the user.
* The demo application uses the username as the account ID.
* (Maybe change this?)
*/
@Override
public UserIdSource getUserIdSource() {
return new AuthenticationNameUserIdSource();
}
@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
return new JdbcUsersConnectionRepository(
dataSource,
connectionFactoryLocator,
/**
* The TextEncryptor encrypts the authorization details of the connection.
* Here, the authorization details are stored as PLAINTEXT.
*/
Encryptors.noOpText()
);
}
/**
* This bean manages the connection flow between
* the account provider and the example application.
*/
@Bean
public ConnectController connectController(final ConnectionFactoryLocator connectionFactoryLocator,
final ConnectionRepository connectionRepository) {
return new ConnectController(connectionFactoryLocator, connectionRepository);
}
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public Google google(final ConnectionRepository repository) {
final Connection<Google> connection = repository.findPrimaryConnection(Google.class);
if (connection == null)
log.debug("Google connection is null");
else
log.debug("google connected");
return connection != null ? connection.getApi() : null;
}
@Bean(name = { "connect/googleConnect", "connect/googleConnected" })
@ConditionalOnProperty(prefix = "spring.social", name = "auto-connection-views")
public GenericConnectionStatusView googleConnectView() {
return new GenericConnectionStatusView("google", "Google");
}
}
Spring social 没有为 Google 完成自动配置。所以添加任何新属性都行不通。您需要找到一个类(在 Eclipse 中为 Ctrl+Shift+T)并查找 FacebookAutoConfiguration 类,您应该可以在 spring-autoconfigure.jar 中的 org.springframework.boot.autoconfigure.social 包中找到它 复制此文件并替换 Facebook与谷歌。
或者,复制下面的类并放入一些包中,确保它在类路径中可用(src/main/java 或在另一个源文件夹中)
@Configuration
@ConditionalOnClass({ SocialConfigurerAdapter.class, GoogleConnectionFactory.class })
@ConditionalOnProperty(prefix = "spring.social.google", name = "app-id")
@AutoConfigureBefore(SocialWebAutoConfiguration.class)
@AutoConfigureAfter(WebMvcAutoConfiguration.class)
public class GoogleAutoConfiguration {
@Configuration
@EnableSocial
@EnableConfigurationProperties(GoogleProperties.class)
@ConditionalOnWebApplication
protected static class GoogleConfigurerAdapter extends SocialAutoConfigurerAdapter {
private final GoogleProperties properties;
protected GoogleConfigurerAdapter(GoogleProperties properties) {
this.properties = properties;
}
@Bean
@ConditionalOnMissingBean(Google.class)
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public Google google(ConnectionRepository repository) {
Connection connection = repository.findPrimaryConnection(Google.class);
return connection != null ? connection.getApi() : null;
}
@Bean(name = { "connect/googleConnect", "connect/googleConnected" })
@ConditionalOnProperty(prefix = "spring.social", name = "auto-connection-views")
public GenericConnectionStatusView googleConnectView() {
return new GenericConnectionStatusView("google", "Google");
}
@Override
protected ConnectionFactory<?> createConnectionFactory() {
return new GoogleConnectionFactory(this.properties.getAppId(), this.properties.getAppSecret());
}
}
}
现在添加 Google 属性
在同一个包中添加以下类
import org.springframework.boot.autoconfigure.social.SocialProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.social.google")
public class GoogleProperties extends SocialProperties{
}
有关更多说明和分步指南,请点击此链接