package com.medicine.management_side.filters;
import com.alibaba.fastjson.JSONObject;
import com.medicine.management_side.utils.RedisUtil;
import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
//允许在哪个请求上起作用
@WebFilter(urlPatterns={"/patientController/*","/doctorOrderController/*","/doctroController/*","/hospitalController/*","/PatientRegisterHistoryController/*","/MbConsultationRoomRegisterController/*","/MbDoctorRegister/*"},filterName="corsFilter", asyncSupported = true)//urlPatterns过滤的哪些url
public class CorsFilter implements Filter {
@Resource
private RedisUtil redisUtil;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse)servletResponse;
HttpServletRequest request = (HttpServletRequest)servletRequest;
String path = ((HttpServletRequest) request).getRequestURI();
System.out.println("具体路径"+path);
if(path.startsWith("/patientController/patientLogin")||path.startsWith("/patientController/sendSmsCode")||path.startsWith("/patientController/addPatient")||path.startsWith("/patientController/patientForget")||path.startsWith("/doctroController/loginUser")||path.startsWith("/doctroController/queryLoginDoctor")||path.startsWith("/patientController/uploadFile")){
System.out.println("排除的url就是不过滤的url");
chain.doFilter(servletRequest, servletResponse);//// 排除的url
}else{
String isLogin=request.getParameter("isLogin");//传过来的登陆的标示
String username=request.getParameter("username");
if(("".equals(username)||username==null||"".equals(isLogin)||isLogin==null)){
response.setCharacterEncoding("utf-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter writer = response.getWriter();
JSONObject o = new JSONObject();
o.put("code", "04");
// o.put("data", "参数不能为空");
writer.write(o.toString());
}else{
// Object uisLogin= UtilsSession.getSssion(request,username);//根据用户名取出数据
Object uisLogin= redisUtil.get(username);//从redis中获取
System.out.println("==========="+uisLogin);
System.out.println("------------------"+username);
if(isLogin.equals(uisLogin)){
System.out.println("同一用户");
chain.doFilter(servletRequest, servletResponse);
}else{
System.out.println("被挤掉");
response.setCharacterEncoding("utf-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter writer = response.getWriter();
JSONObject o = new JSONObject();
o.put("code", "03");
// o.put("data","jiDiao" );
writer.write(o.toString());
}
}
}
}
@Override
public void destroy() {
}
}
-----------注意--------
除了上述配置之外还得在Application启动类中加入此注解
@ServletComponentScan(basePackages = {"com.medicine.management_side.filters"})//包名
public class ManagementSideApplication {
springBoot配置过滤器
最新推荐文章于 2023-10-07 16:56:41 发布