mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 22:57:17 +08:00 
			
		
		
		
	优化:优化跨域配置,可支持配置允许跨域的具体域名或 *
This commit is contained in:
		| @@ -36,6 +36,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | |||||||
|  |  | ||||||
| import top.charles7c.cnadmin.common.config.properties.CorsProperties; | import top.charles7c.cnadmin.common.config.properties.CorsProperties; | ||||||
| import top.charles7c.cnadmin.common.config.properties.LocalStorageProperties; | import top.charles7c.cnadmin.common.config.properties.LocalStorageProperties; | ||||||
|  | import top.charles7c.cnadmin.common.constant.StringConsts; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Web MVC 配置 |  * Web MVC 配置 | ||||||
| @@ -75,13 +76,17 @@ public class WebMvcConfiguration implements WebMvcConfigurer { | |||||||
|     @Bean |     @Bean | ||||||
|     public CorsFilter corsFilter() { |     public CorsFilter corsFilter() { | ||||||
|         CorsConfiguration config = new CorsConfiguration(); |         CorsConfiguration config = new CorsConfiguration(); | ||||||
|         // 配置为 true 后则必须配置允许跨域的域名,且不允许配置为 * |  | ||||||
|         config.setAllowCredentials(true); |  | ||||||
|         // 设置跨域允许时间 |         // 设置跨域允许时间 | ||||||
|         config.setMaxAge(1800L); |         config.setMaxAge(1800L); | ||||||
|  |  | ||||||
|         // 配置允许跨域的域名 |         // 配置允许跨域的域名 | ||||||
|         corsProperties.getAllowedOrigins().forEach(config::addAllowedOrigin); |         if (corsProperties.getAllowedOrigins().contains(StringConsts.ASTERISK)) { | ||||||
|  |             config.addAllowedOriginPattern(StringConsts.ASTERISK); | ||||||
|  |         } else { | ||||||
|  |             // 配置为 true 后则必须配置允许跨域的域名,且不允许配置为 * | ||||||
|  |             config.setAllowCredentials(true); | ||||||
|  |             corsProperties.getAllowedOrigins().forEach(config::addAllowedOrigin); | ||||||
|  |         } | ||||||
|         // 配置允许跨域的请求方式 |         // 配置允许跨域的请求方式 | ||||||
|         corsProperties.getAllowedMethods().forEach(config::addAllowedMethod); |         corsProperties.getAllowedMethods().forEach(config::addAllowedMethod); | ||||||
|         // 配置允许跨域的请求头 |         // 配置允许跨域的请求头 | ||||||
|   | |||||||
| @@ -40,6 +40,11 @@ public class StringConsts implements StrPool { | |||||||
|      */ |      */ | ||||||
|     public static final String SEMICOLON = ";"; |     public static final String SEMICOLON = ";"; | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 星号 | ||||||
|  |      */ | ||||||
|  |     public static final String ASTERISK = "*"; | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 中文逗号 |      * 中文逗号 | ||||||
|      */ |      */ | ||||||
|   | |||||||
| @@ -176,11 +176,7 @@ local-storage: | |||||||
| --- ### 跨域配置 | --- ### 跨域配置 | ||||||
| cors: | cors: | ||||||
|   # 配置允许跨域的域名 |   # 配置允许跨域的域名 | ||||||
|   allowedOrigins: |   allowedOrigins: '*' | ||||||
|     - http://127.0.0.1:5173 |  | ||||||
|     - http://localhost:5173 |  | ||||||
|     - http://cnadmin.charles7c.top |  | ||||||
|     - https://cnadmin.charles7c.top |  | ||||||
|   # 配置允许跨域的请求方式 |   # 配置允许跨域的请求方式 | ||||||
|   allowedMethods: '*' |   allowedMethods: '*' | ||||||
|   # 配置允许跨域的请求头 |   # 配置允许跨域的请求头 | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user