gpt4 book ai didi

java - 为什么 Spring Social Authentication 是 NULL?

转载 作者:行者123 更新时间:2023-11-29 09:32:19 24 4
gpt4 key购买 nike

我有一个简单的 Spring MVC 应用程序,它使用连接到 LinkedIn 并打印用户连接的 Eclipse 运行。与 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");
}
}

请帮忙....

最佳答案

请在此(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>

关于java - 为什么 Spring Social Authentication 是 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21614556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com