I\'ve got the following Spring Security configuration:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
The normal Spring Security behavior is to redirect unauthenticated users to your login page as configured below. Authenticates users who are not authorized (dont have the ADMIN role) will be directed to the access denied page:
http.authorizeRequests().antMatchers("/admin/**")
.access("hasRole('ADMIN')")
.and().formLogin().loginPage("/login")
.and().exceptionHandling().accessDeniedPage("/403");
If you have implemented your own authentication mechanism and you are not counting on the Spring Security configuration to deliver unauthenticated users to your login page, you can game the Spring Security configuration as follows - to serve your custom 403 page instead of a real login page:
http.authorizeRequests().antMatchers("/admin/**")
.access("hasRole('ADMIN')")
.and().formLogin().loginPage("/403")
.and().exceptionHandling().accessDeniedPage("/403");