@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").authenticated()
.antMatchers("/**").permitAll()
.and().exceptionHandling().accessDeniedPage("/handlers/access-denied.html");
}
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers(
// статика
"/css/**",
"/js/**",
"/fonts/**",
"../libs/**",
"/images/**"
);
}
}
@Controller
public class HttpErrorController implements ErrorController {
private final MessageSource messageSource;
@Autowired
public HttpErrorController(MessageSource messageSource) {
this.messageSource = messageSource;
}
@RequestMapping("/error")
public String handleError(
Locale locale,
Model model,
HttpServletRequest request,
Exception ex
) {
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
if (status != null) {
int statusCode = Integer.valueOf(status.toString());
Map<String, String> metaData = new HashMap<>();
// 403
if (statusCode == HttpStatus.FORBIDDEN.value()) {
// do somthing
}
// 404
else if (statusCode == HttpStatus.NOT_FOUND.value()) {
// do somthing
}
// 405
else if (statusCode == HttpStatus.NOT_FOUND.value()) {
// do somthing
}
// 500
else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
// do somthing
}
}
return "templates/errors/httperrors";
}
@Override
public String getErrorPath() {
return "/error";
}
}
# Disable Whitelabel Error Page
server.error.whitelabel.enabled=false