title: How do I add HTTP basic auth for a specific endpoint with spring security?
body: <p>I have a Spring Boot application with Spring Security. A new endpoint <code>/health</code> is to be configured so it is accessible via basic HTTP authentication. The current <code>HttpSecurity</code> configuration is as follows:</p>
  
  <pre><code>@Override
  protected void configure(HttpSecurity http) throws Exception {
  
  http.requestMatchers()
      .antMatchers(HttpMethod.OPTIONS, "/**")
      .and()
      .csrf()
      .disable()
      .authorizeRequests()
      .anyRequest()
      .permitAll()
      .and()
      .sessionManagement()
      .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  </code></pre>
  
  <p>}</p>
  
  <p>How do I add base auth for <code>/health</code>? I figure I need something like this, but I don't think this is completely correct, and I don't really understand where exactly to add it:</p>
  
  <pre><code>    .authorizeRequests()
      .antMatchers(
          // Health status
          "/health",
          "/health/"
      )
      .hasRole(HEALTH_CHECK_ROLE)
      .and()
      .httpBasic()
      .realmName(REALM_NAME)
      .authenticationEntryPoint(getBasicAuthEntryPoint())
      .and()
      .sessionManagement()
      .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
  </code></pre>
  
  <p>I found these resources to be helpful, but not sufficient:</p>
  
  <ul>
  <li><a href="http://www.baeldung.com/spring-security-basic-authentication" rel="noreferrer">http://www.baeldung.com/spring-security-basic-authentication</a></li>
  <li><a href="http://websystique.com/spring-security/secure-spring-rest-api-using-basic-authentication/" rel="noreferrer">http://websystique.com/spring-security/secure-spring-rest-api-using-basic-authentication/</a></li>
  </ul>