zoukankan      html  css  js  c++  java
  • 自定义AccessDeniedHandler

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

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
     
    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

    最后在配置文件中配置下

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
     
    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>  
  • 相关阅读:
    June 26th 2017 Week 26th Monday
    June 25th 2017 Week 26th Sunday
    June 24th 2017 Week 25th Saturday
    June 23rd 2017 Week 25th Friday
    June 22nd 2017 Week 25th Thursday
    2018最佳网页设计:就是要你灵感爆棚!!!
    图片素材类Web原型制作分享-Pexels
    想要打动HR的心,UX设计师求职信究竟应该怎么写?
    【UXPA大赛企业专访】Mockplus:“设计替代开发”将成为现实
    2018年最好的医疗网站设计及配色赏析
  • 原文地址:https://www.cnblogs.com/yanduanduan/p/5162215.html
Copyright © 2011-2022 走看看