spring security 3中关于ajax的处理

来源:互联网 发布:电脑椅推荐 知乎 编辑:程序博客网 时间:2024/06/15 19:52
在spring security 3中,对于某些需要保护的url,可以很容易地实现当没权限的时候, 
redirect到一个页面(比如自定义的404.jsp页面)进行显示没权限的信息; 
但有的时候,必须要对一些AJAX的请求url也同时判断其是否有权限输出; 
如果没权限的话,一般要以JSON的方式返回给用户端,比如弹出一个提示框,显示没权限; 
   在SPRING security 3中,当没权限的时候,会由spring security 自己的拦截器 
AccessDeniedHandler 进行拦截的,因此,可以在这个地方进行扩展自定义, 
然后统一返回给前端的都是json的方式,并且在前端的jquery js中,扩展jquery post的 
方法,如果对json返回的结果中,有相关“没权限操作”的信息,则弹出错误提示框, 
这样,只需要在要用到$ajax提交的页面中,引入js就可以了,下面看代码实现。 


1) 首先,实现AccessDeniedHandler 类; 
  
Java代码  收藏代码
  1. public class MyAccessDeniedHandlerImpl implements AccessDeniedHandler  {  
  2.       
  3.       
  4.     public MyAccessDeniedHandlerImpl()  
  5.     {  
  6.           
  7.     }  
  8.     public String getAccessDeniedUrl() {  
  9.         return accessDeniedUrl;  
  10.     }  
  11.    
  12.   
  13.     public void setAccessDeniedUrl(String accessDeniedUrl) {  
  14.         this.accessDeniedUrl = accessDeniedUrl;  
  15.     }  
  16.   
  17.   public MyAccessDeniedHandlerImpl(String accessDeniedUrl)  
  18.   {  
  19.       this.accessDeniedUrl=accessDeniedUrl;  
  20.   }  
  21.     private String accessDeniedUrl;  
  22.   
  23.       
  24.     @Override  
  25.     public void handle(HttpServletRequest req,  
  26.             HttpServletResponse resp, AccessDeniedException reason) throws ServletException,  
  27.             IOException {  
  28.         boolean isAjax = "XMLHttpRequest".equals(req.getHeader("X-Requested-With"));  
  29.           
  30. //如果是ajax请求  
  31.         if (isAjax) {         
  32.               
  33.             String jsonObject = "{\"message\":\"You are not privileged to request this resource.\","+  
  34.             //      "\"access-denied\":true,\"cause\":\"AUTHORIZATION_FAILURE\"}";  
  35.             String contentType = "application/json";  
  36.             resp.setContentType(contentType);  
  37.             String jsonObject="noright";  
  38.             PrintWriter out = resp.getWriter();  
  39.             out.print(jsonObject);  
  40.             out.flush();  
  41.             out.close();  
  42.             return;  
  43.         }  
  44.         else  
  45.         {  
  46.           
  47.          String path = req.getContextPath();  
  48.          String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/";  
  49.          resp.sendRedirect(basePath+accessDeniedUrl);  
  50.         }  
  51.           
  52.           
  53.     }  

  在上面的处理中,判断如果是ajax处理,则输出json字符串给客户端,否则就 
redirect到指定的accessDeniedUrl, 

2) 在applicationContext-security.xml中进行设置,如下: 
   
Java代码  收藏代码
  1.    <http auto-config="true">  
  2. <intercept-url pattern="/admin*" access="ROLE_ADMIN" />  
  3. <access-denied-handler ref="accessDeniedHandler"/>  
  4.   </http>  
  5.   
  6.   
  7.     <beans:bean id="accessDeniedHandler"   
  8. class="com.test.MyAccessDeniedHandlerImpl">  
  9. <beans:property name="accessDeniedUrl" value="403.jsp" />  
  10.   </beans:bean>  


3) springsecurity.js 
  
Java代码  收藏代码
  1. (function($){  
  2.     // 保存原有的jquery ajax;  
  3.     var $_ajax = $.ajax;  
  4.       
  5.     $.ajax = function(options){  
  6.         var originalSuccess,  
  7.             mySuccess,  
  8.             success_context;  
  9.       
  10.         if (options.success) {  
  11.                         // save reference to original success callback  
  12.             originalSuccess = options.success;  
  13.             success_context = options.context ? options.context : $;  
  14.               
  15.                         // 自定义callback  
  16.             mySuccess = function(data) {  
  17.                   
  18.                   
  19.                                          
  20.                              
  21.                             if (data['access-denied']) {  
  22.                                   if (data.cause==='AUTHENTICATION_FAILURE') {  
  23.                                     alert('登录超时,请重新登录.');  
  24.                     window.location.href = contextPath + '/';  
  25.                                   } else if (data.cause==='AUTHORIZATION_FAILURE') {  
  26.                                        if (data=="noright")  
  27.                                        {  
  28.                                         alert('对不起,你没有访问该资源的权限.');  
  29.                                        }  
  30.                     }  
  31.                                    return;  
  32.                           
  33.                                
  34.                                 // call original success callback                             
  35.                 originalSuccess.apply(success_context, arguments);  
  36.             };  
  37.                         // override success callback with custom implementation  
  38.             options.success = mySuccess;  
  39.         }  
  40.           
  41.                 // call original ajax function with modified arguments  
  42.         $_ajax.apply($, arguments);  
  43.     };  
  44.       
  45. })(jQuery);  
0 0