title: custom 403 error page with spring security configured via java code
body: <p>Anyone knows how to configure a customized 403 page in spring security? Looking in the web, all the results I get it's with XML configuration, and I am using Java configuration. That's my SecurityConfig:</p>
  
  <pre><code>@Configuration
  @ComponentScan(value="com")
  @EnableWebSecurity
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Bean
      @Override
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return new CustomAuthenticationManager();
      }
  
      protected void configure(HttpSecurity http) throws Exception {
          http
              .csrf()
                  .disable()
              .authorizeRequests()
                  .antMatchers("/resources/**", "/publico/**").permitAll()
                  .anyRequest().authenticated()
                  .and()
              .formLogin()
                  .loginPage("/acesso/login").permitAll()
                  .loginProcessingUrl("/login").permitAll()
                  .usernameParameter("login")
                  .passwordParameter("senha")
                  .successHandler(new CustomAuthenticationSuccessHandler())
                  .failureHandler(new CustomAuthenticationFailureHandler())
                  .and()
              .logout()
                  .logoutUrl("/logout")
                  .logoutSuccessUrl("/acesso/login").permitAll();
      }
  
  }
  </code></pre>
  
  <p>I have a custom implementation for AccessDeniedHandler too:</p>
  
  <pre><code>public class CustomAccessDeniedHandler implements AccessDeniedHandler {
  
      @Override
      public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException arg2) throws IOException, ServletException {
          response.sendRedirect(request.getContextPath() + "/erro/no_permit");
      }
  
  }
  </code></pre>