Spring Oauth 2 Facebook 身份验证将用户重定向到我的主页

IT小君   2022-09-15T08:14:29

我正在尝试将已通过身份验证的用户重定向到主页以外的另一个页面。我正在使用 spring boot 1.5.6 和 Oauth 2。用户已通过身份验证,但被重定向到主页。我不明白为什么会这样。拜托,有人应该帮助我。stackoverflow 和互联网上相关问题的一些答案对我没有帮助。

这是我的 SecurityConfig 文件

@Configuration
@EnableGlobalAuthentication
@EnableOAuth2Client
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(2)
public class SecurityConfig extends WebSecurityConfigurerAdapter{

protected final Log logger = LogFactory.getLog(getClass());

@Autowired
private OAuth2ClientContext oauth2ClientContext;

@Autowired
private UserDetailsService userDetailsService;

@Autowired
private GeneralConfig generalConfig;

@Override
public void configure(WebSecurity web) throws Exception {
    super.configure(web);
}

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

   http.authorizeRequests()
   .antMatchers("/user*")
   .access("hasRole('CUSTOMER')")
   .and()

   .formLogin()
   .loginPage("/loginUser")
   .loginProcessingUrl("/user_login")
   .failureUrl("/loginUser?error=loginError")
   .defaultSuccessUrl("/customer/dashboard")

   .and()
   .logout()
   .logoutUrl("/user_logout")
   .logoutSuccessUrl("/loginUser").permitAll()
   .deleteCookies("JSESSIONID")

   .and()
   .exceptionHandling()
   .accessDeniedPage("/403")

   .and()
   .csrf().disable()
   .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);  

}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws               Exception {
    auth.userDetailsService(userDetailsService).
    passwordEncoder(bCryptPasswordEncoder());
}

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

@Bean
public FilterRegistrationBeanoauth2ClientFilterRegistration
(OAuth2ClientContextFilter filter) {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(filter);
    registration.setOrder(-100);
    return registration;
}

private Filter ssoFilter(ClientResources client, String path) {
    OAuth2ClientAuthenticationProcessingFilter filter = new      
    OAuth2ClientAuthenticationProcessingFilter(path);
    OAuth2RestTemplate template = new  
    OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
    filter.setRestTemplate(template);
    UserInfoTokenServices tokenServices = new  
         UserInfoTokenServices(client.getResource().getUserInfoUri(),
         client.getClient().getClientId());
    tokenServices.setRestTemplate(template);
    filter.setTokenServices(tokenServices);
    return filter;
}

private Filter ssoFilter() {
    CompositeFilter filter = new CompositeFilter();
    List<Filter> filters = new ArrayList<>();
    filters.add(ssoFilter(facebook(), "/signin/facebook"));
    filters.add(ssoFilter(google(), "/signin/google"));
    filter.setFilters(filters);
    return filter;
}

@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
    return new BCryptPasswordEncoder();
}

@Bean
@ConfigurationProperties("google")
public ClientResources google() {
    return new ClientResources();
}

@Bean
@ConfigurationProperties("facebook")
public ClientResources facebook() {
    return new ClientResources();
}

}

从 SecurityConfig 中,我希望成功验证后的用户被重定向到客户/仪表板,以便我可以进行进一步处理。我知道用户已通过身份验证,因为我可以访问他们的数据。它不仅仅是重定向到正确的页面

但相反,它不断将用户重定向到主页。我究竟做错了什么?我还有另一个用于管理员的安全配置文件。如果需要,我可以提供。

点击广告,支持我们为你提供更好的服务
评论(1)
IT小君

要更改默认策略,您必须设置一个AuthenticationSuccessHandler,请参阅AbstractAuthenticationProcessingFilter#setAuthenticationSuccessHandler

设置用于处理成功身份验证的策略。默认情况下SavedRequestAwareAuthenticationSuccessHandler使用 a。

您修改后的代码:

private Filter ssoFilter(ClientResources client, String path) {
    OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(path);
    OAuth2RestTemplate template = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
    filter.setRestTemplate(template);
    UserInfoTokenServices tokenServices = new UserInfoTokenServices(client.getResource().getUserInfoUri(),client.getClient().getClientId());
    tokenServices.setRestTemplate(template);
    filter.setTokenServices(tokenServices);
    filter.setAuthenticationSuccessHandler(new SimpleUrlAuthenticationSuccessHandler("/customer/dashboard")‌​;
    return filter;
}
2022-09-15T08:14:29   回复