title: How to setup OAuth2, LDAP authentication for different endpoints in Spring Security?
body: <p>In my application I have some REST layer that requires valid OAuth2 access_token to be invoked. Also I have WebDav endpoint that must require authentication via LDAP. My current Spring Resource Server configuration is:</p>
  
  <pre><code>@Configuration
  @EnableResourceServer
  public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
  
      private final ResourceServerProperties resource;
      private final OAuth2AuthenticationEntryPoint unauthorizedEntryPoint;
  
      @Value("${security.oauth2.server.public.paths}")
      private String[] publicPaths;
  
      @Autowired
      public ResourceServerConfiguration(ResourceServerProperties resource, UnauthorizedEntryPoint unauthorizedEntryPoint) {
          this.resource = resource;
          this.unauthorizedEntryPoint = unauthorizedEntryPoint;
      }
  
      @Override
      public void configure(HttpSecurity http) throws Exception {
          // Logout functionality is implemented in LogoutController.
          http.logout().disable();
  
          http.authorizeRequests()
              .antMatchers(publicPaths).permitAll()
              .antMatchers("/api/secure/password/**").hasAnyAuthority(USER_ROLES)
              .antMatchers("/webdav/**").authenticated()
  
              .antMatchers("/**").denyAll();
      }
  
      @Override
      public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
          resources.authenticationEntryPoint(unauthorizedEntryPoint).resourceId(this.resource.getResourceId());
      }
  }
  </code></pre>
  
  <p>Global Security Configuration:</p>
  
  <pre><code>@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true)
  public class GlobalSecurityConfiguration extends GlobalMethodSecurityConfiguration {
  
      @Override
      protected MethodSecurityExpressionHandler createExpressionHandler() {
          return new OAuth2MethodSecurityExpressionHandler();
      }
  
      @Bean
      public AuthenticationManager authenticationManager(MyCustomOAuth2AuthenticationProvider myCustomOAuth2AuthenticationProvider, ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider) {
          return new ProviderManager(Arrays.asList(myCustomOAuth2AuthenticationProvider, activeDirectoryLdapAuthenticationProvider));
      }
  }
  </code></pre>
  
  <p>When I open <code>/webdav/something</code> URL via browser I retrieve default OAuth2 401 error message "Full authentication is required to access this resource".</p>
  
  <p>How to configure Spring to try invoke <code>ActiveDirectoryLdapAuthenticationProvider</code> instead of showing the default error message?</p>