How to use Spring Security for multiple login scenarios with Java configuration?
<p>I have a Spring Boot application. It has a welcome page which users select their login type and they're redirected to a login page and get their role based on their selection. Each login page provides authentication mechanisms with different external web services. I configured my security for a scenario, but how to do this for multiple scenarios? Should I do it with multiple security configs, or all configurations in the same security config? If so how?</p>

<p><strong>SecurityConfig.java</strong></p>

<pre><code>@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private CustomAuthenticationProvider cap;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/welcomeX").hasAuthority("X_USER")
                .and()
                .formLogin()
                .loginPage("/login")
                .loginPage("/main")
                .loginProcessingUrl("/welcome")
                .permitAll()
                .failureUrl("/login?error=true");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(cap);
    }
</code></pre>

<p><strong>CustomAuthenticationProvider.java</strong></p>

<pre><code>@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private ExternalService externalService;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
       String username = authentication.getName();
       String password = authentication.getCredentials().toString();

       Response resp = externalService.authenticate(username, password);

       if (resp.isSuccess()) {
       List&lt;GrantedAuthority&gt; grantedAuths = new ArrayList&lt;&gt;();
    grantedAuths.add(new SimpleGrantedAuthority("X_USER"));
    Authentication auth = new UsernamePasswordAuthenticationToken(username, password, grantedAuths);
    return auth;
} else {
    return null;
}
    }
}
</code></pre>