【问题标题】:Why Spring Social Authentication is NULL?为什么 Spring Social Authentication 为 NULL?
【发布时间】:2014-03-04 02:04:03
【问题描述】:

我有一个简单的 Spring MVC 应用程序,我使用 Eclipse 运行它连接到 LinkedIn 并打印用户连接。与 LinkedIn 的连接由 Spring Social 完成。

在我将身份验证代码添加到 ConnectionRepository bean 之前,该应用程序运行良好:

@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode=ScopedProxyMode.INTERFACES)
public ConnectionRepository connectionRepository() {

    Authentication authentication = SecurityContextHolder.getContext()
            .getAuthentication();
    if (authentication == null) {
        throw new IllegalStateException(
                "Unable to get a ConnectionRepository: no user signed in");
    }

    return usersConnectionRepository().createConnectionRepository(
            "testUser");
}

当我运行应用程序时,我收到以下错误:

Caused by: java.lang.IllegalStateException: Unable to get a ConnectionRepository: no user signed in
at com.zomba.web.config.SocialConfig.connectionRepository(SocialConfig.java:69)
at com.zomba.web.config.SocialConfig$$EnhancerByCGLIB$$8f50f43e.CGLIB$connectionRepository$2(<generated>)
at com.zomba.web.config.SocialConfig$$EnhancerByCGLIB$$8f50f43e$$FastClassByCGLIB$$a916f011.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:286)
at com.jobomy.web.config.SocialConfig$$EnhancerByCGLIB$$8f50f43e.connectionRepository(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:160)

这个错误的原因是Authentication为NULL。

为什么在请求完成之前调用 connectionRepository bean?我该如何处理?

这里是我的一些配置文件:

WebMvcConfig

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.zomba.web.controllers","com.zomba.web.services"}, 
    excludeFilters = { @ComponentScan.Filter(Configuration.class) })
@EnableTransactionManagement
public class WebMvcConfig extends WebMvcConfigurerAdapter {


  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
      registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/");
      registry.addResourceHandler("/css/**").addResourceLocations("/css/");
      registry.addResourceHandler("/img/**").addResourceLocations("/img/");
  }

....

}

社交配置

@Configuration
@PropertySource("classpath:linkedin.properties")

public class SocialConfig {
    @Inject
    private Environment environment;

    @Inject
    private DataSource dataSource;

    @Inject
    private TextEncryptor textEncryptor;


    /**
     * Factory for Social connections. You could add more special factories
     * here.
     */
    @Bean
    public ConnectionFactoryLocator connectionFactoryLocator() {
        ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
        registry.addConnectionFactory(new LinkedInConnectionFactory(environment
                .getProperty("linkedin.consumerKey"), environment
                .getProperty("linkedin.consumerSecret")));
        return registry;
    }

    @Bean
    public UsersConnectionRepository usersConnectionRepository() {
        return new JdbcUsersConnectionRepository(dataSource,
                connectionFactoryLocator(), textEncryptor);
    }

    /**
     * Bean that control user connection. You should use real user Id for
     * application instead testUser here.
     */
    @Bean
    @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode=ScopedProxyMode.INTERFACES)
    public ConnectionRepository connectionRepository() {

        Authentication authentication = SecurityContextHolder.getContext()
                .getAuthentication();
        if (authentication == null) {
            throw new IllegalStateException(
                    "Unable to get a ConnectionRepository: no user signed in");
        }

        return usersConnectionRepository().createConnectionRepository(
                "testUser");
    }
}

请帮忙....

【问题讨论】:

    标签: java spring spring-mvc spring-social


    【解决方案1】:

    请在这个(1.0.0.RC4)版本中添加依赖:

    <org.springframework.social.linkedin-version>1.0.0.RC4</org.springframework.social.linkedin-version>
    
    <dependency>
      <groupId>org.springframework.social</groupId>
      <artifactId>spring-social-linkedin</artifactId>
      <version>${org.springframework.social.linkedin-version}</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework.social</groupId>
      <artifactId>spring-social-web</artifactId>
      <version>${org.springframework.social-version}</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 2017-11-21
      • 2019-01-17
      • 2013-07-18
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      • 1970-01-01
      • 2017-04-09
      相关资源
      最近更新 更多