title: Disable Keycloak authentication for a specific url in spring-boot
body: <p>Front end of my spring-boot service gets rendered in a 3rd party dashboard. That dashboard also has a generic search bar which we want to use. Now, once we implemented Keycloak authentication, we started facing problems specifically in this search bar. All the other API's works fine because they are called from my front end only, But search API gets called by the 3rd party dashboard.</p>
  
  <p>Weirdly, 3rd party calls my method using Http OPTION method, but my endpoint is registered as GET.</p>
  
  <p>For an interim fix, we are trying to disable the auth on search API only and does not seems to work at all. My configurer is:</p>
  
  <pre><code>@KeycloakConfiguration
  @Profile("!local") // in local profile InsecureLocalConfigurer must be included instead
  public class KeycloakSecurityConfigurer extends KeycloakWebSecurityConfigurerAdapter {
  
  
      /**
       * Enable Keycloak configuration over Spring Boot config instead of {@code keycloak.json} file.
       *
       * @see &lt;a href="https://www.keycloak.org/docs/latest/securing_apps/index.html#spring-boot-integration"&gt;
       * Spring Boot Integration&lt;/a&gt;
       */
      @Bean
      @Nonnull
      public KeycloakConfigResolver keycloakConfigResolver() {
          return new KeycloakSpringBootConfigResolver();
      }
  
      /**
       * Registers the KeycloakAuthenticationProvider with the authentication manager.
       */
      @Autowired
      public void configureGlobal(@Nonnull final AuthenticationManagerBuilder auth) {
          auth.authenticationProvider(keycloakAuthenticationProvider());
      }
  
      /**
       * Accept only bearer token authentication.
       *
       * @see &lt;a href="https://www.keycloak.org/docs/latest/securing_apps/index.html#spring-security-configuration"&gt;
       *     Spring Security Configuration&lt;/a&gt;
       * @see NullAuthenticatedSessionStrategy
       * @return {@link NullAuthenticatedSessionStrategy} instance.
       */
      @Override
      @Nonnull
      protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
          return new NullAuthenticatedSessionStrategy();
      }
  
      /**
       * All request are checked regarding valid token, except &lt;code&gt;/health&lt;/code&gt; check.
       * If configuration property &lt;code&gt;rca.security.enable&lt;/code&gt; is set to &lt;code&gt;false&lt;/code&gt;false
       * (default is true) then all requests are permitted without authentication.
       *
       * &lt;p&gt;
       * &lt;b&gt;CSRF security is disabled&lt;/b&gt; since our app is not multipart web form app.
       * See more at: &lt;ul&gt;
       *
       * &lt;li&gt;&lt;a href="https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-security.html#boot-features-security-csrf"&gt;
       * Cross Site Request Forgery Protection&lt;/a&gt;&lt;/li&gt;
       * &lt;li&gt;&lt;a href="https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html"&gt;
       * Cross Site Request Forgery (CSRF)&lt;/a&gt;&lt;/li&gt;
       * &lt;/ul&gt;
       *
       * @param http the {@link HttpSecurity} to modify
       * @throws Exception if an error occurs
       */
      @Override
      protected void configure(@Nonnull final HttpSecurity http) throws Exception {
          super.configure(http);
  
          http.csrf().disable();
  
          http
                  .cors()
                  .and()
                  .authorizeRequests()
                  .antMatchers("/health", "/error").permitAll()
                  .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                  .anyRequest().authenticated();
      }
  
      /**
       * Avoid double {@link KeycloakAuthenticationProcessingFilter} bean registration.
       *
       * &lt;p&gt;
       * This will set {@link AbstractAuthenticationProcessingFilter#continueChainBeforeSuccessfulAuthentication} to
       * {@code false}, meaning that &lt;b&gt;after success authentication by this Keycloak filter -
       * further filters will be skipped&lt;/b&gt;. See
       * {@link KeycloakAuthenticationProcessingFilter#KeycloakAuthenticationProcessingFilter(
       * org.springframework.security.authentication.AuthenticationManager,
       * org.springframework.security.web.util.matcher.RequestMatcher) KeycloakAuthenticationProcessingFilter constructor}
       *
       * @param filter {@link KeycloakAuthenticationProcessingFilter} auth processing filter
       * @return disabled {@link FilterRegistrationBean}
       * @see &lt;a href="https://www.keycloak.org/docs/latest/securing_apps/index.html#avoid-double-filter-bean-registration"&gt;
       * Avoid double Filter bean registration&lt;/a&gt;
       */
      @Bean
      @Nonnull
      public FilterRegistrationBean keycloakAuthenticationProcessingFilterRegistrationBean(
              @Nonnull final KeycloakAuthenticationProcessingFilter filter) {
  
          final FilterRegistrationBean&lt;KeycloakAuthenticationProcessingFilter&gt; registrationBean =
                  new FilterRegistrationBean&lt;&gt;(filter);
  
          registrationBean.setEnabled(false);
          return registrationBean;
      }
  
      /**
       * Avoid double {@link KeycloakPreAuthActionsFilter} bean registration.
       *
       * @param filter {@link KeycloakPreAuthActionsFilter} filter
       * @return disabled {@link FilterRegistrationBean}
       * @see &lt;a href="https://www.keycloak.org/docs/latest/securing_apps/index.html#avoid-double-filter-bean-registration"&gt;
       * Avoid double Filter bean registration&lt;/a&gt;
       */
      @Bean
      @Nonnull
      public FilterRegistrationBean keycloakPreAuthActionsFilterRegistrationBean(
              @Nonnull final KeycloakPreAuthActionsFilter filter) {
          final FilterRegistrationBean&lt;KeycloakPreAuthActionsFilter&gt; registrationBean =
                  new FilterRegistrationBean&lt;&gt;(filter);
          registrationBean.setEnabled(false);
          return registrationBean;
      }
  
      /**
       * Avoid double {@link KeycloakAuthenticatedActionsFilter} bean registration.
       *
       * @param filter {@link KeycloakAuthenticatedActionsFilter} filter
       * @return disabled {@link FilterRegistrationBean}
       * @see &lt;a href="https://www.keycloak.org/docs/latest/securing_apps/index.html#avoid-double-filter-bean-registration"&gt;
       * Avoid double Filter bean registration&lt;/a&gt;
       */
      @Bean
      public FilterRegistrationBean keycloakAuthenticatedActionsFilterBean(
              @Nonnull final KeycloakAuthenticatedActionsFilter filter) {
          final FilterRegistrationBean&lt;KeycloakAuthenticatedActionsFilter&gt; registrationBean =
                  new FilterRegistrationBean&lt;&gt;(filter);
          registrationBean.setEnabled(false);
          return registrationBean;
      }
  
      /**
       * Avoid double {@link KeycloakSecurityContextRequestFilter} bean registration.
       *
       * @param filter {@link KeycloakSecurityContextRequestFilter} filter
       * @return disabled {@link FilterRegistrationBean}
       * @see &lt;a href="https://www.keycloak.org/docs/latest/securing_apps/index.html#avoid-double-filter-bean-registration"&gt;
       * Avoid double Filter bean registration&lt;/a&gt;
       */
      @Bean
      public FilterRegistrationBean keycloakSecurityContextRequestFilterBean(
              @Nonnull final KeycloakSecurityContextRequestFilter filter) {
  
          final FilterRegistrationBean&lt;KeycloakSecurityContextRequestFilter&gt; registrationBean =
                  new FilterRegistrationBean&lt;&gt;(filter);
  
          registrationBean.setEnabled(false);
          return registrationBean;
      }
  }
  </code></pre>
  
  <p>I've tried multiple approaches but none of them works. Sometimes I start getting CORS exception, once I fix it, it again starts throwing unauthorised user error. Kindly help in identifying what I am doing wrong.</p>
  
  <p>Stuff which I tried yet:</p>
  
  <ol>
  <li><pre><code>        super.configure(http);
          http.csrf().disable();
  
          http
  
  
             .cors()
              .and()
              .authorizeRequests()
              .antMatchers("/health", "/error").permitAll()
              .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
              .anyRequest().authenticated();
  </code></pre></li>
  <li><p><code>@Override
  public void configure(final WebSecurity web) {
      web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**");
  }</code></p></li>
  <li><p>`        http.csrf().disable();</p>
  
  <pre><code>http
          .cors()
          .and()
          .authorizeRequests()
          .antMatchers("/health", "/error").permitAll()
          .antMatchers(HttpMethod.OPTIONS, "/item/search").permitAll()
          .antMatchers(HttpMethod.OPTIONS, "/item/search/").permitAll()
          .antMatchers(HttpMethod.OPTIONS, "/item/search/**").permitAll()
          .antMatchers(HttpMethod.OPTIONS, "/**/search").permitAll()
          .antMatchers(HttpMethod.OPTIONS, "/**/search/").permitAll()
          .antMatchers(HttpMethod.OPTIONS, "/**/search/**").permitAll()
          .anyRequest().authenticated();`
  </code></pre></li>
  </ol>