title: Using httpBasic with AuthenticationEntryPoint
body: <p>I am following <a href="https://grokonez.com/spring-framework/spring-boot/angular-spring-boot-jwt-authentication-example-angular-6-spring-security-mysql-full-stack-part-2-build-backend" rel="nofollow noreferrer">this tutorial</a> on using JWT authentication with spring boot. I can login and access the api from postman as shown in the tutorial. However, when I try to do the same from the browser at localhost:8080, I get a 401 Unauthorized error.</p>
  
  <p>The configure method used in the tutorial is</p>
  
  <pre><code>@Override
  protected void configure(HttpSecurity http) throws Exception {
      http.cors().and().csrf().disable()
        .authorizeRequests().antMatchers("/api/auth/**").permitAll().anyRequest()
        .authenticated().and().exceptionHandling()
        .authenticationEntryPoint(unauthorizedHandler).and()
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  
      http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
  }
  </code></pre>
  
  <p>When I remove the authenticationEntryPoint and add httpbasic, the login dialog is displayed in the browser when accessing localhost:8080.</p>
  
  <pre><code>@Override
  protected void configure(HttpSecurity http) throws Exception { 
      http.cors().and().csrf().disable()
        .authorizeRequests().antMatchers("/api/auth/**").permitAll().anyRequest()
        .authenticated().and().exceptionHandling()
        .and().httpBasic().and().sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  }
  </code></pre>
  
  <p>But when I try to add httpBasic with authenticationEntryPoint, no login dialog is shown.</p>
  
  <pre><code>@Override
  protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
          .authorizeRequests().antMatchers("/api/auth/**").permitAll().anyRequest()
          .authenticated().and().exceptionHandling()
          .authenticationEntryPoint(unauthorizedHandler)
          .and().httpBasic().and().sessionManagement()
          .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  
    http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
  }
  </code></pre>
  
  <p><strong>Updated</strong></p>
  
  <p>The <code>unauthorizedHandler</code> is an implementation of AuthenticationEntryPoint.</p>
  
  <pre><code>@Component
  public class UnauthorizedHandler implements AuthenticationEntryPoint {
  
      private static final Logger logger = LoggerFactory.getLogger(UnauthorizedHandler.class);
  
      @Override
      public void commence(HttpServletRequest request,
                       HttpServletResponse response,
                       AuthenticationException e) 
                               throws IOException, ServletException {
  
          logger.error("Unauthorized error. Message - {}", e.getMessage());
          response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Error -&gt; Unauthorized");
  }
  </code></pre>
  
  <p>}</p>
  
  <p>How can I display the login dialog while using AuthenticationEntryPoint?</p>