package com.dy.pmsWechat; import com.alibaba.fastjson2.JSON; import com.dy.common.webUtil.BaseResponse; import com.dy.common.webUtil.BaseResponseUtils; import com.dy.pmsWechat.util.JwtUtil; import jakarta.servlet.*; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; import java.io.IOException; import java.io.PrintWriter; @SpringBootApplication @EnableAspectJAutoProxy @ComponentScan(basePackages = {"com.dy.common", "com.dy.pmsGlobal", "com.dy.pmsWechat"}) @MapperScan(basePackages={"com.dy.pmsGlobal.dao*"}) public class PmsWechatApplication { public static void main(String[] args) { SpringApplication.run(PmsWechatApplication.class, args); } @Configuration public static class WechatModuleConfig { @Bean public FilterRegistrationBean wechatFilter() { FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new LoginCheckFilter()); registrationBean.addUrlPatterns("/wechat/*"); registrationBean.setOrder(1); return registrationBean; } } @Slf4j public static class LoginCheckFilter implements Filter { @Value("${wechat.jwt.secret-key}") private String secretKey; @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; String url = req.getRequestURI().toString(); // 在这里编写过滤器逻辑 if (url.contains("login")) { chain.doFilter(request, response); // "登录操作,放行" 放行请求,继续执行后续过滤器链或目标资源 return; // 方法结束,后续逻辑不再执行 } // 获取请求头中的 "token" 值 String jwt = req.getHeader("token"); // 如果请求头中没有 "token",返回未登录错误信息 if (jwt == null || jwt.trim().isEmpty()) { // 使用 Spring 的 StringUtils 检查 jwt 是否为空 PrintWriter pw = null ; try { BaseResponse res = BaseResponseUtils.buildToLogin(); String jsonString = JSON.toJSONString(res); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); pw = response.getWriter() ; pw.write(jsonString); pw.flush(); } catch (IOException e) { e.printStackTrace(); }finally { if(pw != null){ pw.close(); } } return; // 方法结束,后续逻辑不再执行 } // 解析令牌 try { JwtUtil.parseJWT(secretKey,jwt); // 尝试解析令牌,验证其合法性 } catch (Exception e) { PrintWriter pw = null ; try { BaseResponse res = BaseResponseUtils.buildToLogin(); String jsonString = JSON.toJSONString(res); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); pw = response.getWriter() ; pw.write(jsonString); pw.flush(); } catch (IOException ex) { ex.printStackTrace(); }finally { if(pw != null){ pw.close(); } } return; // 方法结束,后续逻辑不再执行 } chain.doFilter(request, response); } } }