Spring Boot社交禁用ConnectController

时间:2015-05-02 09:26:42

标签: spring spring-boot spring-social

我通过SpringSocialConfigurer使用Spring Boot Social进行OAuth身份验证,所有与第三方OAuth提供商的OAuth身份验证都通过/auth/providerId路径。

一切正常但我注意到ConnectController也在我的应用程序中显示和配置:

2015-05-02 12:02:47 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.connect(java.lang.String,org.springframework.web.context.request.NativeWebRequest)
2015-05-02 12:02:47 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[GET],params=[error],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.oauth2ErrorCallback(java.lang.String,java.lang.String,java.lang.String,java.lang.String,org.springframework.web.context.request.NativeWebRequest)
2015-05-02 12:02:47 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[DELETE],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.removeConnections(java.lang.String,org.springframework.web.context.request.NativeWebRequest)
2015-05-02 12:02:47 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/connect],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.springframework.social.connect.web.ConnectController.connectionStatus(org.springframework.web.context.request.NativeWebRequest,org.springframework.ui.Model)
2015-05-02 12:02:47 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.springframework.social.connect.web.ConnectController.connectionStatus(java.lang.String,org.springframework.web.context.request.NativeWebRequest,org.springframework.ui.Model)
2015-05-02 12:02:47 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[GET],params=[oauth_token],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.oauth1Callback(java.lang.String,org.springframework.web.context.request.NativeWebRequest)
2015-05-02 12:02:47 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}],methods=[GET],params=[code],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.oauth2Callback(java.lang.String,org.springframework.web.context.request.NativeWebRequest)
2015-05-02 12:02:47 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/connect/{providerId}/{providerUserId}],methods=[DELETE],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.removeConnection(java.lang.String,java.lang.String,org.springframework.web.context.request.NativeWebRequest)

我想我不需要ConnectController。有什么方法可以禁用它吗?

更新:

这是我目前的 SocialConfig

@Configuration
@EnableSocial
public class SocialConfig extends SocialConfigurerAdapter {

    @Autowired
    private UserService userService;

    @Autowired
    private SocialUserService socialUserService;

    @Autowired
    private TextEncryptor textEncryptor;

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        Neo4jUsersConnectionRepository connectionRepository = new Neo4jUsersConnectionRepository(userService, socialUserService,
                (SocialAuthenticationServiceLocator) connectionFactoryLocator, textEncryptor);
        connectionRepository.setConnectionSignUp(new UserConnectionSignUp(userService));
        return connectionRepository;
    }

    @Bean
    public SocialUserDetailsService socialUserDetailsService() {
        return new DBSocialUserDetailService(userService);
    }

}

1 个答案:

答案 0 :(得分:2)

添加

@SpringBootApplication(exclude = SocialWebAutoConfiguration.class)

解决了我。

<强>更新

我需要创建以下类才能正常工作。

import org.springframework.context.annotation.Configuration;
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.social.UserIdSource;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurer;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;

import javax.inject.Inject;
import javax.sql.DataSource;

@Configuration
@EnableSocial
public class MySocialConfig  implements SocialConfigurer {
    @Inject
    private DataSource dataSource;

    //
    // SocialConfigurer implementation methods
    //

    @Override
    public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
    }

    @Override
    public UserIdSource getUserIdSource() {
        return new UserIdSource() {
            @Override
            public String getUserId() {
                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                if (authentication == null) {
                    throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
                }
                return authentication.getName();
            }
        };
    }

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        return new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
    }
}