MvcConfigurer.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.api.base.config;
  2. import com.api.common.config.UploadConfig;
  3. import com.api.core.annotation.condition.HttpsCondition;
  4. import org.apache.catalina.Context;
  5. import org.apache.tomcat.util.descriptor.web.SecurityCollection;
  6. import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
  10. import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Conditional;
  13. import org.springframework.context.annotation.Configuration;
  14. import org.springframework.http.converter.HttpMessageConverter;
  15. import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
  16. import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
  17. import org.springframework.web.servlet.config.annotation.CorsRegistry;
  18. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  19. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  20. import javax.annotation.Resource;
  21. import java.util.List;
  22. /**
  23. * Spring MVC 配置
  24. */
  25. @Configuration
  26. public class MvcConfigurer implements WebMvcConfigurer {
  27. private final Logger logger = LoggerFactory.getLogger(MvcConfigurer.class);
  28. @Resource
  29. private UploadConfig uploadConfig;
  30. @Override
  31. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  32. registry.addResourceHandler("/"+uploadConfig.getPrefix()+"**")
  33. .addResourceLocations("file:" + uploadConfig.getFilePath());
  34. registry.addResourceHandler("swagger-ui.html")
  35. .addResourceLocations("classpath:/META-INF/resources/");
  36. registry.addResourceHandler("/webjars/**")
  37. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  38. registry.addResourceHandler("/**")
  39. .addResourceLocations("classpath:/META-INF/resources/")
  40. .addResourceLocations("classpath:/resources/")
  41. .addResourceLocations("classpath:/static/");
  42. WebMvcConfigurer.super.addResourceHandlers(registry);
  43. }
  44. //解决跨域问题
  45. @Override
  46. public void addCorsMappings(CorsRegistry registry) {
  47. registry.addMapping("/**")
  48. .allowedHeaders("*")
  49. .allowedMethods("*")
  50. .allowedOrigins("*");
  51. }
  52. @Override
  53. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  54. Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
  55. builder.indentOutput(true);
  56. converters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));
  57. }
  58. @Conditional(HttpsCondition.class)
  59. @Bean
  60. public ServletWebServerFactory servletContainer() {
  61. TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
  62. @Override
  63. protected void postProcessContext(Context context) {
  64. SecurityConstraint constraint = new SecurityConstraint();
  65. constraint.setUserConstraint("CONFIDENTIAL");
  66. SecurityCollection collection = new SecurityCollection();
  67. collection.addPattern("/*");
  68. constraint.addCollection(collection);
  69. context.addConstraint(constraint);
  70. }
  71. };
  72. return tomcat;
  73. }
  74. }