我正在添加一个Facebook连接拦截器,但它没有被调用。我已经从版本1.0.2升级到1.0.3,认为它可能是一个错误但无济于事。正在处理配置并正在调用addInterceptor方法(通过调试检查)。登录按预期工作。拦截器类型是Facebook。无法弄清楚问题。请帮忙。
我的社交配置:
import javax.inject.Inject;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.env.Environment;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.support.ConnectionFactoryRegistry;
import org.springframework.social.connect.web.ConnectController;
import org.springframework.social.connect.web.ProviderSignInController;
import org.springframework.social.connect.web.SignInAdapter;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
import com.test.security.ConnectionSignUpImpl;
import com.test.security.SignInAdapterImpl;
import com.test.social.facebook.PostToWallAfterConnectInterceptor;
import com.test.social.facebook.RedirectAfterConnectInterceptor;
@Configuration
public class SocialConfig {
@Inject
private Environment environment;
@Inject
private DataSource dataSource;
@Inject
private TextEncryptor textEncryptor;
@Value("${app.url}")
private String applicationUrl;
@Value("${facebook.clientId}")
private String facebookClientId;
@Value("${facebook.clientSecret}")
private String facebookClientSecret;
@Bean
public ConnectionFactoryLocator connectionFactoryLocator() {
ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
registry.addConnectionFactory(new FacebookConnectionFactory(
facebookClientId,
facebookClientSecret));
return registry;
}
@Bean
@Scope(value="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(authentication.getName());
}
@Bean
public UsersConnectionRepository usersConnectionRepository() {
JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(
dataSource, connectionFactoryLocator(), textEncryptor);
repository.setConnectionSignUp(connectionSignUp());
return repository;
}
@Bean
public TextEncryptor textEncryptor() {
return Encryptors.noOpText();
}
@Bean
public ConnectController connectController() {
ConnectController controller = new ConnectController(
connectionFactoryLocator(), connectionRepository());
controller.setApplicationUrl(applicationUrl);
controller.addInterceptor(new RedirectAfterConnectInterceptor());
return controller;
}
@Bean
public ProviderSignInController providerSignInController() {
ProviderSignInController controller = new ProviderSignInController(connectionFactoryLocator(),
usersConnectionRepository(), signInAdapter());
controller.setSignUpUrl("/register");
controller.setSignInUrl("/");
return controller;
}
@Bean
public SignInAdapter signInAdapter() {
return new SignInAdapterImpl();
}
@Bean
public ConnectionSignUp connectionSignUp() {
return new ConnectionSignUpImpl();
}
}
我的拦截器:
package com.test.social.facebook;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.web.ConnectInterceptor;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.request.WebRequest;
public class RedirectAfterConnectInterceptor implements ConnectInterceptor<Facebook> {
public void preConnect(ConnectionFactory<Facebook> connectionFactory, MultiValueMap<String, String> parameters, WebRequest request) {
System.out.println("PRE CONNECT!!!!!!!!!!!!!!!");
request.setAttribute("redirectUrl", "test.com", WebRequest.SCOPE_SESSION);
}
public void postConnect(Connection<Facebook> connection, WebRequest request) {
System.out.println("POST CONNECT!!!!!!!!!!!!!!!");
String redirectUrl = (String)request.getAttribute("redirectUrl", WebRequest.SCOPE_SESSION);
if (redirectUrl != null) {
System.out.println("REDIRECT URL: " + redirectUrl);
request.removeAttribute("redirectUrl", WebRequest.SCOPE_SESSION);
}
}
}
答案 0 :(得分:1)
也许您需要添加ProviderSignInInterceptor
而不是ConnectInterceptor
?因为在您提到的问题中,您确实测试了登录功能,而不是连接功能。
答案 1 :(得分:1)
发现我的错误,我已经将Spring Social控制器包添加到组件扫描中,我已经混淆了