1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package com.api.base.config;
- import com.api.common.config.UploadConfig;
- import com.api.core.annotation.condition.HttpsCondition;
- import org.apache.catalina.Context;
- import org.apache.tomcat.util.descriptor.web.SecurityCollection;
- import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
- import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Conditional;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.http.converter.HttpMessageConverter;
- import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
- import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
- import org.springframework.web.servlet.config.annotation.CorsRegistry;
- import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
- import javax.annotation.Resource;
- import java.util.List;
- /**
- * Spring MVC 配置
- */
- @Configuration
- public class MvcConfigurer implements WebMvcConfigurer {
- private final Logger logger = LoggerFactory.getLogger(MvcConfigurer.class);
- @Resource
- private UploadConfig uploadConfig;
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/"+uploadConfig.getPrefix()+"**")
- .addResourceLocations("file:" + uploadConfig.getFilePath());
- 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/resources/")
- .addResourceLocations("classpath:/resources/")
- .addResourceLocations("classpath:/static/");
- WebMvcConfigurer.super.addResourceHandlers(registry);
- }
- //解决跨域问题
- @Override
- public void addCorsMappings(CorsRegistry registry) {
- registry.addMapping("/**")
- .allowedHeaders("*")
- .allowedMethods("*")
- .allowedOrigins("*");
- }
- @Override
- public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
- Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
- builder.indentOutput(true);
- converters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));
- }
- @Conditional(HttpsCondition.class)
- @Bean
- public ServletWebServerFactory servletContainer() {
- TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
- @Override
- protected void postProcessContext(Context context) {
- SecurityConstraint constraint = new SecurityConstraint();
- constraint.setUserConstraint("CONFIDENTIAL");
- SecurityCollection collection = new SecurityCollection();
- collection.addPattern("/*");
- constraint.addCollection(collection);
- context.addConstraint(constraint);
- }
- };
- return tomcat;
- }
- }
|