0

我正在添加一个 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);
        }
    }


}
4

2 回答 2

1

Found my mistake, I've added Spring Social controller package to the component-scan, I've had a mix up

于 2013-08-02T18:31:09.100 回答
1

也许您需要添加ProviderSignInInterceptor而不是ConnectInterceptor?因为在您提到的问题中,您确实测试了登录功能,而不是连接功能。

于 2013-08-02T15:03:26.813 回答