title: How to integrate Keycloak with Spring boot(API) without login page
body: <p>There's need to integrate Keycloak with my spring boot application. What i need is that any REST request coming to my API will have a header e.g. "Authorisation" which will have value as "basic " to be used as auth token.
  The request came to API should be validated from keyclaok without redirecting to any login page of keycloak.
  All the tutorials to integrate keycloak with spring boot shows a login page or pre generated bearer token.</p>
  
  <p>When i try to do this, below is my SecurityConfig.java:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
  class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) {
          KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
          keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
          auth.authenticationProvider(keycloakAuthenticationProvider);
      }
  
      @Bean
      public KeycloakSpringBootConfigResolver keycloakConfigResolver() {
          return new KeycloakSpringBootConfigResolver();
      }
  
      @Bean
      @Override
      protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
          return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          super.configure(http);
          http.authorizeRequests().antMatchers("/myapi*").hasRole("user").anyRequest().permitAll();
      }
  }
  </code></pre>
  
  <p>My application.properties:</p>
  
  <pre><code>server.port=8081
  keycloak.auth-server-url=http://localhost:9080/auth
  keycloak.realm=myrealm
  keycloak.resource=myclient
  keycloak.public-client=false
  keycloak.credentials.secret=mysecret
  keycloak.use-resource-role-mappings=true
  keycloak.enabled=true
  keycloak.ssl-required=external
  </code></pre>
  
  <p>pom.xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.1.7.RELEASE&lt;/version&gt;
      &lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
  &lt;/parent&gt;
  &lt;groupId&gt;com.example.api&lt;/groupId&gt;
  &lt;artifactId&gt;springboot-kc-api&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;springboot-kc-api&lt;/name&gt;
  
  &lt;properties&gt;
      &lt;java.version&gt;1.8&lt;/java.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencyManagement&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.keycloak.bom&lt;/groupId&gt;
              &lt;artifactId&gt;keycloak-adapter-bom&lt;/artifactId&gt;
              &lt;version&gt;6.0.1&lt;/version&gt;
              &lt;type&gt;pom&lt;/type&gt;
              &lt;scope&gt;import&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/dependencyManagement&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
          &lt;scope&gt;runtime&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.keycloak&lt;/groupId&gt;
          &lt;artifactId&gt;keycloak-spring-boot-starter&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
  &lt;/dependencies&gt;
  
  &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p></p>
  
  <p>Whenever a GET request is made, keycloak debug shows below log:</p>
  
  <p>o.k.adapters.OAuthRequestAuthenticator   : Sending redirect to login page: <a href="http://localhost:9080/auth/realms/myrealm/protocol/openid-connect/auth?response_type=code&amp;client_id=myclient&amp;redirect_uri=http%3A%2F%2Flocalhost%3A8081%2Fmyapi%2Fsampleget?param1=val1&amp;state=a2b5072a-acb8-4bf6-8f33-b3f25deab492&amp;login=true&amp;scope=openid" rel="nofollow noreferrer">http://localhost:9080/auth/realms/myrealm/protocol/openid-connect/auth?response_type=code&amp;client_id=myclient&amp;redirect_uri=http%3A%2F%2Flocalhost%3A8081%2Fmyapi%2Fsampleget?param1=val1&amp;state=a2b5072a-acb8-4bf6-8f33-b3f25deab492&amp;login=true&amp;scope=openid</a></p>
  
  <p>Keycloak configuration:</p>
  
  <pre><code>Client Protocol : openid-connect
  Access Type : confidential
  Valid Redirect URIs: http://localhost:8081/myapi/*
  </code></pre>
  
  <p>Above setup working fine for an API written in Java REST Easy framework for one of existing application running on JBoss EAP 7.</p>
  
  <p>Need help to understand how to configure spring boot API to use auth header in request to authenticate &amp; authorise request.</p>