Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
chnmuseum-party
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liqin
chnmuseum-party
Commits
634e3459
Commit
634e3459
authored
Mar 11, 2021
by
liqin
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug fixed
parent
b158ab0e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
161 additions
and
49 deletions
+161
-49
GlobalCorsConfig.java
...a/cn/wisenergy/chnmuseum/party/conf/GlobalCorsConfig.java
+49
-49
MvcConfiguration.java
...a/cn/wisenergy/chnmuseum/party/conf/MvcConfiguration.java
+112
-0
No files found.
src/main/java/cn/wisenergy/chnmuseum/party/conf/GlobalCorsConfig.java
View file @
634e3459
package
cn
.
wisenergy
.
chnmuseum
.
party
.
conf
;
//
package cn.wisenergy.chnmuseum.party.conf;
//
import
org.apache.commons.lang3.StringUtils
;
//
import org.apache.commons.lang3.StringUtils;
import
org.springframework.beans.factory.annotation.Value
;
//
import org.springframework.beans.factory.annotation.Value;
import
org.springframework.context.annotation.Bean
;
//
import org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration
;
//
import org.springframework.context.annotation.Configuration;
import
org.springframework.web.cors.CorsConfiguration
;
//
import org.springframework.web.cors.CorsConfiguration;
import
org.springframework.web.cors.UrlBasedCorsConfigurationSource
;
//
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import
org.springframework.web.filter.CorsFilter
;
//
import org.springframework.web.filter.CorsFilter;
//
import
java.util.Arrays
;
//
import java.util.Arrays;
//
@Configuration
//
@Configuration
public
class
GlobalCorsConfig
{
//
public class GlobalCorsConfig {
//
@Value
(
"${cors.url.list:#{null}}"
)
//
@Value("${cors.url.list:#{null}}")
private
String
corsUrlList
;
//
private String corsUrlList;
//
@Bean
//
@Bean
public
CorsFilter
corsFilter
()
{
//
public CorsFilter corsFilter() {
//1.添加CORS配置信息
//
//1.添加CORS配置信息
CorsConfiguration
config
=
new
CorsConfiguration
();
//
CorsConfiguration config = new CorsConfiguration();
//1) 允许通过的域,不要写*,否则cookie就无法使用了
//
//1) 允许通过的域,不要写*,否则cookie就无法使用了
if
(
corsUrlList
!=
null
&&
corsUrlList
.
length
()
>
1
)
{
//
if (corsUrlList != null && corsUrlList.length() > 1) {
config
.
setAllowedOrigins
(
Arrays
.
asList
(
StringUtils
.
split
(
corsUrlList
,
","
)));
//
config.setAllowedOrigins(Arrays.asList(StringUtils.split(corsUrlList, ",")));
}
else
{
//
} else {
config
.
addAllowedOrigin
(
"*"
);
//
config.addAllowedOrigin("*");
}
//
}
//
//2) 是否发送Cookie信息
//
//2) 是否发送Cookie信息
//
config
.
setAllowCredentials
(
true
);
//
config.setAllowCredentials(true);
//3) 允许的请求方式
//
//3) 允许的请求方式
config
.
addAllowedMethod
(
"OPTIONS"
);
//
config.addAllowedMethod("OPTIONS");
config
.
addAllowedMethod
(
"GET"
);
//
config.addAllowedMethod("GET");
config
.
addAllowedMethod
(
"POST"
);
//
config.addAllowedMethod("POST");
config
.
addAllowedMethod
(
"PUT"
);
//
config.addAllowedMethod("PUT");
config
.
addAllowedMethod
(
"DELETE"
);
//
config.addAllowedMethod("DELETE");
//
// 4)允许的头信息
//
// 4)允许的头信息
config
.
addAllowedHeader
(
"*"
);
//
config.addAllowedHeader("*");
//2.添加映射路径,我们拦截一切请求
//
//2.添加映射路径,我们拦截一切请求
UrlBasedCorsConfigurationSource
configSource
=
new
UrlBasedCorsConfigurationSource
();
//
UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource
.
registerCorsConfiguration
(
"/**"
,
config
);
//
configSource.registerCorsConfiguration("/**", config);
//3.返回新的CorsFilter.
//
//3.返回新的CorsFilter.
return
new
CorsFilter
(
configSource
);
//
return new CorsFilter(configSource);
}
//
}
//
}
//
}
src/main/java/cn/wisenergy/chnmuseum/party/conf/MvcConfiguration.java
0 → 100644
View file @
634e3459
package
cn
.
wisenergy
.
chnmuseum
.
party
.
conf
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
;
/**
* MVC配置
*/
@Configuration
public
class
MvcConfiguration
extends
WebMvcConfigurationSupport
{
/**
* 后置跨域支持【当出现跨域请求,此处会放在拦截器最后执行,CORS失效】
*
* @param registry
*/
@Override
public
void
addCorsMappings
(
CorsRegistry
registry
)
{
registry
.
addMapping
(
"/**"
)
.
allowedOriginPatterns
(
"*"
)
.
allowedHeaders
(
"*"
)
.
allowedMethods
(
"*"
)
.
allowCredentials
(
true
)
.
maxAge
(
3600
);
}
/**
* 配置消息转换器:Ali开源的fastJson
*
* @param converters
*/
// @Override
// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// // 先移除jackson转换器,springBoot1.x可以不排除
// for (int i = converters.size() - 1; i >= 0; i--) {
// if (converters.get(i) instanceof MappingJackson2HttpMessageConverter) {
// converters.remove(i);
// }
// }
// //1.需要定义一个convert转换消息的对象;
// FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
// StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
// ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
//
// //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据;
// FastJsonConfig fastJsonConfig = new FastJsonConfig();
// fastJsonConfig.setSerializerFeatures(
// SerializerFeature.PrettyFormat,
// SerializerFeature.WriteMapNullValue,
// SerializerFeature.WriteNullStringAsEmpty,
// SerializerFeature.DisableCircularReferenceDetect,
// SerializerFeature.WriteNullListAsEmpty,
// SerializerFeature.BrowserCompatible,
// SerializerFeature.WriteDateUseDateFormat);
// // 设置编码
// fastJsonConfig.setCharset(Charset.forName("UTF-8"));
// fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
//
// // 设置数字转化问题
// SerializeConfig serializeConfig = SerializeConfig.globalInstance;
// serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
// serializeConfig.put(Long.class, ToStringSerializer.instance);
// serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
// serializeConfig.setPropertyNamingStrategy( PropertyNamingStrategy.CamelCase);
// fastJsonConfig.setSerializeConfig(serializeConfig);
//
// //3处理中文乱码问题
// List<MediaType> fastMediaTypes = new ArrayList<>();
// fastMediaTypes.add(MediaType.APPLICATION_JSON);
// fastMediaTypes.add(MediaType.TEXT_HTML);
// fastMediaTypes.add(MediaType.MULTIPART_FORM_DATA);
//
// //4.在convert中添加配置信息.
// fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
// fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
//
// //5.将convert添加到converters当中.
// converters.add(fastJsonHttpMessageConverter);
// converters.add(stringHttpMessageConverter);
// converters.add(byteArrayHttpMessageConverter);
// }
/**
* 启用@EnableWebMvc后,properties文件中的静态路径失效,必须覆盖后重新制定
* 配置静态访问资源
*
* @param registry
*/
@Override
public
void
addResourceHandlers
(
ResourceHandlerRegistry
registry
)
{
// swagger2配置
registry
.
addResourceHandler
(
"/swagger-ui.html"
)
.
addResourceLocations
(
"classpath:/META-INF/resources/"
);
registry
.
addResourceHandler
(
"/webjars/**"
)
.
addResourceLocations
(
"classpath:/META-INF/resources/webjars/"
);
// 静态资源拦截
registry
.
addResourceHandler
(
"/**"
)
.
addResourceLocations
(
"classpath:/META-INF/"
)
.
addResourceLocations
(
"classpath:/META-INF/resources/"
)
.
addResourceLocations
(
"classpath:/resources/"
)
.
addResourceLocations
(
"classpath:/static/"
)
.
addResourceLocations
(
"classpath:/public/"
)
.
addResourceLocations
(
"classpath:/statics/"
)
.
addResourceLocations
(
"classpath:/template/"
)
.
addResourceLocations
(
"classpath:/"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment