title: Spring Boot Security Login (v4)
body: <p>I've been racking my brain on this one for a couple days now. Backstory is, I'm using Spring Boot with <code>spring-boot-starter-security</code> dependency for the security aspect. I'm trying to build a REST Web Service that should only reply with JSON (or XML if I decide to implement that as well). I can return resources and my URI's work fine, so no problem there. I even have json object authentication on the security.</p>
  
  <p>So Here's my problem. When an unauthorized user tries to access a restricted resource, Spring Security delivers a login page (HTML). This is problematic. I just want it to return a 401 and possibly a JSON error object of my choosing. Same with successful logins. I don't want it to redirect. And especially not a pre-made HTML page.</p>
  
  <p>I have looked up tutorials and everyone mentions using <code>AuthenticationEntryPoint</code> and the like for controlling the flow. BUT! I don't have that. Apparently that method doesn't exist anymore for HttpSecurity class. Anyone have the answer? Here's the relevant code, I'll supply more if you need it.</p>
  
  <pre><code>    @Configuration
  public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
      @Autowired
      private UserDetailsService userDetailsService;
  
      private JsonAuthenticationFilter authFilter;
  
      @Override
      protected void configure(HttpSecurity httpSecurity) throws Exception {
          authFilter = new JsonAuthenticationFilter();
          authFilter.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/login","POST"));
          authFilter.setAuthenticationManager(this.authenticationManagerBean());
  
          httpSecurity
              .addFilterBefore(authFilter, UsernamePasswordAuthenticationFilter.class)
              .csrf()
                  .disable()
              .formLogin()
                  .permitAll()
                  .loginProcessingUrl("/login")
                  .failureUrl("/error")
                  .usernameParameter("username")
                  .passwordParameter("password")
              .and()
              .logout()
                  .permitAll()
                  .logoutUrl("/logout")
              .and()
              .authorizeRequests()
                  .antMatchers("/", "/version")
                  .permitAll()
              .and()
              .authorizeRequests()
                  .anyRequest()
                  .authenticated();
      }
  
      @Override
      public void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
      }
  }
  </code></pre>
  
  <p>And the maven dependencies...</p>
  
  <pre><code>&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;1.3.2.RELEASE&lt;/version&gt;
      &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  &lt;/parent&gt;
  
  &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;java.version&gt;1.8&lt;/java.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-hateoas&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
          &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
          &lt;scope&gt;runtime&lt;/scope&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.restdocs&lt;/groupId&gt;
          &lt;artifactId&gt;spring-restdocs-mockmvc&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-starter-security&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.datastax.cassandra&lt;/groupId&gt;
          &lt;artifactId&gt;cassandra-driver-mapping&lt;/artifactId&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>