title: Combine database and SAML authentication in one application using spring security
body: <p>I am trying to implement authentication and authorization in the spring boot(2.2.4) app using spring security(spring-security-starter).</p>
  
  <p><strong>Use Case:</strong> Based on the username I want to redirect the user for the specific authentication provider</p>
  
  <ul>
  <li>If username ends with <em>'mit.com'</em> Authenticate User using database (I am using hibernate)- For this, I can use spring's UserDetailService </li>
  <li><p>If username ends with <em>'einfochips.com'</em> Authenticate User using SAML 2.0
  protocol- Using identity provider like Okta, SSOCircle, OneLogin etc. </p>
  
  <p>I am not able to get how I can do it. I tried using custom filter but couldn't do it.</p></li>
  </ul>
  
  <p>I have gone through many articles but couldn't achieve this.</p>
  
  <p>I wrote below code for authentication using only SAML. It is working fine. Taking the user to okta idp for login.</p>
  
  <pre><code>package com.example.demo;
  
  import static org.springframework.security.extensions.saml2.config.SAMLConfigurer.saml;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.http.HttpMethod;
  import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.saml.userdetails.SAMLUserDetailsService;
  
  @EnableWebSecurity
  @Configuration
  @EnableGlobalMethodSecurity(securedEnabled = true)
  public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  
      @Autowired
      SAMLUserDetailsService userDetailsService;
  
      @Value("${security.saml2.metadata-url}")
      String metadataUrl;
  
      @Value("${server.ssl.key-alias}")
      String keyAlias;
  
      @Value("${server.ssl.key-store-password}")
      String password;
  
      @Value("${server.port}")
      String port;
  
      @Value("${server.ssl.key-store}")
      String keyStoreFilePath;   
  
      //Uisng SAML2.0
      @Override
      protected void configure(final HttpSecurity http) throws Exception {
          http.csrf().disable()
              .authorizeRequests()
                  .antMatchers("/").permitAll()
                  .anyRequest().authenticated()
                  .and()
              .apply(saml())
                  .serviceProvider()
                      .keyStore()
                          .storeFilePath(this.keyStoreFilePath)
                          .password(this.password)
                          .keyname(this.keyAlias)
                          .keyPassword(this.password)
                          .and()
                      .protocol("https")
                      .hostname(String.format("%s:%s", "localhost", this.port))
                      .basePath("/")
                      .and().userDetailsService(userDetailsService)
                  .identityProvider()
                  .metadataFilePath(this.metadataUrl);
      }
  
  }
  </code></pre>
  
  <p>anybody can guide me so that I can configure in such a way that I can use any IDP like okta, ssocircle, OneLogin etc.</p>