Java 为什么Spring社交身份验证为空?
我有一个简单的SpringMVC应用程序,我使用Eclipse运行它,它连接到LinkedIn并打印用户连接。与LinkedIn的连接由Spring Social完成 在我将身份验证代码添加到ConnectionRepository bean之前,应用程序运行良好:Java 为什么Spring社交身份验证为空?,java,spring,spring-mvc,spring-social,Java,Spring,Spring Mvc,Spring Social,我有一个简单的SpringMVC应用程序,我使用Eclipse运行它,它连接到LinkedIn并打印用户连接。与LinkedIn的连接由Spring Social完成 在我将身份验证代码添加到ConnectionRepository bean之前,应用程序运行良好: @Bean @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode=ScopedProxyMode.INTERFACES) public ConnectionRep
@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)
社会配置
请帮助……请在此(1.0.0.RC4)版本中添加依赖项:
1.0.0.RC4
org.springframework.social
spring社交linkedin
${org.springframework.social.linkedin版本}
org.springframework.social
spring社交网站
${org.springframework.social version}
请在此(1.0.0.RC4)版本中添加依赖项:
1.0.0.RC4
org.springframework.social
spring社交linkedin
${org.springframework.social.linkedin版本}
org.springframework.social
spring社交网站
${org.springframework.social version}
@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");
}
}
<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>