Spring Security教程(9)---- 自定义AccessDeniedHandler

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

[java]  view plain copy 在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 plain copy 在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>  
session-management本来计划在之前就讲的,但是准备深入讲下session-management所以就一直没有讲。今天既然提到了就简单的说下session-management最简单的配置,就是上面的配置invalid-session-url表示Session失效时跳转的连接。随后会深入讲下这个。
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值