{ /** * @description:登录过滤校验 * @author: lijialuo * @date: 2023/11/19 13:28 * @param: [servletRequest, servletResponse, filterChain] * @return: void **/ @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) servletRequest; HttpServletResponse resp = (HttpServletResponse) servletResponse; //1.获取请求的url String url = req.getRequestURL().toString(); log.info("请求的路径为:{}",url); //2.判断url里面是否为登录的路径 login 如果是则放行 if (url.contains("login")) { log.info("登录请求,放行"); filterChain.doFilter(req,resp); return; } //3.不是登录路径,验证jwt令牌 //获取jwt令牌 String jwt = req.getHeader("token"); //验证有没有token,如果没有,则返回错误信息 if (!StringUtils.hasLength(jwt)) { //没有token log.info("该用户没有登录"); String noLogin = "false"; //手动转换 对象->json ---------->阿里巴巴fastJSON String s = JSONObject.toJSONString(noLogin); PrintWriter writer = resp.getWriter(); writer.write(s); writer.close(); return; } //jwt存在,如果解析失败,返回错误结果(未登录) try { JwtUtils.parseJWT(jwt); } catch (Exception e) { e.printStackTrace(); //解析失败 log.info("解析令牌失败"); String noLogin = "false"; String s = JSONObject.toJSONString(noLogin); PrintWriter writer = resp.getWriter(); writer.write(s); writer.close(); return; } //放行 log.info("令牌合法,放行"); filterChain.doFilter(req,resp); } }
标签:info,String,登录,resp,writer,jwt,过滤器,权限,Servlet From: https://www.cnblogs.com/lijialuo/p/17841957.html