title: Spring multiple authentication methods for different api endpoints
body: <p>I want to check for different authentication methods for different endpoints. Methods i want to use are x509 and jwt. I need to use <strong>only</strong> x509 for certain endpoint and use JWT for all other requests.</p>
  
  <p>Here's my web security configuration:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
  
      @Configuration
      @Order(1)
      public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{
  
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              http
                  .authorizeRequests()
                  .antMatchers("/api/transaction/testf").authenticated().and()
                  .x509()
                  .subjectPrincipalRegex("CN=(.*?)(?:,|$)")
                  .userDetailsService(new X509UserDetailsService())
                  ;
          }
      }
  
      @Configuration
      @Order(2)
      public static class ApiTokenSecurityConfig extends WebSecurityConfigurerAdapter{
  
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              http.authorizeRequests()
                  .antMatchers("/oauth/token", "/api/dealer/login").permitAll()
                  .and()
                  .authorizeRequests()
                  .anyRequest()
                  .authenticated()
                  ;
          }
  
      }
  }
  </code></pre>
  
  <p>This configuration only checks <em>/api/transaction/testf</em> endpoint for x509 certificate and allows all other endpoints to respond. I need other endpoints to return 503 without a jwt token.</p>