From c62ec58c827e780c4fb039f03149884c10ba0c52 Mon Sep 17 00:00:00 2001
From: Fancy <Fancy.fx@outlook.com>
Date: 星期五, 27 十二月 2024 11:16:02 +0800
Subject: [PATCH] parse JWT

---
 pms-parent/pms-web-wechat/src/main/java/com/dy/pmsWechat/PmsWechatApplication.java |   92 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 91 insertions(+), 1 deletions(-)

diff --git a/pms-parent/pms-web-wechat/src/main/java/com/dy/pmsWechat/PmsWechatApplication.java b/pms-parent/pms-web-wechat/src/main/java/com/dy/pmsWechat/PmsWechatApplication.java
index fb472bb..3295c10 100644
--- a/pms-parent/pms-web-wechat/src/main/java/com/dy/pmsWechat/PmsWechatApplication.java
+++ b/pms-parent/pms-web-wechat/src/main/java/com/dy/pmsWechat/PmsWechatApplication.java
@@ -1,19 +1,109 @@
 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 MyModuleConfig {
+
+        @Bean
+        public FilterRegistrationBean<LoginCheckFilter> myCustomFilter() {
+            FilterRegistrationBean<LoginCheckFilter> registrationBean = new FilterRegistrationBean<>();
+            registrationBean.setFilter(new LoginCheckFilter());
+            registrationBean.addUrlPatterns("/myModule/*");
+            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; // 鏂规硶缁撴潫锛屽悗缁�昏緫涓嶅啀鎵ц
+            }
+
+            // 瑙f瀽浠ょ墝
+            try {
+                JwtUtil.parseJWT(secretKey,jwt); // 灏濊瘯瑙f瀽浠ょ墝锛岄獙璇佸叾鍚堟硶鎬�
+            } 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);
+        }
+    }
+
 }

--
Gitblit v1.8.0