【问题标题】:Spring social Google春季社交谷歌
【发布时间】:2016-06-03 12:24:53
【问题描述】:

我一直在使用 Spring Boot 开发 Spring Social Google 访问。当我在配置时

application.properties

我在 spring social 中找不到任何 google 的元数据。
就像 facebook、twitter 和 linkedin 的元数据一样。
那么我将如何使用连接控制器连接到谷歌所需的 Client-ID 和 Client-Secret 进行配置。

【问题讨论】:

    标签: spring-social spring-social-google


    【解决方案1】:

    具有内置 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");
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      Spring social 没有为 Google 完成自动配置。所以添加任何新属性都行不通。您需要找到一个类(在 Eclipse 中为 Ctrl+Shift+T)并查找 FacebookAutoConfiguration 类,您应该能够在 spring-autoconfigure.jar 的 org.springframework.boot.autoconfigure.social 包中找到它 复制此文件并将 Facebook 替换为 Google。

      或者,复制下面的类并放入一些包中,确保它在类路径中可用(src/main/java 或在另一个源文件夹中)

      Follow this Link for more details

      @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{
      
      
      }
      

      更多解释和分步指南follow this link

      【讨论】:

      • 给你个好评!对我来说工作得很好,除了我必须删除@EnableSocial,因为它给了我一个preparedstatement错误。
      猜你喜欢
      • 2015-01-07
      • 2016-05-04
      • 2014-09-21
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多