我有一个简单的Spring MVC应用程序,我使用Eclipse连接到LinkedIn并打印用户连接。与LinkedIn的联系由Spring Social完成。
应用程序正常工作,直到我将Authentication代码添加到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)
此错误的原因是身份验证为NULL。
为什么在请求完成之前调用connectionRepository bean?我应该如何处理它?</ p>
这里有一些配置文件:
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/");
}
....
}
SocialConfig
@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");
}
}
请帮助......
答案 0 :(得分:-2)
请在此(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>