|
4 | 4 | import cn.springcloud.gray.server.resources.interceptor.AuthorityInterceptor; |
5 | 5 | import cn.springcloud.gray.server.resources.interceptor.NamespaceInterceptor; |
6 | 6 | import org.springframework.beans.factory.annotation.Autowired; |
7 | | -import org.springframework.boot.web.servlet.FilterRegistrationBean; |
8 | | -import org.springframework.context.annotation.Bean; |
9 | 7 | import org.springframework.context.annotation.ComponentScan; |
10 | 8 | import org.springframework.context.annotation.Configuration; |
11 | 9 | import org.springframework.context.annotation.Import; |
12 | | -import org.springframework.web.cors.CorsConfiguration; |
13 | | -import org.springframework.web.cors.UrlBasedCorsConfigurationSource; |
14 | | -import org.springframework.web.filter.CorsFilter; |
| 10 | +import org.springframework.web.servlet.config.annotation.CorsRegistry; |
15 | 11 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
16 | | -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; |
| 12 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
17 | 13 |
|
18 | 14 | /** |
19 | 15 | * Created by saleson on 2017/7/5. |
20 | 16 | */ |
21 | 17 | @Configuration |
22 | 18 | @ComponentScan({"cn.springcloud.gray.server.resources"}) |
23 | 19 | @Import(Swagger2Configuration.class) |
24 | | -public class WebConfiguration extends WebMvcConfigurerAdapter { |
| 20 | +public class WebConfiguration implements WebMvcConfigurer { |
25 | 21 |
|
26 | 22 |
|
27 | 23 | @Autowired |
28 | 24 | private AuthorityModule authorityModule; |
29 | 25 |
|
30 | | - @Bean |
31 | | - public FilterRegistrationBean filterRegistrationBean() { |
32 | | - |
33 | | - CorsConfiguration corsConfiguration = new CorsConfiguration(); |
34 | | - corsConfiguration.setAllowCredentials(true); |
35 | | - corsConfiguration.addAllowedOrigin("*"); |
36 | | - corsConfiguration.addAllowedHeader("*"); |
37 | | - corsConfiguration.addAllowedMethod("*"); |
38 | | - corsConfiguration.setMaxAge(3600l); |
39 | | - corsConfiguration.addExposedHeader("X-Total-Count"); |
40 | | - corsConfiguration.addExposedHeader("X-Pagination"); |
41 | | - UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource(); |
42 | | - urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration); |
43 | | - new CorsFilter(urlBasedCorsConfigurationSource); |
44 | | - |
45 | | - FilterRegistrationBean filterRegistrationBean = |
46 | | - new FilterRegistrationBean(new CorsFilter(urlBasedCorsConfigurationSource)); |
47 | | - filterRegistrationBean.addUrlPatterns("/*"); |
48 | | - return filterRegistrationBean; |
| 26 | +// @Bean |
| 27 | +// public FilterRegistrationBean filterRegistrationBean() { |
| 28 | +// |
| 29 | +// CorsConfiguration corsConfiguration = new CorsConfiguration(); |
| 30 | +// corsConfiguration.setAllowCredentials(true); |
| 31 | +// corsConfiguration.addAllowedOrigin("*"); |
| 32 | +// corsConfiguration.addAllowedHeader("*"); |
| 33 | +// corsConfiguration.addAllowedMethod("*"); |
| 34 | +// corsConfiguration.setMaxAge(3600l); |
| 35 | +// corsConfiguration.addExposedHeader("X-Total-Count"); |
| 36 | +// corsConfiguration.addExposedHeader("X-Pagination"); |
| 37 | +// UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource(); |
| 38 | +// urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration); |
| 39 | +// |
| 40 | +// FilterRegistrationBean filterRegistrationBean = |
| 41 | +// new FilterRegistrationBean(new CorsFilter(urlBasedCorsConfigurationSource)); |
| 42 | +// filterRegistrationBean.addUrlPatterns("/*"); |
| 43 | +// return filterRegistrationBean; |
| 44 | +// } |
| 45 | + |
| 46 | + |
| 47 | + @Override |
| 48 | + public void addCorsMappings(CorsRegistry registry) { |
| 49 | + registry.addMapping("/**") |
| 50 | + .allowedHeaders("*") |
| 51 | + .allowedMethods("*") |
| 52 | + .allowedOrigins("*") |
| 53 | + .allowCredentials(true) |
| 54 | + .exposedHeaders("X-Total-Count", "X-Pagination") |
| 55 | + .maxAge(3600l); |
49 | 56 | } |
50 | 57 |
|
51 | 58 | @Override |
52 | 59 | public void addInterceptors(InterceptorRegistry registry) { |
53 | | - super.addInterceptors(registry); |
54 | 60 | registry.addInterceptor(new NamespaceInterceptor(authorityModule)) |
55 | 61 | .addPathPatterns("/**"); |
56 | 62 | registry.addInterceptor(new AuthorityInterceptor(authorityModule)).addPathPatterns("/**"); |
|
0 commit comments