3

The Spring Social SocialAuthenticationFilter processes the url /auth. How can I change this url?
There is a method SocialAuthenticationFilter.setFilterProcessesUrl(String filterProcessesUrl). How can I access the SocialAuthenticationFilter object to use this method? (I use SpringSocialConfigurer, which automatically adds SocialAuthenticationFilter to the chain.)

olivmir
  • 692
  • 10
  • 29

2 Answers2

2

Unfortunately using the original SpringSocialConfigurer you can't set the processed url. Looking at the code, you may "only" fluently configure things like postLoginUrl, postFailureUrl, signupUrl, connectionAddedRedirectUrl, defaultFailureUrl and alwaysUsePostLoginUrl.

Imho that's an omission in SpringSocialConfigurer and you may want to file an issue at https://github.com/spring-projects/spring-social/issues.

For the time being, just use the code from SpringSocialConfigurer and come up with your own (small) class.

Hille
  • 4,096
  • 2
  • 22
  • 32
  • Thank you - yes, by creating a custom `Configurer` it can be set. I've posted my resulting solution in an additional answer. – olivmir Feb 28 '17 at 07:26
  • 1
    I've also filed an issue on github - [**see here**](https://github.com/spring-projects/spring-social/issues/226). – olivmir Feb 28 '17 at 07:41
2

As proposed by user "Hille" I've now created my own SpringSocialConfigurer by doing the following:

a) Via github (see here) I've copied the source code and pasted it into my custom class.

b) Then I could set the filterProcessesUrl - here is an example:

public class MySpringSocialConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

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

        ApplicationContext applicationContext = http.getSharedObject(ApplicationContext.class);
        UserIdSource userIdSource = getDependency(applicationContext, UserIdSource.class);
        UsersConnectionRepository usersConnectionRepository = getDependency(applicationContext, UsersConnectionRepository.class);
        SocialAuthenticationServiceLocator authServiceLocator = getDependency(applicationContext, SocialAuthenticationServiceLocator.class);
        SocialUserDetailsService socialUsersDetailsService = getDependency(applicationContext, SocialUserDetailsService.class);

        SocialAuthenticationFilter filter = new SocialAuthenticationFilter(
                http.getSharedObject(AuthenticationManager.class), 
                userIdSource, 
                usersConnectionRepository, 
                authServiceLocator);

        ...

        filter.setFilterProcessesUrl("/mysociallogin");

        ...

        http.authenticationProvider(
            new SocialAuthenticationProvider(usersConnectionRepository, socialUsersDetailsService))
            .addFilterBefore(postProcess(filter), AbstractPreAuthenticatedProcessingFilter.class);
    }

    private <T> T getDependency(ApplicationContext applicationContext, Class<T> dependencyType) {
        try {
            T dependency = applicationContext.getBean(dependencyType);
            return dependency;
        } catch (NoSuchBeanDefinitionException e) {
            throw new IllegalStateException("SpringSocialConfigurer depends on " + dependencyType.getName() +". No single bean of that type found in application context.", e);
        }
    }
}
olivmir
  • 692
  • 10
  • 29
  • You may want to write your code a bit more "springish" and stay with the existing fluent approach, i.e. add `if (filterProcessUrl != null ) { filter.setFilterProcessesUrl(filterProcessUrl); }` plus an additional setter like `public MySpringSocialConfigurer filterProcessUrl(String filterProcessUrl)` with code analogous to e.g. `userIdSource(UserIdSource userIdSource)`. – Hille Mar 01 '17 at 17:33