vue-admin-template 登录过滤器Filter,校验session(支持跨域、支持判断是否ajax请求) 作者:马育民 • 2022-09-24 09:43 • 阅读:10166 需要掌握:[vue-admin-template 目录](https://www.malaoshi.top/show_1IX2LobgPU2c.html "vue-admin-template 目录") # 说明 需要依赖 jackson 库,将 java 对象转成 json字符串 ### 登录过滤器 - 跨域时,非简单请求,会发送 `OPTIONS` 请求,需要判断该请求,并直接放行 - 从 `session` 中取出用户信息对象,并判断是否登录 - 如果没有登录,发送的是 ajax 请求,就返回 json 字符串;如果是普通请求,就跳转到登录页面 ### 注意 - 如果前端使用 axios 发送 ajax 请求,**在跨域时**,需要指定 `x-requested-with` 头,详见 [链接](https://www.malaoshi.top/show_1IX46ZsMmg8T.html "链接") - 使用某些小程序发请求,可能需要指定 `x-requested-with` 头,具体看该小程序开发文档 # 代码 ``` package com.zrgj.epidemic.filter; import com.zrgj.epidemic.pojo.Result; import com.zrgj.epidemic.util.ResponseUtil; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; @WebFilter(urlPatterns = {"/*"}) public class LoginFilter2 implements Filter { private static List notFilterList = new ArrayList(); static { notFilterPath.add("/login.html"); // 必须允许访问登录页,所以不需要过滤 notFilterPath.add("/login"); // 必须允许访问登录请求,所以不需要过滤 // 登录过滤器,不过滤下面 js、css路径,如果认为不安全,则注销掉 notFilterPath.add("/js/*");//支持通配符* notFilterPath.add("/css/*"); } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; String method = request.getMethod(); System.out.println("method:"+method); //跨域会发送 OPTIONS 请求,直接放行 if("OPTIONS".equals(method)){ chain.doFilter(req,resp); return; } boolean res = isNotFilter(request); if(res){ chain.doFilter(req,resp); return; } HttpSession session = request.getSession(); if(session.getAttribute("user") == null){ // 判断是否ajax请求 if(Ajax.isAjax(req)){ JsonResult result = new JsonResult(JsonResult.CODE_NO_LOGIN,"您未登录或登录超时,请重新登录!"); ResponseUtil.sendJSON(response,result); }else{ // System.out.println("普通请求"); String context = response.getContextPath(); response.sendRedirect(context+"/login.html"); } }else{ chain.doFilter(req, resp); } } /** * 判断该路径是否需要过滤 * @param request false,不需要过滤;true,需要过滤 * @return */ private boolean isNotFilter(HttpServletRequest request){ String servletPath = request.getServletPath(); // 问1:如何在过滤器中,知道访问的是 静态文件(内容不会变的文件:.html、.css、.jpg、.gif) // String uri = request.getRequestURI(); // System.out.println("uri:"+servletPath); // 问2:如何刨除去这些请求,不经过过滤器 for(String item : notFilterList){ item = item.replaceAll("\\.","\\\\."); item = item.replaceAll("\\*","\\.\\*"); boolean res = servletPath.matches(item); if(res){ return true; } } return false; } } ``` ### Ajax 详见: https://www.malaoshi.top/show_1IX72EERV9ES.html ### JsonResult 类 详见:https://www.malaoshi.top/show_1IX2rn0kwlcB.html ### ResponseUtil 详见:https://www.malaoshi.top/show_1IX2rmww9nux.html 原文出处:http://malaoshi.top/show_1IX46aIZfaGv.html