/**
* 自定义参数异常
*/
public class NoLoginException extends RuntimeException {
private Integer code=300;
private String msg="用户未登录!";
//提供get set 方法
}
全局异常处理
/**
* 全局异常处理
*/
@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {
/**
* 异常处理方法
* 方法的返回值
* 1、返回视图
* 2、返回数据(JSON)
*
* @param httpServletRequest
* @param httpServletResponse
* @param handler
* @param e
* @return
*/
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, Exception e) {
/**
* 非法请求拦截
*/
if (e instanceof NoLoginException) {
//重定向到登陆界面
ModelAndView modelAndView = new ModelAndView("redirect:/index");
return modelAndView;
}
/**
*默认异常处理,返回视图
*/
ModelAndView modelAndView = new ModelAndView("Error");
//设置异常信息
modelAndView.addObject("Code", 500);
modelAndView.addObject("msg", "系统异常,请重试...");
//判断handlerMethod
if (handler instanceof HandlerMethod) {
//类型转换
HandlerMethod handlerMethod = (HandlerMethod) handler;
//获取方法上声明的@ResponseBody注解对象
ResponseBody responseBody = handlerMethod.getMethod().getDeclaredAnnotation(ResponseBody.class);
//判断ResponseBody对象是否为空,(如果为空,返回视图
// 如果不为空,返回数据)
if (responseBody == null) {
//设置默认返回视图
if (e instanceof ParamsException) {
ParamsException p = (ParamsException) e;
modelAndView.addObject("code", p.getCode());
modelAndView.addObject("msg", p.getMsg());
}
return modelAndView;
} else {
//返回数据
//设置默认的异常处理
ResultInfo resultInfo = new ResultInfo();
resultInfo.setCode(500);
resultInfo.setMsg("系统异常,请重试!");
//判断是否为自定义异常
if (e instanceof ParamsException) {
ParamsException p = (ParamsException) e;
resultInfo.setCode(p.getCode());
resultInfo.setMsg(p.getMsg());
}
//设置响应类型与编码格式
httpServletResponse.setContentType("application/json;charset=UTF-8");
//得到字符输出流
PrintWriter out = null;
try {
//得到输出流
out = httpServletResponse.getWriter();
//将需要返回的对象转换成JSON格式字符串
String json = JSON.toJSONString(resultInfo);
//输出数据
out.write(json);
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
return null;
}
}
return modelAndView;
}
}
拦截器(非法访问拦截器)
/**
* 非法访问拦截
*/
public class NoLoginInterceptor extends HandlerInterceptorAdapter {
@Resource
private UserMapper userMapper;
/**
* 拦截用户是否登陆状态
* 返回true 目标方法可以执行
* 返回false 目标方法不可以执行
*
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//获取cookie中用户id
Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
//判断用户ID是否为空,且数据库中是否存在用户记录
if (null == userId || userMapper.selectByPrimaryKey(userId) == null) {
//抛出未登陆异常
throw new NoLoginException();
}
return true;
}
}
资源拦截器
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Bean //
public NoLoginInterceptor noLoginInterceptor() {
return new NoLoginInterceptor();
}
/**
* 添加拦截器
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(noLoginInterceptor()).addPathPatterns("/**") //默认拦截所有资源
.excludePathPatterns("/CSS/**", "/images/**", "/js/**", "/lib/**","/index","/user/login"); //默认放弃拦截的页面
}
}