我一直在使用Spring Boot开发Spring Social Google访问权限。我正在配置
application.properties
我无法在春季社交中找到谷歌的任何元数据 就像有facebook,twitter和linkedin的元数据一样 然后我将如何配置Client-ID和Client-Secret,这是connectcontroller连接谷歌所必需的。
答案 0 :(得分:2)
内置spring boot支持的最新版本在spring.social.google下的application.properties中查找appSecret和appId键。
spring.social.google.appId=
spring.social.google.appSecret=
请参阅props file
旧代码应该在类似于此的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");
}
}
答案 1 :(得分:2)
Spring社交没有为Google完成自动配置。所以添加任何新属性都不会起作用。您需要找到一个类(在eclipse中按Ctrl + Shift + T)并查找FacebookAutoConfiguration类,您应该能够在spring-autoconfigure.jar中的org.springframework.boot.autoconfigure.social包中找到它。 复制此文件并将Google替换为Google。
或者,复制下面的类并放入一些包,确保它在classpath(src / main / java或其他源文件夹中)中可用
Follow this Link for more details
index.html
现在添加GoogleProperties
在同一个包中添加以下类
@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());
}
}
}
有关详细说明和分步指南follow this link