Spring security log or add custom message for each unathorized (access denied) request

Viewed 66

I have spring security applied like below:

httpSecurity.authorizeRequests().antMatchers("/ctxPath1/**", "/ctxPath2/**", "/ctxPath3/**").access("hasAuthority('ADMIN')");

All works fine here. i.e. user with any other role than ADMIN get access denied message (page/view). No issues here at all.

Situation: I want to send a custom friendly message to view/page for each denied /ctxPathX. Something like - You tried to access "Feature X" on which you dont have access. Click here to send a request to Administrator for this feature entitlement.

Is that possible at all? If yes, where should I start looking and what options we have to achieve that?

Summary: Can I configure/code and send custom message to view(s) for each denied path individually?

1 Answers

You can look at this article. You can add a custom handler (implements AccessDeniedHandler) to display an error page.

.exceptionHandling().accessDeniedHandler(accessDeniedHandler());
Related