1. 程式人生 > >CRM客戶關係管理系統<3>全域性異常處理以及登入攔截

CRM客戶關係管理系統<3>全域性異常處理以及登入攔截

全域性異常處理 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";
    }

重新整理父頁面