安全约束阻止访问时如何发送消息或重定向用户

How to send message or redirect user when security constraint block access

我的 web.xml

中有以下安全限制
<security-constraint>
    <display-name>Admin Pages</display-name>
    <web-resource-collection>

        <web-resource-name>Protected Admin Area</web-resource-name>
        <description/>
        <url-pattern>/administrator/*</url-pattern>
        <url-pattern>/faces/backend/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
        <http-method>HEAD</http-method>
        <http-method>PUT</http-method>
        <http-method>OPTIONS</http-method>
        <http-method>TRACE</http-method>
        <http-method>DELETE</http-method>
    </web-resource-collection>
    <auth-constraint>
        <description/>
        <role-name>administrator</role-name>
    </auth-constraint>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint> 

它工作正常。但我想重定向用户(已经登录的用户具有一些分配角色但不是角色管理员)。在这种情况下。当用户尝试访问 url http://mywebap//administrator/* 时,他从服务器

得到响应
403 Forbiden. 

我不会在这里显示这个,而是将用户重定向到一些更友好的视图。有可能吗?

只需将所需页面配置为 web.xml 中的自定义 HTTP 403 错误页面即可。

<error-page>
    <error-code>403</error-code>
    <location>/WEB-INF/errorpages/403.xhtml</location>
</error-page>

这假设您已将 *.xhtml 涵盖为 URL pattern of FacesServlet. And, it's being placed in /WEB-INF to prevent direct access