From 7d81ce01560d384f15212edc40ebeaa9924913f9 Mon Sep 17 00:00:00 2001 From: wlzboy <66905212@qq.com> Date: 星期日, 21 九月 2025 19:12:53 +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