springspring-bootspring-securityspring-security-oauth2spring-social-facebook

Spring Oauth 2 Facebook Authentication Redirects User To My Home Page


I am trying to redirect a user who have been authenticated to another page other than the home page. I am using spring boot 1.5.6 and Oauth 2. User is authenticated but was redirected to the home page. I don't understand why this is happening. Please, someone should help me. Some answers to related problem on stackoverflow and the internet didn't help me.

Here is my SecurityConfig file

@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();
}

}

From the SecurityConfig I expect the user upon successful authentication to be redirected to customer/dashboard so that I can do further processing. I know the user is authenticated because I can access their data. It's not just redirecting to the right page

But instead it keep redirecting the user to the home page. What am I doing wrong? I also have another Security Config File for admin. I can provide it if required.


Solution

  • To change the default strategy, you have to set an AuthenticationSuccessHandler, see AbstractAuthenticationProcessingFilter#setAuthenticationSuccessHandler:

    Sets the strategy used to handle a successful authentication. By default a SavedRequestAwareAuthenticationSuccessHandler is used.

    Your modified code:

    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;
    }