CRM客户关系管理系统<3>全局异常处理以及登录拦截

版权声明:有一种生活不去经历不知其中艰辛,有一种艰辛不去体会,不会知道其中快乐,有一种快乐,没有拥有不知其中纯粹 https://blog.csdn.net/wwwzydcom/article/details/83105859

全局异常处理
1.判断页面请求:报错,跳转到错误页面
2.json请求:返回错误的json数据
解决方法:通过反射获取目标方法的注解,有就是json请求,没有是普通请求

@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
                                         Object handler, Exception ex) {
       ModelAndView mv = createDefaultModelAndView(request,ex);
       //登录异常的处理

        if (ex instanceof LoginExcepiton){
            mv.addObject("errorMsg", CrmConstant.USER_NOT_LOGIN_MSG);
            mv.setViewName("login_error");
            return mv;
        }
        /**
         * 区分是什么异常,页面请求或者json请求
         */
        if (handler instanceof HandlerMethod)//逻辑上的安全,是该方法就出处理
        {
            /**
             * 解决:通过反射获取目标方法的注解,有就是json请求没有.普通请求
             */
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            ResponseBody responseBody = method.getAnnotation(ResponseBody.class);
            if (null == responseBody){
                //普通页面请求
                if (ex instanceof ParamsException){

                    ParamsException e = (ParamsException) ex;
                    mv.addObject("errorMsg",e.getMessage());

                }
            }else {
                //json请求
                ResultInfo info = new ResultInfo();
                info.setCode(300);
                info.setMsg("系统繁忙");
                if (ex instanceof ParamsException){
                    ParamsException e = (ParamsException) ex;
                    info.setMsg(e.getMsg());
                }
                //设置返回的响应编码
                response.setCharacterEncoding("utf-8");
                response.setContentType("application/json;charset=utf-8");
                //用流的方式输出
                PrintWriter pw = null;

                try {
                    pw = response.getWriter();
                    pw.write(JSON.toJSONString(info));
                    pw.flush();
                    pw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    //确保流关闭
                    if (null!=pw){
                        pw.close();
                    }
                }
                return null;
            }
        }

        return mv;
    }

    private ModelAndView createDefaultModelAndView(HttpServletRequest request, Exception ex) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("error"); //跳转到错误页面
        mv.addObject("errorMsg","系统繁忙");   //错误信息
        mv.addObject("errorCode",300);      //错误码
        mv.addObject("uri",request.getRequestURI());        //请求路径
        mv.addObject("ctx",request.getContextPath());       //上下文路径
        mv.addObject("ctx",request.getContextPath());   //发生错误的时候,跳出Controller层 ctx是没有值的
        return mv;
    }
}

登录拦截

public class LoginInterceptor extends HandlerInterceptorAdapter {
    @Resource
    private UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       //从cookie里面获取用户id ,1.cookie存在,并且cookie里面的信息是正确的
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
        AssertUtil.isNotLogin(null==userService.queryById(userId) || (null == userId),CrmConstant.USER_NOT_LOGIN_MSG);
        return true;
    }
}

BUG:清除cookie但是只刷新的子页面,出现页面嵌套

解决方案
在error的页面配置

if('${uri}'=="/main"){
        window.location.href="${ctx}/index";
    }else{
        window.parent.location.href="${ctx}/index";
    }

刷新父页面

猜你喜欢

转载自blog.csdn.net/wwwzydcom/article/details/83105859