title: How to test Keycloak authentication in Spring Boot application?
body: <p>In a <em>Spring Boot</em> project we enabled <em>Spring Security</em> and applied Keycloak authentication with bearer token like described in the following articles:</p>
  
  <p><a href="https://www.keycloak.org/docs/3.2/securing_apps/topics/oidc/java/spring-security-adapter.html" rel="noreferrer">https://www.keycloak.org/docs/3.2/securing_apps/topics/oidc/java/spring-security-adapter.html</a></p>
  
  <p><a href="https://www.keycloak.org/docs/3.2/securing_apps/topics/oidc/java/spring-boot-adapter.html" rel="noreferrer">https://www.keycloak.org/docs/3.2/securing_apps/topics/oidc/java/spring-boot-adapter.html</a></p>
  
  <p>But i can't find any recommendations how to make automation tests so that the Keycloak config is applied.</p>
  
  <p>So, how to test/mock/verify the Keycloak configuration when Spring security is enabled? One really annoying thing: by default Spring activates <em>csrf</em> security filter, but how to avoid testing it?</p>
  
  <p>(Note: we use bearer tokens, so looks like <code>@WithMockUser</code> is not applicable in this case)</p>
  
  <p>A bonus question:
  basically we don't want to verify security on each controller integration test, so is it possible to verify security separately from the controllers integration tests (those which use <code>@SpringBootTest</code>, <code>@WebAppConfiguration</code>, <code>@AutoConfigureMockMvc</code> and so on?</p>