首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >春季社交Google

春季社交Google
EN

Stack Overflow用户
提问于 2016-06-03 12:24:53
回答 2查看 1.3K关注 0票数 0

我一直在使用Spring开发。当我在配置

application.properties

我在春季社交网站上找不到谷歌的任何元数据。

就像facebook、twitter和linkedin都有元数据一样。

然后,我将如何配置客户机ID和客户端-秘密,这是连接控制器连接到谷歌所需要的。

EN

回答 2

Stack Overflow用户

发布于 2016-06-09 19:08:29

支持内置春季引导的最新版本在appSecret和appId键中查找spring.social.google下的application.properties键。

代码语言:javascript
复制
spring.social.google.appId=
spring.social.google.appSecret=

道具文件

旧代码应该在类似于此的SocialConfiguration类中手动初始化它。注意,在本例中,我们可以在application.properties中使用我们想要的任何键。

代码语言:javascript
复制
@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");
    }

}
票数 2
EN

Stack Overflow用户

发布于 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或在另一个源文件夹中)中可用

请按照此链接获取更多详细信息

代码语言:javascript
复制
@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

在同一个包中添加以下类

代码语言:javascript
复制
import org.springframework.boot.autoconfigure.social.SocialProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "spring.social.google")

public class GoogleProperties extends SocialProperties{


}

有关更多解释和逐步指南按照这个链接

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37614354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档