Springboot+Shiro统一异常处理包含Ajax

8 篇文章 0 订阅
6 篇文章 0 订阅

1.采用方式

这里通过 @ControllerAdvice和@ExceptionHandler 的异常处理方式

2.具体步骤

1.创建统一异常处理类(GlobalExpetion)

2.定义判断是否是Aajx请求的方法

 public boolean isAjax(HttpServletRequest request) {
	        return (request.getHeader("X-Requested-With") != null &&
	                "XMLHttpRequest".equals(request.getHeader("X-Requested-With").toString()));
	    }

3.不是ajax请求的异常方法

public  ModelAndView  exceptionPD(Exception ex,HttpServletRequest request) {
		 ModelAndView modelAndView=new ModelAndView();
		 if(ex instanceof UnauthenticatedException) {
  		  	modelAndView.addObject("msg", "认证时间已过,请重新登录");
  		  	modelAndView.addObject("login",0);
  	     }
  	   else if(ex instanceof UnauthorizedException) {
  		   modelAndView.addObject("msg", "你还没有该访问权限!!!请联系管理员吧");
		}
  	   else if(ex instanceof Exception){
  		 modelAndView.addObject("msg", "系统异常");
  		
  	     }
  	    modelAndView.setViewName(DEFAULT_ERROR_VIEW);
		 return modelAndView;
	   }

4.ajax异常处理方法

public  JSONResult  ajaxException(Exception ex,HttpServletRequest request,HttpServletResponse response) {
		 //自定义的返回数据类
		 JSONResult json=new JSONResult();
		 if(ex instanceof UnauthenticatedException) {
			   json.setStatus(401);
		  	 	json.setMsg("未认证或者session过期"); 
		  	   Map<String, Object> map=new HashMap<>();
	    	   map.put("title","是否重新登录");
	    	   map.put("url","/login");
	       }
	    else if(ex instanceof UnauthorizedException) {
	    	   json.setMsg("没有权限");
	    	   json.setStatus(403);
	    	   Map<String, Object> map=new HashMap<>();
	    	   map.put("title","是否切换账户,重新登录");
	    	   map.put("url","/login");
		      }
	   else if(ex instanceof Exception){
		        json.setStatus(500);
		        json.setMsg("系统异常");
	         }
		
		 return json;
	 }

最后在异常接受中判断什么异常,是不是ajax请求,再决定怎么跳转

@ExceptionHandler(Exception.class)
	@ResponseBody
    public Object errorHandler(HttpServletRequest request,
            HttpServletResponse response, Exception ex) {
       ex.printStackTrace();
       //是不是ajax请求
        if(isAjax(request)){ 
           //传递异常,调用ajaxException方法
           return ajaxException(ex, request, response);
         }
        else {
        	 //不是异步请求 	
            return exceptionPD(ex,request);
        }
		
	}

3.完整代码

package com.xyz.controller;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import com.xyz.util.JSONResult;

@ControllerAdvice
public class GlobalExpetion {


    public static final String DEFAULT_ERROR_VIEW = "error/error";
	@ExceptionHandler(Exception.class)
	@ResponseBody
    public Object errorHandler(HttpServletRequest request,
            HttpServletResponse response, Exception ex) {
       ex.printStackTrace();
        if(isAjax(request)){ 
           return ajaxException(ex, request, response);
         }
        else {
        	 //不是异步请求 	
            return exceptionPD(ex,request);
        }
		
	}
	 public boolean isAjax(HttpServletRequest request) {
	        return (request.getHeader("X-Requested-With") != null &&
	                "XMLHttpRequest".equals(request.getHeader("X-Requested-With").toString()));
	    }
	 public  ModelAndView  exceptionPD(Exception ex,HttpServletRequest request) {
		 ModelAndView modelAndView=new ModelAndView();
		 if(ex instanceof UnauthenticatedException) {
  		  	modelAndView.addObject("msg", "认证时间已过,请重新登录");
  		  	modelAndView.addObject("login",0);
  	     }
  	   else if(ex instanceof UnauthorizedException) {
  		   modelAndView.addObject("msg", "你还没有该访问权限!!!请联系管理员吧");
		}
  	   else if(ex instanceof Exception){
  		 modelAndView.addObject("msg", "系统异常");
  		
  	     }
  	    modelAndView.setViewName(DEFAULT_ERROR_VIEW);
		 return modelAndView;
	   }
	 
	 public  JSONResult  ajaxException(Exception ex,HttpServletRequest request,HttpServletResponse response) {
		 JSONResult json=new JSONResult();
		 if(ex instanceof UnauthenticatedException) {
			   json.setStatus(401);
		  	 	json.setMsg("未认证或者session过期"); 
		  	   Map<String, Object> map=new HashMap<>();
	    	   map.put("title","是否重新登录");
	    	   map.put("url","/login");
	       }
	    else if(ex instanceof UnauthorizedException) {
	    	   json.setMsg("没有权限");
	    	   json.setStatus(403);
	    	   Map<String, Object> map=new HashMap<>();
	    	   map.put("title","是否切换账户,重新登录");
	    	   map.put("url","/login");
		      }
	   else if(ex instanceof Exception){
		        json.setStatus(500);
		        json.setMsg("系统异常");
	         }
		
		 return json;
	 }
	 
	
}

注意:
1.JSONResult 类是我自已定义的消息返回类。
2.根据实际情况修改代码,这只是适用于当前项目
3.对异常的拦截这一块可以根据你自己的需求去拦截,注意普通异常和异步异常的返回消息不同
4.有不好的地方欢迎指点。

如有疑问可以留言交流!!!!!!!!!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值