I have a Spring MvC application and I’m trying to set up error pages, but when I access a page I don’t get a 403 error page and when I search for an unknown page I don’t get a 404 error page, instead I get a blank page that downloads a blank file. I suspect the problem is in the Spring Security securityFilterChain. Does anybody see a problem with the securityFilterChain:
`@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests(authorizeRequests ->
authorizeRequests
.requestMatchers(“/”, “/WEB-INF/“, “/static/“, “/token”, “/images/“, “/nieuws/“, “/zoeken/“, “/register”, “/registration”, “/error”).permitAll()
.requestMatchers(“/admin/“).hasRole(“ADMIN”)
.requestMatchers(“/user/**”).hasRole(“USER”)
.anyRequest().authenticated()
)
.formLogin(form -> form
.loginPage(“/login”)
.defaultSuccessUrl(“/”, true)
.permitAll()
)
.logout(logout -> logout.permitAll())
.exceptionHandling(exceptionHandling -> exceptionHandling
.accessDeniedHandler(accessDeniedHandler())
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
);
return http.build();
}`
I tried setting up a CustomErrorController, but none of the pages get reached. `@Controller
public class CustomErrorController implements ErrorController {
@RequestMapping("/error")
public String handleError(HttpServletRequest request) {
Object status = request.getAttribute("javax.servlet.error.status_code");
if (status != null) {
Integer statusCode = Integer.valueOf(status.toString());
if (statusCode == HttpStatus.NOT_FOUND.value()) {
return "error/404";
} else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
return "error/500";
} else if (statusCode == HttpStatus.FORBIDDEN.value()) {
return "error/403";
}
}
return "error/error"; // Generic error page
}`