刘小明
2025-01-16 57391775d5f5ae8a93b308d0b261b2244eb7579a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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 WechatModuleConfig {
 
        @Bean
        public FilterRegistrationBean<LoginCheckFilter> wechatFilter() {
            FilterRegistrationBean<LoginCheckFilter> 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);
        }
    }
 
}