From f5a9ea1e36db1cccdf744177f4af959159b2addf Mon Sep 17 00:00:00 2001
From: wzp <2040239371@qq.com>
Date: 星期四, 01 五月 2025 19:45:19 +0800
Subject: [PATCH] feat: 新增内部接口调用

---
 ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java |   54 +++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
index 511842b..ed6678c 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java
@@ -20,6 +20,14 @@
 import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter;
 import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
 import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
+import com.ruoyi.common.annotation.Anonymous;
+import org.springframework.security.web.util.matcher.RequestMatcher;
+import org.springframework.web.method.HandlerMethod;
+import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
+import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * spring security閰嶇疆
@@ -66,6 +74,26 @@
     @Autowired
     private PermitAllUrlProperties permitAllUrl;
 
+    @Autowired
+    private RequestMappingHandlerMapping requestMappingHandlerMapping;
+
+    /**
+     * 鑾峰彇鎵�鏈夋爣娉ㄤ簡@Anonymous鐨刄RL
+     */
+    private Set<String> getAnonymousUrls() {
+        Set<String> urls = new HashSet<>();
+        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
+        for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
+            HandlerMethod handlerMethod = entry.getValue();
+            Anonymous anonymous = handlerMethod.getMethodAnnotation(Anonymous.class);
+            if (anonymous != null) {
+                Set<String> patterns = entry.getKey().getPatternValues();
+                urls.addAll(patterns);
+            }
+        }
+        return urls;
+    }
+
     /**
      * 韬唤楠岃瘉瀹炵幇
      */
@@ -96,6 +124,9 @@
     @Bean
     protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception
     {
+        // 鑾峰彇鎵�鏈夋爣娉ㄤ簡@Anonymous鐨刄RL
+        Set<String> anonymousUrls = getAnonymousUrls();
+        
         return httpSecurity
             // CSRF绂佺敤锛屽洜涓轰笉浣跨敤session
             .csrf(csrf -> csrf.disable())
@@ -107,17 +138,18 @@
             .exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedHandler))
             // 鍩轰簬token锛屾墍浠ヤ笉闇�瑕乻ession
             .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
-            // 娉ㄨВ鏍囪鍏佽鍖垮悕璁块棶鐨剈rl
-            .authorizeHttpRequests((requests) -> {
-                permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
-                // 瀵逛簬鐧诲綍login 娉ㄥ唽register 楠岃瘉鐮乧aptchaImage 鍏佽鍖垮悕璁块棶
-                requests.antMatchers("/login", "/register", "/captchaImage").permitAll()
-                    // 闈欐�佽祫婧愶紝鍙尶鍚嶈闂�
-                    .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
-                    .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
-                    // 闄や笂闈㈠鐨勬墍鏈夎姹傚叏閮ㄩ渶瑕侀壌鏉冭璇�
-                    .anyRequest().authenticated();
-            })
+            // 杩囨护璇锋眰
+            .authorizeRequests()
+            // 瀵逛簬鐧诲綍login 娉ㄥ唽register 楠岃瘉鐮乧aptchaImage 鍏佽鍖垮悕璁块棶
+            .antMatchers("/login", "/register", "/captchaImage").permitAll()
+            // 娣诲姞鏍囨敞浜咢Anonymous鐨刄RL鍒板尶鍚嶈闂垪琛�
+            .antMatchers(anonymousUrls.toArray(new String[0])).permitAll()
+            // 闈欐�佽祫婧愶紝鍙尶鍚嶈闂�
+            .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
+            .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
+            // 闄や笂闈㈠鐨勬墍鏈夎姹傚叏閮ㄩ渶瑕侀壌鏉冭璇�
+            .anyRequest().authenticated()
+            .and()
             // 娣诲姞Logout filter
             .logout(logout -> logout.logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler))
             // 娣诲姞JWT filter

--
Gitblit v1.9.1