首页 > 解决方案 > Spring Social 成功登录后重定向问题

问题描述

我使用此文档配置了我的 Spring Boot 应用程序以进行社交登录。应用程序已启动,我已使用我的 gmail 帐户登录。到目前为止没有问题。但是当我使用 gmail 帐户登录时,我的应用程序没有获取用户信息。这是我的社交配置。

自动注册为真

@Configuration
@EnableSocial
@PropertySource("classpath:social-config.properties")
public class BlogSiteSocialConfig implements SocialConfigurer{

@Autowired
private DataSource dataSource;

@Autowired
private Environment env;

@Autowired
private IKullaniciDao kullaniciDao;

private boolean autoSignUp = false;

@Override
public void addConnectionFactories(ConnectionFactoryConfigurer connectionFactoryConfigurer,
        Environment environment) {
     try {
        this.autoSignUp = Boolean.parseBoolean(env.getProperty("social.auto-signup"));
    } catch (Exception e) {
        this.autoSignUp = false;
    }

     GoogleConnectionFactory googleFactory = new GoogleConnectionFactory(
                env.getProperty("google.client.id"), 
                env.getProperty("google.client.secret"));
     googleFactory.setScope(env.getProperty("google.scope"));

     connectionFactoryConfigurer.addConnectionFactory(googleFactory);

}

@Override
public UserIdSource getUserIdSource() {
     return new AuthenticationNameUserIdSource();
}

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

    if (autoSignUp) {
        // After logging in to social networking.
        // Automatically creates corresponding APP_USER if it does not exist.
        ConnectionSignUp connectionSignUp = new ConnectionSignUpImpl(kullaniciDao);
        usersConnectionRepository.setConnectionSignUp(connectionSignUp);
    } else {
        // After logging in to social networking.
        // If the corresponding APP_USER record is not found.
        // Navigate to registration page.
        usersConnectionRepository.setConnectionSignUp(null);
    }
    return usersConnectionRepository;
}

@Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, //
        ConnectionRepository connectionRepository) {
    return new ConnectController(connectionFactoryLocator, connectionRepository);
}

我的安全配置也在这里;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)

公共类 BlogSiteSecurityConfig 扩展 WebSecurityConfigurerAdapter{

@Autowired
private UserDetailsService userDetailsService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService);
}

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.csrf().disable();

    // Pages do not require login
    http.authorizeRequests().antMatchers(
            "/css/**","/js/**","/fonts/**","/images/**", 
            "/anasayfa","/yazidetay","/index","/","/hakkimda","/api/kullanici/kaydet",
            "/500","/404","/exceptions/**","/yazilar/getir","/signup"
            ).permitAll();



    // For ADMIN only.
    http.authorizeRequests().antMatchers("/admin/**").access("hasRole('ADMIN')");

    // When the user has logged in as XX.
    // But access a page that requires role YY,
    // AccessDeniedException will be thrown.
    http.authorizeRequests().and().exceptionHandling().accessDeniedPage("/403");

    // Form Login config
    http.authorizeRequests().and().formLogin()//
            // Submit URL of login page.
            .loginProcessingUrl("/j_spring_security_check") // Submit URL
            .loginPage("/login")//
            .defaultSuccessUrl("/anasayfa")//
            .failureUrl("/login?error=true")//
            .usernameParameter("username")//
            .passwordParameter("password");

    // Logout Config
    http.authorizeRequests().and().logout().logoutUrl("/logout").logoutSuccessUrl("/anasayfa");

    // Spring Social Config.
    http.apply(new SpringSocialConfigurer())
            .signupUrl("/signup");

}

// This bean is load the user specific data when form login is used.
@Override
public UserDetailsService userDetailsService() {
    return userDetailsService;
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

为什么应用程序在使用社交帐户登录后没有获取用户信息?

标签: spring-bootspring-securityspring-security-oauth2spring-socialspring-social-google

解决方案


推荐阅读