• 自定义AccessDeniedHandler


    在Spring默认的AccessDeniedHandler中只有对页面请求的处理,而没有对Ajax的处理。而在项目开发是Ajax又是我们要常用的技术,所以我们可以通过自定义AccessDeniedHandler来处理Ajax请求。我们在Spring默认的AccessDeniedHandlerImpl上稍作修改就可以了。

    1. public class DefaultAccessDeniedHandler implements AccessDeniedHandler {  
    2.   
    3.     /* (non-Javadoc) 
    4.      * @see org.springframework.security.web.access.AccessDeniedHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.springframework.security.access.AccessDeniedException) 
    5.      */  
    6.     private String errorPage;  
    7.   
    8.     //~ Methods ========================================================================================================  
    9.   
    10.     public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)  
    11.             throws IOException, ServletException {  
    12.         boolean isAjax = ControllerTools.isAjaxRequest(request);  
    13.         if(isAjax){  
    14.             Message msg = MessageManager.exception(accessDeniedException);  
    15.             ControllerTools.print(response, msg);  
    16.         }else if (!response.isCommitted()) {  
    17.             if (errorPage != null) {  
    18.                 // Put exception into request scope (perhaps of use to a view)  
    19.                 request.setAttribute(WebAttributes.ACCESS_DENIED_403, accessDeniedException);  
    20.   
    21.                 // Set the 403 status code.  
    22.                 response.setStatus(HttpServletResponse.SC_FORBIDDEN);  
    23.   
    24.                 // forward to error page.  
    25.                 RequestDispatcher dispatcher = request.getRequestDispatcher(errorPage);  
    26.                 dispatcher.forward(request, response);  
    27.             } else {  
    28.                 response.sendError(HttpServletResponse.SC_FORBIDDEN, accessDeniedException.getMessage());  
    29.             }  
    30.         }  
    31.     }  
    32.   
    33.     /** 
    34.      * The error page to use. Must begin with a "/" and is interpreted relative to the current context root. 
    35.      * 
    36.      * @param errorPage the dispatcher path to display 
    37.      * 
    38.      * @throws IllegalArgumentException if the argument doesn't comply with the above limitations 
    39.      */  
    40.     public void setErrorPage(String errorPage) {  
    41.         if ((errorPage != null) && !errorPage.startsWith("/")) {  
    42.             throw new IllegalArgumentException("errorPage must begin with '/'");  
    43.         }  
    44.   
    45.         this.errorPage = errorPage;  
    46.     }  
    47.   
    48. }  

    这里我们直接将异常信息通过PrintWriter输出到前台,然后在前台做统一的处理就可以了。在前台对后台消息统一处理的方法可以参考我的这篇文章http://blog.csdn.net/jaune161/article/details/18135607

    最后在配置文件中配置下

    1. <sec:http auto-config="true" access-decision-manager-ref="accessDecisionManager">  
    2.       
    3.     <sec:access-denied-handler ref="accessDeniedHandler"/>  
    4.       
    5.     <sec:session-management invalid-session-url="/login.jsp" />  
    6.       
    7.     <sec:intercept-url pattern="/app.jsp" access="AUTH_LOGIN"/>  
    8.     <sec:intercept-url pattern="/**" access="AUTH_GG_FBGBGG"/>  
    9.       
    10.     <sec:form-login login-page="/login.jsp" authentication-failure-url="/login.jsp"  
    11.         default-target-url="/index.jsp"/>  
    12.           
    13. </sec:http>  
    14.   
    15. <!-- 自定义权限不足处理程序 -->  
    16. <bean id="accessDeniedHandler" class="com.zrhis.system.security.RequestAccessDeniedHandler">  
    17.     <property name="errorPage" value="/WEB-INF/error/403.jsp"></property>  
    18. </bean>  
  • 相关阅读:
    iOS序列化与反序列化
    iOS官方文档阅读 基本格式指北
    iOS实现地图半翻页效果--老代码备用参考
    AVQueuePlayer
    ios音频视频资料--备用
    ios coredata 老代码备用参考
    uitableviewcell 自适应大小 参考
    触发UIButton长按事件
    gsoap 学习 1-由wsdl文件生成h头文件
    gsoap简介
  • 原文地址:https://www.cnblogs.com/yechanglv/p/6941796.html
Copyright © 2020-2023  润新知