From 6b2f3024479140bb99a94128062c9ae541e99234 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期五, 09 八月 2024 16:38:15 +0800
Subject: [PATCH] 用户token过滤,增加不过滤的URl
---
pms-parent/pms-global/src/main/resources/application-global.yml | 8 +++-
pms-parent/pms-common/src/main/java/com/dy/common/webFilter/UserTokenFilter.java | 54 +++++++++++++++-----------
pms-parent/pms-common/src/main/java/com/dy/common/webFilter/UserTokenNoFilter.java | 27 +++++++++++++
3 files changed, 64 insertions(+), 25 deletions(-)
diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/webFilter/UserTokenFilter.java b/pms-parent/pms-common/src/main/java/com/dy/common/webFilter/UserTokenFilter.java
index bff17e8..a398ef6 100644
--- a/pms-parent/pms-common/src/main/java/com/dy/common/webFilter/UserTokenFilter.java
+++ b/pms-parent/pms-common/src/main/java/com/dy/common/webFilter/UserTokenFilter.java
@@ -25,34 +25,42 @@
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
- HttpServletRequest request = (HttpServletRequest) servletRequest;
- HttpServletResponse response = (HttpServletResponse) servletResponse;
- String token = request.getHeader(Constant.UserTokenKeyInHeader);
- if(!StringUtils.isNullOrEmpty(token)){
- UserTokenContext.set(token);
+ HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
+ String requestURI = httpRequest.getRequestURI();
+ // 妫�鏌ヨ姹傛槸鍚︿负闈炶繃婊ら」
+ if (UserTokenNoFilter.urls != null && UserTokenNoFilter.urls.stream().anyMatch(url -> requestURI.startsWith(url))) {
+ // 濡傛灉鏄潪杩囨护椤癸紝鐩存帴璋冪敤涓嬩竴涓繃婊ゅ櫒鎴杝ervlet
filterChain.doFilter(servletRequest, servletResponse);
- }else{
- 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();
+ } else {
+ // 濡傛灉涓嶆槸闈炶繃婊ら」锛屾墽琛岃繃婊ら�昏緫
+ HttpServletRequest request = (HttpServletRequest) servletRequest;
+ HttpServletResponse response = (HttpServletResponse) servletResponse;
+ String token = request.getHeader(Constant.UserTokenKeyInHeader);
+ if(!StringUtils.isNullOrEmpty(token)){
+ UserTokenContext.set(token);
+ filterChain.doFilter(servletRequest, servletResponse);
+ }else{
+ 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();
+ }
}
+ //request.setAttribute(Constant.ErrorKeyInRequest, "鏈緱鍒扮敤鎴穞oken");
+ //response.sendRedirect("/error");
}
- //request.setAttribute(Constant.ErrorKeyInRequest, "鏈緱鍒扮敤鎴穞oken");
- //response.sendRedirect("/error");
}
}
-
@Override
public void destroy() {
}
diff --git a/pms-parent/pms-common/src/main/java/com/dy/common/webFilter/UserTokenNoFilter.java b/pms-parent/pms-common/src/main/java/com/dy/common/webFilter/UserTokenNoFilter.java
new file mode 100644
index 0000000..cae7094
--- /dev/null
+++ b/pms-parent/pms-common/src/main/java/com/dy/common/webFilter/UserTokenNoFilter.java
@@ -0,0 +1,27 @@
+package com.dy.common.webFilter;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.List;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2024/7/1 14:01
+ * @Description
+ */
+@Configuration
+@ConfigurationProperties(prefix = "tokennofilter")
+public class UserTokenNoFilter {
+
+ public static List<String> urls ;
+
+ public List<String> getUrls(){
+ return urls ;
+ }
+
+ public void setUrls(List<String> urls){
+ UserTokenNoFilter.urls = urls ;
+ }
+
+}
diff --git a/pms-parent/pms-global/src/main/resources/application-global.yml b/pms-parent/pms-global/src/main/resources/application-global.yml
index 18aa106..f9f3ba4 100644
--- a/pms-parent/pms-global/src/main/resources/application-global.yml
+++ b/pms-parent/pms-global/src/main/resources/application-global.yml
@@ -43,16 +43,20 @@
cache-enabled: true #鎵撳紑鍏ㄥ眬缂撳瓨寮�鍏筹紙浜岀骇鐜锛夛紝榛樿鍊兼槸true
#default-enum-type-handler: com.dy.common.mybatis.envm.EnumCodeTypeHandler
+
+#涓嶈繘琛寀serToken杩囨护鐨刄RL锛孈ConfigurationProperties瑕佹眰tokennofilter涓墍鏈夊瓧姣嶉兘灏忓啓
+tokennofilter:
+ urls:
+ - /sso/sso
pms:
global:
dev: true #鏄惁寮�鍙戦樁娈碉紝true鎴杅alse锛屽紑鍙戦樁娈典笉杩涜鐧诲綍楠岃瘉鍙婃潈闄愰獙璇�
- ssoUserExpireAfterAccess: 60 #sso鐧诲綍鍚庯紝鐢ㄦ埛鍦ㄤ竴瀹氭椂闂达紙ssoUserExpireAfterAccess锛夛紙鍒嗛挓锛夊唴鏈闂郴缁燂紝绯荤粺娓呴櫎缂撳瓨淇℃伅锛屼娇鐢ㄩ噸鏂扮櫥褰曠郴缁�
+ ssoUserExpireAfterAccess: 60 #sso鐧诲綍鍚庯紝鐢ㄦ埛鍦ㄤ竴瀹氭椂闂达紙ssoUserExpireAfterAccess锛夛紙鍒嗛挓锛夊唴鏈闂郴缁燂紝绯荤粺娓呴櫎缂撳瓨淇℃伅锛屼娇鍏堕噸鏂扮櫥褰曠郴缁�
ssoCacheSizeInit: 10 #sso缂撳瓨鍒濆瀹归噺
ssoCacheSizeMax: 10000 #sso缂撳瓨鏈�澶у閲忥紝鍗虫敮鎸乻soCacheSizeMax涓敤鎴峰悓鏃剁櫥褰�
nginx:
webPort: 8000
sso:
-# checkUrl: http://127.0.0.1:12344/sso/sso/ssoCheck
checkUrl: http://127.0.0.1:12344/sso/sso/ssoPowerCheck
curUserUrl: http://127.0.0.1:12344/sso/sso/ssoCurUser
webPort: 12344
--
Gitblit v1.8.0