mirror of https://github.com/elunez/eladmin
88 lines
3.6 KiB
Java
88 lines
3.6 KiB
Java
/*
|
|
* Copyright 2019-2020 Zheng Jie
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
package me.zhengjie.config;
|
|
|
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
import com.alibaba.fastjson.support.config.FastJsonConfig;
|
|
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
import org.springframework.web.cors.CorsConfiguration;
|
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
import org.springframework.web.filter.CorsFilter;
|
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* WebMvcConfigurer
|
|
*
|
|
* @author Zheng Jie
|
|
* @date 2018-11-30
|
|
*/
|
|
@Configuration
|
|
@EnableWebMvc
|
|
public class ConfigurerAdapter implements WebMvcConfigurer {
|
|
|
|
/** 文件配置 */
|
|
private final FileProperties properties;
|
|
|
|
public ConfigurerAdapter(FileProperties properties) {
|
|
this.properties = properties;
|
|
}
|
|
|
|
@Bean
|
|
public CorsFilter corsFilter() {
|
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
|
CorsConfiguration config = new CorsConfiguration();
|
|
config.setAllowCredentials(true);
|
|
config.addAllowedOrigin("*");
|
|
config.addAllowedHeader("*");
|
|
config.addAllowedMethod("*");
|
|
source.registerCorsConfiguration("/**", config);
|
|
return new CorsFilter(source);
|
|
}
|
|
|
|
@Override
|
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
FileProperties.ElPath path = properties.getPath();
|
|
String avatarUtl = "file:" + path.getAvatar().replace("\\","/");
|
|
String pathUtl = "file:" + path.getPath().replace("\\","/");
|
|
registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0);
|
|
registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
|
|
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
|
|
}
|
|
|
|
@Override
|
|
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
|
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
|
|
List<MediaType> supportMediaTypeList = new ArrayList<>();
|
|
supportMediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
|
|
FastJsonConfig config = new FastJsonConfig();
|
|
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
|
|
converter.setFastJsonConfig(config);
|
|
converter.setSupportedMediaTypes(supportMediaTypeList);
|
|
converter.setDefaultCharset(StandardCharsets.UTF_8);
|
|
converters.add(converter);
|
|
}
|
|
}
|