时间:2022-12-18 11:01:14 | 栏目:JAVA代码 | 点击:次
@CrossOrigin
可以添加到方法上,也可以添加到Controller
上
AbstractHandlerMethodMapping的内部类MappingRegistry的register:
public void register(T mapping, Object handler, Method method) { // Assert that the handler method is not a suspending one. if (KotlinDetector.isKotlinType(method.getDeclaringClass())) { Class<?>[] parameterTypes = method.getParameterTypes(); if ((parameterTypes.length > 0) && "kotlin.coroutines.Continuation".equals(parameterTypes[parameterTypes.length - 1].getName())) { throw new IllegalStateException("Unsupported suspending handler method detected: " + method); } } this.readWriteLock.writeLock().lock(); try { HandlerMethod handlerMethod = createHandlerMethod(handler, method); validateMethodMapping(handlerMethod, mapping); this.mappingLookup.put(mapping, handlerMethod); List<String> directUrls = getDirectUrls(mapping); for (String url : directUrls) { this.urlLookup.add(url, mapping); } String name = null; if (getNamingStrategy() != null) { name = getNamingStrategy().getName(handlerMethod, mapping); addMappingName(name, handlerMethod); } CorsConfiguration corsConfig = initCorsConfiguration(handler, method, mapping); if (corsConfig != null) { this.corsLookup.put(handlerMethod, corsConfig); } this.registry.put(mapping, new MappingRegistration<>(mapping, handlerMethod, directUrls, name)); } finally { this.readWriteLock.writeLock().unlock(); } }
@CrossOrigin
注解在AbstractHandlerMethodMapping
的内部类MappingRegistry
的register方法中完成解析,@CrossOrigin注解中的内容会被解析成一个配置对象CorsConfiguration@CrossOrigin
所标记的请求方法对象HandlerMethod
和CorsConfiguration
一一对应存入corsLookup的map集合中DispatcherServlet
的doDispatch方法之后,调用AbstractHandlerMapping
的getHandler方法获取执行链HandlerExecutionChain时,会从map中获取CorsConfiguration对象CorsConfiguration
对象构建一个CorsInterceptor拦截器CorsInterceptor
拦截器中触发对CorsProcessor
的processRequest方法调用,跨域请求的校验工作将在该方法中完成。@CrossOrigin
是添加在不同的Controller
中 全局配置
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedMethods("*") .allowedOrigins("*") .allowedHeaders("*") .allowCredentials(false) .exposedHeaders("") .maxAge(3600); } }
全局配置和@CrossOrigin
注解相同,都是在CorsInterceptor
拦截器中触发对CorsProcessor的processRequest方法调用,最终在该方法中完成跨域请求的校验工作
registry.addMapping(“/**”)
方法中配置了一个CorsRegistration对象,该对象中包含了一个路径拦截规则,同时CorsRegistration还包含了一个CorsConfiguration配置对象,该对象用来保存这里跨域相关的配置。WebMvcConfigurationSupport
的requestMappingHandlerMapping
方法中触发了addCorsMappings方法执行,将获取到的CorsRegistration对象重新组装成一个UrlBasedCorsConfigurationSource对象,该对象保存了拦截规则和CorsConfiguration对象的映射关系。UrlBasedCorsConfigurationSource
对象赋值给AbstractHandlerMapping的corsConfigurationSource属性@CrossOrigin
注解处理流程一样,在AbstractHandlerMapping的getHandler方法处理,从corsConfigurationSource
中获取CorsConfiguration配置对象,而@CrossOrigin从map中获取CorsConfiguration对象。如果两处都可以获取到CorsConfiguration对象,则获取到的对象属性值进行合并。CorsConfiguration
对象构造CorsInterceptor拦截器CorsInterceptor
拦截器中触发对CorsProcessor的processRequest方法调用,跨域请求的校验工作将在该方法中完成。这里的跨域校验是通过DispatcherServlet中的方法触发的,DispatcherServlet在Filter之后执行
@Configuration public class WebMvcConfig { @Bean FilterRegistrationBean<CorsFilter> corsFilter() { FilterRegistrationBean<CorsFilter> registrationBean = new FilterRegistrationBean<>(); CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowedHeaders(Arrays.asList("*")); corsConfiguration.setAllowedMethods(Arrays.asList("*")); corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:8081")); corsConfiguration.setMaxAge(3600L); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", corsConfiguration); registrationBean.setFilter(new CorsFilter(source)); registrationBean.setOrder(-1); return registrationBean; } }
CorsConfiguration
对象UrlBasedCorsConfigurationSource
对象,将过滤器的拦截规则和CorsConfiguration
对象之间的映射关系由UrlBasedCorsConfigurationSource
中的corsConfiguration
变量保存起来。CorsFilter
设置优先级CorsFilter的doFilterInternal方法:
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { CorsConfiguration corsConfiguration = this.configSource.getCorsConfiguration(request); boolean isValid = this.processor.processRequest(corsConfiguration, request, response); if (isValid && !CorsUtils.isPreFlightRequest(request)) { filterChain.doFilter(request, response); } }
触发对CorsProcessor
的processRequest
方法调用,跨域请求的校验工作将在该方法中完成