|
23 | 23 | import org.springframework.http.converter.HttpMessageConverter;
|
24 | 24 | import org.springframework.http.converter.StringHttpMessageConverter;
|
25 | 25 | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
26 |
| -import org.springframework.web.filter.CharacterEncodingFilter; |
27 | 26 | import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
|
28 | 27 | import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
29 | 28 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
43 | 42 | public class SpringConfig implements WebMvcConfigurer, ErrorPageRegistrar {
|
44 | 43 |
|
45 | 44 |
|
46 |
| - @Autowired(required = false) |
47 |
| - private InjectionAttackInterceptor injectionAttackInterceptor; |
48 |
| - |
49 |
| - /** |
50 |
| - * <pre> |
51 |
| - * aidijing: |
52 |
| - * filter: |
53 |
| - * injection-attack-interceptor: |
54 |
| - * enabled: true |
55 |
| - * |
56 |
| - * </pre> |
57 |
| - */ |
58 |
| - @Bean |
59 |
| - @Order( Ordered.HIGHEST_PRECEDENCE ) |
60 |
| - @ConditionalOnProperty( prefix = "aidijing.filter.injection-attack-interceptor", name = "enabled", havingValue = "true" ) |
61 |
| - public InjectionAttackInterceptor injectionAttackInterceptor () { |
62 |
| - return new InjectionAttackInterceptor(); |
63 |
| - } |
64 |
| - |
65 |
| - |
66 |
| - /** |
67 |
| - * 添加过滤器 |
68 |
| - * |
69 |
| - * @return |
70 |
| - */ |
71 |
| - @Bean |
72 |
| - public FilterRegistrationBean filterRegistrationBean () { |
73 |
| - // 日志处理过滤器 |
74 |
| - return new FilterRegistrationBean<>( new RequestLoggingFilter() ); |
75 |
| - } |
76 |
| - |
77 |
| - /** |
78 |
| - * 添加转换器 |
79 |
| - * |
80 |
| - * @param registry |
81 |
| - */ |
82 |
| - @Override |
83 |
| - public void addFormatters ( FormatterRegistry registry ) { |
84 |
| - // 从前台过来的数据转换成对应类型的转换器,但是对 @RequestBody 注解的参数无效 |
85 |
| - registry.addConverter( new StringToDateConverter() ); |
86 |
| - } |
87 |
| - |
88 |
| - @Bean |
89 |
| - public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter () { |
90 |
| - ObjectMapper customizationMapper = JsonUtils.buildCustomizationMapper() |
91 |
| - // 设置格式化解析,支持多种,针对 @RequestBody 参数解析时,前端传入不同种类时间格式(以字符串形式)时能够正常解析. |
92 |
| - // 解析失败时会抛 HttpMessageNotReadableException 异常 |
93 |
| - .setDateFormat( new SimpleDateFormatPro( DateFormatStyle.getDateFormatStyles() ) ); |
94 |
| - return new MappingJackson2HttpMessageConverter( customizationMapper ); |
95 |
| - } |
96 |
| - |
97 |
| - /** |
98 |
| - * 消息转换 |
99 |
| - * |
100 |
| - * @param converters |
101 |
| - */ |
102 |
| - @Override |
103 |
| - public void extendMessageConverters ( List< HttpMessageConverter< ? > > converters ) { |
104 |
| - // 默认转换器注册后, 插入自定义的请求响应转换器 |
105 |
| - converters.add( new StringHttpMessageConverter( StandardCharsets.UTF_8 ) ); |
106 |
| - converters.add( this.mappingJackson2HttpMessageConverter() ); |
107 |
| - |
108 |
| - } |
109 |
| - |
110 |
| - |
111 |
| - @Override |
112 |
| - public void addReturnValueHandlers ( List< HandlerMethodReturnValueHandler > returnValueHandlers ) { |
113 |
| - // 返回值的处理,可以用来处理敏感数据的显示 |
114 |
| - } |
115 |
| - |
116 |
| - @Override |
117 |
| - public void addInterceptors ( InterceptorRegistry registry ) { |
118 |
| - if ( Objects.nonNull( injectionAttackInterceptor ) ) { |
119 |
| - registry.addInterceptor( injectionAttackInterceptor ).addPathPatterns( "/**" ); |
120 |
| - } |
121 |
| - } |
122 |
| - |
123 |
| - /** |
124 |
| - * cors跨域处理 |
125 |
| - * |
126 |
| - * @param registry |
127 |
| - */ |
128 |
| - @Override |
129 |
| - public void addCorsMappings ( CorsRegistry registry ) { |
130 |
| - registry.addMapping( "/**" ) |
131 |
| - .allowedMethods( |
132 |
| - HttpMethod.HEAD.name() , |
133 |
| - HttpMethod.GET.name() , |
134 |
| - HttpMethod.POST.name() , |
135 |
| - HttpMethod.PUT.name() , |
136 |
| - HttpMethod.DELETE.name() , |
137 |
| - HttpMethod.OPTIONS.name() , |
138 |
| - HttpMethod.PATCH.name() , |
139 |
| - HttpMethod.TRACE.name() |
140 |
| - ) |
141 |
| - // 允许的域名 |
142 |
| - .allowedOrigins( "*" ); |
143 |
| - } |
144 |
| - |
145 |
| - |
146 |
| - @Override |
147 |
| - public void registerErrorPages ( ErrorPageRegistry registry ) { |
148 |
| - registry.addErrorPages( new ErrorPage( HttpStatus.NOT_FOUND , "/404" ) ); |
149 |
| - registry.addErrorPages( new ErrorPage( HttpStatus.UNAUTHORIZED , "/401" ) ); |
150 |
| - registry.addErrorPages( new ErrorPage( Throwable.class , "/500" ) ); |
151 |
| - } |
| 45 | + @Autowired( required = false ) |
| 46 | + private InjectionAttackInterceptor injectionAttackInterceptor; |
| 47 | + |
| 48 | + /** |
| 49 | + * <pre> |
| 50 | + * goblin: |
| 51 | + * filter: |
| 52 | + * injection-attack-interceptor: |
| 53 | + * enabled: true |
| 54 | + * |
| 55 | + * </pre> |
| 56 | + */ |
| 57 | + @Bean |
| 58 | + @Order( Ordered.HIGHEST_PRECEDENCE ) |
| 59 | + @ConditionalOnProperty( prefix = "goblin.filter.injection-attack-interceptor", name = "enabled", havingValue = "true" ) |
| 60 | + public InjectionAttackInterceptor injectionAttackInterceptor () { |
| 61 | + return new InjectionAttackInterceptor(); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + /** |
| 66 | + * 添加过滤器 |
| 67 | + * |
| 68 | + * @return |
| 69 | + */ |
| 70 | + @Bean |
| 71 | + public FilterRegistrationBean filterRegistrationBean () { |
| 72 | + String[] excludeUrlPatterns = { "*.js" , "*.jpg" , "*.png" , "*.css" , "*.html" , "*.gif" }; |
| 73 | + // 日志处理过滤器 |
| 74 | + return new FilterRegistrationBean<>( new RequestLoggingFilter().setExcludeUrlPatterns( excludeUrlPatterns ) ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * 添加转换器 |
| 79 | + * |
| 80 | + * @param registry |
| 81 | + */ |
| 82 | + @Override |
| 83 | + public void addFormatters ( FormatterRegistry registry ) { |
| 84 | + // 从前台过来的数据转换成对应类型的转换器,但是对 @RequestBody 注解的参数无效 |
| 85 | + registry.addConverter( new StringToDateConverter() ); |
| 86 | + } |
| 87 | + |
| 88 | + @Bean |
| 89 | + public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter () { |
| 90 | + ObjectMapper customizationMapper = JsonUtils.buildCustomizationMapper() |
| 91 | + // 设置格式化解析,支持多种,针对 @RequestBody 参数解析时,前端传入不同种类时间格式(以字符串形式)时能够正常解析. |
| 92 | + // 解析失败时会抛 HttpMessageNotReadableException 异常 |
| 93 | + .setDateFormat( new SimpleDateFormatPro( DateFormatStyle.getDateFormatStyles() ) ); |
| 94 | + return new MappingJackson2HttpMessageConverter( customizationMapper ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * 消息转换 |
| 99 | + * |
| 100 | + * @param converters |
| 101 | + */ |
| 102 | + @Override |
| 103 | + public void extendMessageConverters ( List< HttpMessageConverter< ? > > converters ) { |
| 104 | + // 默认转换器注册后, 插入自定义的请求响应转换器 |
| 105 | + converters.add( new StringHttpMessageConverter( StandardCharsets.UTF_8 ) ); |
| 106 | + converters.add( this.mappingJackson2HttpMessageConverter() ); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + @Override |
| 112 | + public void addReturnValueHandlers ( List< HandlerMethodReturnValueHandler > returnValueHandlers ) { |
| 113 | + // 返回值的处理,可以用来处理敏感数据的显示 |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void addInterceptors ( InterceptorRegistry registry ) { |
| 118 | + if ( Objects.nonNull( injectionAttackInterceptor ) ) { |
| 119 | + registry.addInterceptor( injectionAttackInterceptor ).addPathPatterns( "/**" ); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * cors跨域处理 |
| 125 | + * |
| 126 | + * @param registry |
| 127 | + */ |
| 128 | + @Override |
| 129 | + public void addCorsMappings ( CorsRegistry registry ) { |
| 130 | + registry.addMapping( "/**" ) |
| 131 | + .allowedMethods( |
| 132 | + HttpMethod.HEAD.name() , |
| 133 | + HttpMethod.GET.name() , |
| 134 | + HttpMethod.POST.name() , |
| 135 | + HttpMethod.PUT.name() , |
| 136 | + HttpMethod.DELETE.name() , |
| 137 | + HttpMethod.OPTIONS.name() , |
| 138 | + HttpMethod.PATCH.name() , |
| 139 | + HttpMethod.TRACE.name() |
| 140 | + ) |
| 141 | + // 允许的域名 |
| 142 | + .allowedOrigins( "*" ); |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + @Override |
| 147 | + public void registerErrorPages ( ErrorPageRegistry registry ) { |
| 148 | + registry.addErrorPages( new ErrorPage( HttpStatus.NOT_FOUND , "/404" ) ); |
| 149 | + registry.addErrorPages( new ErrorPage( HttpStatus.UNAUTHORIZED , "/401" ) ); |
| 150 | + registry.addErrorPages( new ErrorPage( Throwable.class , "/500" ) ); |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + |
152 | 155 | }
|
0 commit comments