如何处理 spring-social-facebook 中的 ExpiredAuthorizationException(授权已过期)
How to handle ExpiredAuthorizationException (The authorization has expired) in spring-social-facebook
我正在使用 spring-social-facebook
,有时,在一段时间后身份验证似乎过期,我得到这个异常:
org.springframework.social.ExpiredAuthorizationException: The authorization has expired.
at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleFacebookError(FacebookErrorHandler.java:83)
at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleError(FacebookErrorHandler.java:59)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:667)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:620)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:595)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:303)
at org.springframework.social.facebook.api.impl.SocialContextTemplate.getSocialContext(SocialContextTemplate.java:120)
这个错误我不知道怎么处理...请问验证过期后可以自动重连吗?
使用的版本:
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-facebook</artifactId>
<version>2.0.4.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-core</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
我在 SO 中发现了类似的问题:
- How to handle ExpiredAuthorizationException happening in spring social facebook?
我认为此解决方案不适用于我的较新版本。
- How to recover from a Spring Social ExpiredAuthorizationException:此解决方案仅适用于 spring-社会-google 特殊情况。
如有任何帮助,我们将不胜感激。
要解决此问题,您必须将 ReconnectFilter
添加到社交配置 Java 文件:
@Bean
public ReconnectFilter apiExceptionHandler(UsersConnectionRepository usersConnectionRepository, UserIdSource userIdSource) {
return new ReconnectFilter(usersConnectionRepository, userIdSource);
}
这 ReconnectFilter
依赖于 UserIdSource Bean 来检索用户信息并刷新连接。您必须覆盖它:
@Override
@Bean
public UserIdSource getUserIdSource() {
return new UserIdSource() {
@Override
public String getUserId() {
User user = getUser();
if (user == null) {
throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
}
return user.getId();
}
};
}
您可以在下面看到完整的社交配置文件:
@Configuration
@PropertySource("classpath:application.properties")
@EnableSocial
public class SocialConfig extends SocialConfigurerAdapter {
@Override
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
cfConfig.addConnectionFactory(new FacebookConnectionFactory(env.getProperty("facebook.appKey"), env.getProperty("facebook.appSecret")));
}
@Bean
public UsersConnectionRepository usersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
return new UserConnectionRepositoryImpl(connectionFactoryLocator);
}
@Override
@Bean
public UserIdSource getUserIdSource() {
return new UserIdSource() {
@Override
public String getUserId() {
User user = getUser();
if (user == null) {
throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
}
return user.getId();
}
};
}
@Bean
public ReconnectFilter apiExceptionHandler(UsersConnectionRepository usersConnectionRepository, UserIdSource userIdSource) {
return new ReconnectFilter(usersConnectionRepository, userIdSource);
}
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public ConnectionRepository connectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
User user = getUser();
if (user == null) {
throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
}
return usersConnectionRepository(connectionFactoryLocator).createConnectionRepository(user.getId());
}
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public Facebook facebook(ConnectionRepository repository) {
Connection<Facebook> connection = null;
if (repository != null) {
connection = repository.findPrimaryConnection(Facebook.class);
}
return connection != null ? connection.getApi() : null;
}
}
我正在使用 spring-social-facebook
,有时,在一段时间后身份验证似乎过期,我得到这个异常:
org.springframework.social.ExpiredAuthorizationException: The authorization has expired.
at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleFacebookError(FacebookErrorHandler.java:83)
at org.springframework.social.facebook.api.impl.FacebookErrorHandler.handleError(FacebookErrorHandler.java:59)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:667)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:620)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:595)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:303)
at org.springframework.social.facebook.api.impl.SocialContextTemplate.getSocialContext(SocialContextTemplate.java:120)
这个错误我不知道怎么处理...请问验证过期后可以自动重连吗?
使用的版本:
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-facebook</artifactId>
<version>2.0.4.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-core</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
我在 SO 中发现了类似的问题:
- How to handle ExpiredAuthorizationException happening in spring social facebook? 我认为此解决方案不适用于我的较新版本。
- How to recover from a Spring Social ExpiredAuthorizationException:此解决方案仅适用于 spring-社会-google 特殊情况。
如有任何帮助,我们将不胜感激。
要解决此问题,您必须将 ReconnectFilter
添加到社交配置 Java 文件:
@Bean
public ReconnectFilter apiExceptionHandler(UsersConnectionRepository usersConnectionRepository, UserIdSource userIdSource) {
return new ReconnectFilter(usersConnectionRepository, userIdSource);
}
这 ReconnectFilter
依赖于 UserIdSource Bean 来检索用户信息并刷新连接。您必须覆盖它:
@Override
@Bean
public UserIdSource getUserIdSource() {
return new UserIdSource() {
@Override
public String getUserId() {
User user = getUser();
if (user == null) {
throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
}
return user.getId();
}
};
}
您可以在下面看到完整的社交配置文件:
@Configuration
@PropertySource("classpath:application.properties")
@EnableSocial
public class SocialConfig extends SocialConfigurerAdapter {
@Override
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
cfConfig.addConnectionFactory(new FacebookConnectionFactory(env.getProperty("facebook.appKey"), env.getProperty("facebook.appSecret")));
}
@Bean
public UsersConnectionRepository usersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
return new UserConnectionRepositoryImpl(connectionFactoryLocator);
}
@Override
@Bean
public UserIdSource getUserIdSource() {
return new UserIdSource() {
@Override
public String getUserId() {
User user = getUser();
if (user == null) {
throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
}
return user.getId();
}
};
}
@Bean
public ReconnectFilter apiExceptionHandler(UsersConnectionRepository usersConnectionRepository, UserIdSource userIdSource) {
return new ReconnectFilter(usersConnectionRepository, userIdSource);
}
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public ConnectionRepository connectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
User user = getUser();
if (user == null) {
throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
}
return usersConnectionRepository(connectionFactoryLocator).createConnectionRepository(user.getId());
}
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public Facebook facebook(ConnectionRepository repository) {
Connection<Facebook> connection = null;
if (repository != null) {
connection = repository.findPrimaryConnection(Facebook.class);
}
return connection != null ? connection.getApi() : null;
}
}