Skip to content

Commit 33b5bfb

Browse files
🐯 优化cors升级hutool 4.6.10
1 parent 876162e commit 33b5bfb

File tree

6 files changed

+78
-32
lines changed

6 files changed

+78
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
### 🔨 Dependency Upgrades
3333
- Upgrade to `spring-boot` 2.1.9.RELEASE
3434
- Upgrade to `Fastjson` 1.2.62
35+
- Upgrade to `hutool` 4.6.10
3536
- Add `commons-text` 1.8
3637

3738
## [V1.3.0-RELEASE] 2019.10.06

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<reflections.version>0.9.11</reflections.version>
6565
<jansi.version>1.18</jansi.version>
6666
<lombok.version>1.18.8</lombok.version>
67-
<hutool.version>4.6.4</hutool.version>
67+
<hutool.version>4.6.10</hutool.version>
6868
<junit.version>4.12</junit.version>
6969
<ini4j.version>0.5.4</ini4j.version>
7070
<mapstruct.version>1.3.0.Final</mapstruct.version>

src/main/java/io/geekidea/springbootplus/core/config/SpringBootPlusConfig.java

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,15 @@
1616
package io.geekidea.springbootplus.core.config;
1717

1818
import io.geekidea.springbootplus.aop.LogAop;
19-
import io.geekidea.springbootplus.interceptor.PermissionInterceptor;
2019
import io.geekidea.springbootplus.core.properties.*;
20+
import io.geekidea.springbootplus.interceptor.PermissionInterceptor;
2121
import io.geekidea.springbootplus.resource.interceptor.DownloadInterceptor;
2222
import io.geekidea.springbootplus.resource.interceptor.ResourceInterceptor;
2323
import io.geekidea.springbootplus.resource.interceptor.UploadInterceptor;
2424
import lombok.extern.slf4j.Slf4j;
2525
import org.springframework.boot.context.properties.EnableConfigurationProperties;
26-
import org.springframework.boot.web.servlet.FilterRegistrationBean;
2726
import org.springframework.context.annotation.Bean;
2827
import org.springframework.context.annotation.Configuration;
29-
import org.springframework.core.Ordered;
30-
import org.springframework.web.cors.CorsConfiguration;
31-
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
32-
import org.springframework.web.filter.CorsFilter;
3328

3429
/**
3530
* spring-boot-plus配置
@@ -49,29 +44,6 @@
4944
})
5045
public class SpringBootPlusConfig {
5146

52-
/**
53-
* CORS跨域设置
54-
*
55-
* @return
56-
*/
57-
@Bean
58-
public FilterRegistrationBean corsFilter(SpringBootPlusCorsProperties corsProperties) {
59-
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
60-
CorsConfiguration corsConfiguration = new CorsConfiguration();
61-
// 跨域配置
62-
corsConfiguration.setAllowedOrigins(corsProperties.getAllowedOrigins());
63-
corsConfiguration.setAllowedHeaders(corsProperties.getAllowedHeaders());
64-
corsConfiguration.setAllowedMethods(corsProperties.getAllowedMethods());
65-
corsConfiguration.setAllowCredentials(corsProperties.isAllowCredentials());
66-
corsConfiguration.setExposedHeaders(corsProperties.getExposedHeaders());
67-
source.registerCorsConfiguration(corsProperties.getPath(), corsConfiguration);
68-
69-
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
70-
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
71-
bean.setEnabled(corsProperties.isEnable());
72-
return bean;
73-
}
74-
7547
/**
7648
* 配置日志AOP
7749
*
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2019-2029 geekidea(https://github.com/geekidea)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.geekidea.springbootplus.core.config;
18+
19+
import io.geekidea.springbootplus.core.properties.SpringBootPlusCorsProperties;
20+
import lombok.extern.slf4j.Slf4j;
21+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.core.Ordered;
25+
import org.springframework.web.cors.CorsConfiguration;
26+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
27+
import org.springframework.web.filter.CorsFilter;
28+
29+
/**
30+
* 跨域配置
31+
*
32+
* @author geekidea
33+
* @date 2019/10/14
34+
* @since 1.3.1.RELEASE
35+
*/
36+
@Slf4j
37+
@Configuration
38+
public class SpringBootPlusCorsConfig {
39+
40+
/**
41+
* CORS跨域设置
42+
*
43+
* @return
44+
*/
45+
@Bean
46+
public FilterRegistrationBean corsFilter(SpringBootPlusCorsProperties corsProperties) {
47+
log.debug("corsProperties:{}", corsProperties);
48+
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
49+
CorsConfiguration corsConfiguration = new CorsConfiguration();
50+
// 跨域配置
51+
corsConfiguration.setAllowedOrigins(corsProperties.getAllowedOrigins());
52+
corsConfiguration.setAllowedHeaders(corsProperties.getAllowedHeaders());
53+
corsConfiguration.setAllowedMethods(corsProperties.getAllowedMethods());
54+
corsConfiguration.setAllowCredentials(corsProperties.isAllowCredentials());
55+
corsConfiguration.setExposedHeaders(corsProperties.getExposedHeaders());
56+
corsConfiguration.setMaxAge(corsConfiguration.getMaxAge());
57+
58+
source.registerCorsConfiguration(corsProperties.getPath(), corsConfiguration);
59+
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
60+
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
61+
bean.setEnabled(corsProperties.isEnable());
62+
return bean;
63+
}
64+
65+
}

src/main/java/io/geekidea/springbootplus/core/properties/SpringBootPlusCorsProperties.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public class SpringBootPlusCorsProperties {
6767
/**
6868
* 允许响应的头
6969
*/
70-
private List<String> exposedHeaders = Arrays.asList("content-type", "token");
70+
private List<String> exposedHeaders = Arrays.asList("token");
71+
72+
/**
73+
* 该响应的有效时间默认为30分钟,在有效时间内,浏览器无须为同一请求再次发起预检请求
74+
*/
75+
private Long maxAge = 1800L;
7176

7277
}

src/main/resources/config/application.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ spring-boot-plus:
100100
# 允许访问的请求方式
101101
allowed-methods: OPTION,GET,POST
102102
# 允许响应的头
103-
exposed-headers: content-type,token
103+
exposed-headers: token
104+
# 该响应的有效时间默认为30分钟,在有效时间内,浏览器无须为同一请求再次发起预检请求
105+
max-age: 1800
106+
104107
############################ CORS end ##############################
105108

106109
########################## Resource start ##########################

0 commit comments

Comments
 (0)