我一直在使用Spring开发。当我在配置
application.properties
我在春季社交网站上找不到谷歌的任何元数据。
就像facebook、twitter和linkedin都有元数据一样。
然后,我将如何配置客户机ID和客户端-秘密,这是连接控制器连接到谷歌所需要的。
发布于 2016-06-09 19:08:29
支持内置春季引导的最新版本在appSecret和appId键中查找spring.social.google下的application.properties键。
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");
}
}发布于 2017-05-04 09:30:16
Spring没有为Google完成自动配置。因此,添加任何新的属性都是行不通的。您需要找到一个类(eclipse中的Ctrl+Shift+T)并查找FacebookAutoConfiguration类,您应该能够在spring中的org.springframework.boot.autoconfigure.social包中找到它-autofigre.jar,复制这个文件并用Google替换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());
}
}
}现在添加GoogleProperties
在同一个包中添加以下类
import org.springframework.boot.autoconfigure.social.SocialProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.social.google")
public class GoogleProperties extends SocialProperties{
}有关更多解释和逐步指南按照这个链接
https://stackoverflow.com/questions/37614354
复制相似问题