how to redirect the access denied page in spring security?

12,861

Solution 1

Have you read the relevant sections of the Spring Security manual, namely the AccessDeniedHandler and the namespace appendix.

If you want more control, you can use

 <http use-expressions="true">
     <intercept-url pattern="/denied/*" access="permitAll" />

     <access-denied-handler error-page="/denied">

     <!-- The rest of your configuration -->
 </http>

Where /denied maps to a web controller class which you write. Make sure /denied/** is unprotected.

If this doesn't answer your question, could you please explain in some more detail what you're trying to achieve?

Solution 2

Use a RedirectView for this purpose

Sample

       return new ModelAndView(new RedirectView(request.getContextPath()+ "urlPath"), modelMap);

or use the redirect: prefix.

Share:
12,861
sudo
Author by

sudo

Updated on June 30, 2022

Comments

  • sudo
    sudo almost 2 years

    I want to know how to redirect the access denied page in spring security? Shall I use some kind of handler or edit in web.xml?

    thanks

  • sudo
    sudo over 12 years
    I want to do it for authority.
  • Simeon
    Simeon over 12 years
    I'm not sure if comments are a proper way to say this, but you might consider saying that you're actually one of Spring-Security's developers in your profile. It could give your answers on the spring-security tag more credibility (which you should have IMO).