From 89de1beb6fe245eb0c9f96e68cbe5cd46877e7d1 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期三, 22 十二月 2021 10:19:57 +0800
Subject: [PATCH] update 优化web拦截器 使用原生接口处理 默认非生产环境开启

---
 ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java |   85 ++++++++++++++++--------------------------
 1 files changed, 33 insertions(+), 52 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 4822c91..450eccd 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
@@ -1,9 +1,9 @@
 package com.ruoyi.framework.config;
 
+import com.ruoyi.framework.config.properties.SecurityProperties;
 import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter;
 import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
 import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
-import de.codecentric.boot.admin.server.config.AdminServerProperties;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Bean;
 import org.springframework.http.HttpMethod;
@@ -21,18 +21,17 @@
 
 /**
  * spring security閰嶇疆
- * 
+ *
  * @author ruoyi
  */
 @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
-public class SecurityConfig extends WebSecurityConfigurerAdapter
-{
+public class SecurityConfig extends WebSecurityConfigurerAdapter {
     /**
      * 鑷畾涔夌敤鎴疯璇侀�昏緫
      */
     @Autowired
     private UserDetailsService userDetailsService;
-    
+
     /**
      * 璁よ瘉澶辫触澶勭悊绫�
      */
@@ -58,8 +57,8 @@
     private CorsFilter corsFilter;
 
     @Autowired
-    private AdminServerProperties adminServerProperties;
-    
+    private SecurityProperties securityProperties;
+
     /**
      * 瑙e喅 鏃犳硶鐩存帴娉ㄥ叆 AuthenticationManager
      *
@@ -68,8 +67,7 @@
      */
     @Bean
     @Override
-    public AuthenticationManager authenticationManagerBean() throws Exception
-    {
+    public AuthenticationManager authenticationManagerBean() throws Exception {
         return super.authenticationManagerBean();
     }
 
@@ -89,45 +87,31 @@
      * authenticated       |   鐢ㄦ埛鐧诲綍鍚庡彲璁块棶
      */
     @Override
-    protected void configure(HttpSecurity httpSecurity) throws Exception
-    {
+    protected void configure(HttpSecurity httpSecurity) throws Exception {
         httpSecurity
-                // CSRF绂佺敤锛屽洜涓轰笉浣跨敤session
-                .csrf().disable()
-                // 璁よ瘉澶辫触澶勭悊绫�
-                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
-                // 鍩轰簬token锛屾墍浠ヤ笉闇�瑕乻ession
-                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
-                // 杩囨护璇锋眰
-                .authorizeRequests()
-                // 瀵逛簬鐧诲綍login 楠岃瘉鐮乧aptchaImage 鍏佽鍖垮悕璁块棶
-                .antMatchers("/login", "/captchaImage").anonymous()
-                .antMatchers(
-                        HttpMethod.GET,
-                        "/*.html",
-                        "/**/*.html",
-                        "/**/*.css",
-                        "/**/*.js"
-                ).permitAll()
-                .antMatchers("/profile/**").anonymous()
-                .antMatchers("/common/download**").anonymous()
-                .antMatchers("/common/download/resource**").anonymous()
-                .antMatchers("/swagger-ui.html").anonymous()
-                .antMatchers("/swagger-resources/**").anonymous()
-                .antMatchers("/webjars/**").anonymous()
-                .antMatchers("/*/api-docs").anonymous()
-                .antMatchers("/druid/**").anonymous()
-                // Spring Boot Admin Server 鐨勫畨鍏ㄩ厤缃�
-                .antMatchers(adminServerProperties.getContextPath()).anonymous()
-                .antMatchers(adminServerProperties.getContextPath() + "/**").anonymous()
-                // Spring Boot Actuator 鐨勫畨鍏ㄩ厤缃�
-                .antMatchers("/actuator").anonymous()
-                .antMatchers("/actuator/**").anonymous()
-                // 闄や笂闈㈠鐨勬墍鏈夎姹傚叏閮ㄩ渶瑕侀壌鏉冭璇�
-                .anyRequest().authenticated()
-                .and()
-                .headers().frameOptions().disable();
-        httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
+            // CSRF绂佺敤锛屽洜涓轰笉浣跨敤session
+            .csrf().disable()
+            // 璁よ瘉澶辫触澶勭悊绫�
+            .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
+            // 鍩轰簬token锛屾墍浠ヤ笉闇�瑕乻ession
+            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
+            // 杩囨护璇锋眰
+            .authorizeRequests()
+            .antMatchers(
+                HttpMethod.GET,
+                "/",
+                "/*.html",
+                "/**/*.html",
+                "/**/*.css",
+                "/**/*.js"
+            ).permitAll()
+            .antMatchers(securityProperties.getAnonymous()).anonymous()
+            .antMatchers(securityProperties.getPermitAll()).permitAll()
+            // 闄や笂闈㈠鐨勬墍鏈夎姹傚叏閮ㄩ渶瑕侀壌鏉冭璇�
+            .anyRequest().authenticated()
+            .and()
+            .headers().frameOptions().disable();
+        httpSecurity.logout().logoutUrl(securityProperties.getLogoutUrl()).logoutSuccessHandler(logoutSuccessHandler);
         // 娣诲姞JWT filter
         httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
         // 娣诲姞CORS filter
@@ -135,13 +119,11 @@
         httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);
     }
 
-    
     /**
      * 寮烘暎鍒楀搱甯屽姞瀵嗗疄鐜�
      */
     @Bean
-    public BCryptPasswordEncoder bCryptPasswordEncoder()
-    {
+    public BCryptPasswordEncoder bCryptPasswordEncoder() {
         return new BCryptPasswordEncoder();
     }
 
@@ -149,8 +131,7 @@
      * 韬唤璁よ瘉鎺ュ彛
      */
     @Override
-    protected void configure(AuthenticationManagerBuilder auth) throws Exception
-    {
+    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
         auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
     }
 }

--
Gitblit v1.9.3