WebMvcConfig.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package com.zhiqiyun.open.config;
  2. import com.fasterxml.jackson.core.JsonGenerator;
  3. import com.fasterxml.jackson.databind.DeserializationFeature;
  4. import com.fasterxml.jackson.databind.JsonSerializer;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import com.fasterxml.jackson.databind.SerializerProvider;
  7. import com.fasterxml.jackson.databind.module.SimpleModule;
  8. import com.zhiqiyun.open.core.enmus.IBaseEnum;
  9. import com.zhiqiyun.open.utils.ServletContext;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.http.converter.HttpMessageConverter;
  14. import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
  15. import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
  16. import org.springframework.lang.NonNull;
  17. import org.springframework.web.servlet.HandlerInterceptor;
  18. import org.springframework.web.servlet.config.annotation.CorsRegistry;
  19. import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
  20. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  21. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.io.IOException;
  25. import java.util.List;
  26. /**
  27. * 系统启动配置
  28. *
  29. * @author jtoms
  30. */
  31. @Slf4j
  32. @Configuration
  33. public class WebMvcConfig extends DelegatingWebMvcConfiguration {
  34. @Autowired
  35. private OauthInterceptor oauthInterceptor;
  36. @Override
  37. protected void addResourceHandlers(ResourceHandlerRegistry registry) {
  38. registry.addResourceHandler("/**").addResourceLocations("classpath:/public/");
  39. super.addResourceHandlers(registry);
  40. }
  41. @Override
  42. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  43. converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
  44. ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
  45. objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
  46. SimpleModule module = new SimpleModule();
  47. module.addSerializer(IBaseEnum.class, new JsonSerializer<IBaseEnum>() {
  48. @Override
  49. public void serialize(IBaseEnum value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
  50. gen.writeStartObject();
  51. gen.writeObjectField("value", value.getValue());
  52. gen.writeStringField("text", value.getText());
  53. gen.writeStringField("name", value.getName());
  54. gen.writeEndObject();
  55. }
  56. });
  57. objectMapper.registerModule(module);
  58. MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(objectMapper);
  59. converters.add(converter);
  60. log.info("{}", converters.size());
  61. super.configureMessageConverters(converters);
  62. }
  63. @Override
  64. public void addInterceptors(InterceptorRegistry registry) {
  65. registry.addInterceptor(new HandlerInterceptor() {
  66. @Override
  67. public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) {
  68. ServletContext.init(request, response);
  69. return true;
  70. }
  71. @Override
  72. public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, Exception ex) {
  73. ServletContext.clean();
  74. }
  75. }).addPathPatterns("/**").excludePathPatterns("/static/**", "/index.html");
  76. registry.addInterceptor(this.oauthInterceptor).addPathPatterns("/**").excludePathPatterns("/static/**", "/index.html");
  77. super.addInterceptors(registry);
  78. }
  79. @Override
  80. public void addCorsMappings(CorsRegistry registry) {
  81. registry.addMapping("/**")
  82. .allowedOriginPatterns("*")
  83. .allowCredentials(true)
  84. .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")
  85. .maxAge(3600);
  86. super.addCorsMappings(registry);
  87. }
  88. }