spring-boot - 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();
}
为什么应用程序在使用社交帐户登录后没有获取用户信息?
解决方案
推荐阅读
- python - 如何将 whois.whois().creation_date 的输出导出到 csv 文件
- javascript - React-select:默认值仅在静态设置时更新
- java - 使用 Hibernate 5.4 从 SQL 查询中读取嵌套对象
- ruby-on-rails - 加密 Rails URL 中的 id 并在单独的前端访问它?
- python - 将 python virtualenv 创建到 /usr/share/ 是否安全?
- reactjs - 将 Axios 与 react-chrono 一起使用
- php - 请求中的正文作为响应返回
- angular - 在多个数据库选择中隐藏导航栏
- java - 将 toPandas() 与 PySpark 一起使用时,如何避免 java.io.StreamCorruptedException: invalid stream header: 204356EC?
- database-design - 数据库设计 - 将订单数据库表拆分为买入/卖出