From 30f0bdc754cc1d85a72c5cb86b84ca20a67ef20c Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期六, 16 四月 2022 14:08:11 +0800 Subject: [PATCH] update 使用 spring cglib 替换 停止维护的 cglib --- ruoyi-framework/src/main/java/com/ruoyi/framework/config/SaTokenConfig.java | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 58 insertions(+), 1 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SaTokenConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SaTokenConfig.java index 9c9813f..671400f 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SaTokenConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SaTokenConfig.java @@ -1,12 +1,69 @@ package com.ruoyi.framework.config; +import cn.dev33.satoken.interceptor.SaAnnotationInterceptor; +import cn.dev33.satoken.interceptor.SaRouteInterceptor; import cn.dev33.satoken.jwt.StpLogicJwtForStyle; +import cn.dev33.satoken.router.SaRouter; import cn.dev33.satoken.stp.StpLogic; +import cn.dev33.satoken.stp.StpUtil; +import com.ruoyi.common.helper.LoginHelper; +import com.ruoyi.framework.config.properties.SecurityProperties; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * sa-token 閰嶇疆 + * + * @author Lion Li + */ +@RequiredArgsConstructor +@Slf4j @Configuration -public class SaTokenConfig { +public class SaTokenConfig implements WebMvcConfigurer { + + private final SecurityProperties securityProperties; + + /** + * 娉ㄥ唽sa-token鐨勬嫤鎴櫒 + */ + @Override + public void addInterceptors(InterceptorRegistry registry) { + // 娉ㄥ唽璺敱鎷︽埅鍣紝鑷畾涔夐獙璇佽鍒� + registry.addInterceptor(new SaRouteInterceptor((request, response, handler) -> { + // 鐧诲綍楠岃瘉 -- 鎺掗櫎澶氫釜璺緞 + SaRouter + // 鑾峰彇鎵�鏈夌殑 + .match("/**") + // 鎺掗櫎涓嬩笉闇�瑕佹嫤鎴殑 + .notMatch(securityProperties.getExcludes()) + // 瀵规湭鎺掗櫎鐨勮矾寰勮繘琛屾鏌� + .check(() -> { + // 妫�鏌ユ槸鍚︾櫥褰� 鏄惁鏈塼oken + StpUtil.checkLogin(); + + // 鏈夋晥鐜囧奖鍝� 鐢ㄤ簬涓存椂娴嬭瘯 + // if (log.isDebugEnabled()) { + // log.debug("鍓╀綑鏈夋晥鏃堕棿: {}", StpUtil.getTokenTimeout()); + // log.debug("涓存椂鏈夋晥鏃堕棿: {}", StpUtil.getTokenActivityTimeout()); + // } + + }); + }) { + @SuppressWarnings("all") + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + LoginHelper.clearCache(); + } + }).addPathPatterns("/**"); + registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**"); + } @Bean public StpLogic getStpLogicJwt() { -- Gitblit v1.9.3