@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException { System.out.println("-----------------------进入拦截器-------------------------"); //每次都会先发一个不带参数的OPTIONS请求,若此请求失败则不会发起正式请求,所以要过滤掉OPTIONS请求 if (HttpMethod.OPTIONS.toString().equals(request.getmethod())) { System.out.println("OPTIONS请求,放行"); return true; } try { String token=request.getHeader("Authorization"); //如果验证token失败,并且方法注明了Authorization,返回401错误 if (token == null) { //设置response状态 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); //返回的数据 JSONObject res = new JSONObject(); res.put("status","-1"); res.put("msg","need login"); PrintWriter out = null ; out = response.getWriter(); out.write(res.toString()); out.flush(); out.close(); return false; } }catch (Exception e){ return false; } return true; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。