SpringBoot 跨域问题的解决方案
什么是跨域?
定义:浏览器从一个域名的网页取请求另一个域名下的东西。通俗点说,浏览器直接从A域访问B域中的资源是不被允许的,如果想要访问,就需要进行一步操作,这操作就叫“跨域”。例如,你从百度的页面,点击一个按钮,请求了新浪的一个接口,这就进行了跨域。不单单只有域名不同就是跨域,域名、端口、协议其一不同就是不同的域,请求资源需要跨域。
为什么要跨域?
为什么需要跨域,而不直接访问其他域下的资源呢?这是浏览器的限制,专业点说叫浏览器同源策略限制。主要是为了安全考虑。现在的安全框架,一般请求的时候header中不是都存个token嘛,你要是用这个token去正常访问A域下的东西是没问题的,然后又去访问了B域,结果阴差阳错的还带着这个token,那么B域,或者说B网站是不是就可以拿着你的token去A域下做点什么呢,这就相当危险了。所以浏览器加上了所谓的浏览器同源策略限制。但是为了我们真的需要从A域下访问B的资源(正常访问),就需要用到跨域,跨越这个限制了。
SpringBoot解决跨域问题
SpringBoot可以基于Cors解决跨域问题,Cors是一种机制,告诉我们的后台,哪边(origin)来的请求可以访问服务器的数据。
全局配置
配置实例如下:
@Configuration publicclassCorsConfigimplementsWebMvcConfigurer{ @Override publicvoidaddCorsMappings(CorsRegistryregistry){ registry.addMapping("/**") .allowedOrigins("*") .allowCredentials(true) .allowedMethods("GET","POST","PUT","DELETE","OPTIONS") .maxAge(3600); } }
首先实现了WebMvcConfigurer接口,WebMvcConfigurer这个接口十分强大,里面还有很多可用的方法,在SpringBoot2.0里面可以解决WebMvcConfigurerAdapter曾经的部分任务。其中一个方法就是addCorsMappings(),是专门为开发人员解决跨域而诞生的接口。其中构造参数为CorsRegistry。
看下CorsRegistry源码,十分简单:
publicclassCorsRegistry{ privatefinalListregistrations=newArrayList<>(); publicCorsRegistrationaddMapping(StringpathPattern){ CorsRegistrationregistration=newCorsRegistration(pathPattern); this.registrations.add(registration); returnregistration; } protectedMap getCorsConfigurations(){ Map configs=newLinkedHashMap<>(this.registrations.size()); for(CorsRegistrationregistration:this.registrations){ configs.put(registration.getPathPattern(),registration.getCorsConfiguration()); } returnconfigs; } }
可以看出CorsRegistry有个属性registrations,按道理可以根据不同的项目路径进行定制访问行为,但是我们示例直接将pathPattern设置为/**,也就是说已覆盖项目所有路径,只需要创建一个CorsRegistration就好。getCorsConfigurations(),这个方法是获取所有CorsConfiguration的Map集合,key值为传入路径pathPattern。
回到示例代码CorsConfig中,registry对象addMapping()增加完传入路径pathPattern之后,return了一个CorsRegistration对象,是进行更多的配置,看一下CorsRegistration的代码,看看我们能配些什么?
publicclassCorsRegistration{ //传入的路径 privatefinalStringpathPattern; //配置信息实体类 privatefinalCorsConfigurationconfig; //构造方法 publicCorsRegistration(StringpathPattern){ this.pathPattern=pathPattern; //原生注释看到了一个@CrossOrigin这个注解,待会看看是什么 //Sameimplicitdefaultvaluesasthe@CrossOriginannotation+allowssimplemethods this.config=newCorsConfiguration().applyPermitDefaultValues(); } //允许哪些源网站访问,默认所有 publicCorsRegistrationallowedOrigins(String...origins){ this.config.setAllowedOrigins(Arrays.asList(origins)); returnthis; } //允许何种方式访问,默认简单方式,即:GET,HEAD,POST publicCorsRegistrationallowedMethods(String...methods){ this.config.setAllowedMethods(Arrays.asList(methods)); returnthis; } //设置访问header,默认所有 publicCorsRegistrationallowedHeaders(String...headers){ this.config.setAllowedHeaders(Arrays.asList(headers)); returnthis; } //设置responseheaders,默认没有(什么都不设置) publicCorsRegistrationexposedHeaders(String...headers){ this.config.setExposedHeaders(Arrays.asList(headers)); returnthis; } //是否浏览器应该发送credentials,例如cookiesAccess-Control-Allow-Credentials publicCorsRegistrationallowCredentials(booleanallowCredentials){ this.config.setAllowCredentials(allowCredentials); returnthis; } //设置等待时间,默认1800秒 publicCorsRegistrationmaxAge(longmaxAge){ this.config.setMaxAge(maxAge); returnthis; } protectedStringgetPathPattern(){ returnthis.pathPattern; } protectedCorsConfigurationgetCorsConfiguration(){ returnthis.config; } }
局部配置
刚才遇到一个@CrossOrigin这个注解,看看它是干什么的?
@Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public@interfaceCrossOrigin{ /**@deprecatedasofSpring5.0,infavorof{@linkCorsConfiguration#applyPermitDefaultValues}*/ @Deprecated String[]DEFAULT_ORIGINS={"*"}; /**@deprecatedasofSpring5.0,infavorof{@linkCorsConfiguration#applyPermitDefaultValues}*/ @Deprecated String[]DEFAULT_ALLOWED_HEADERS={"*"}; /**@deprecatedasofSpring5.0,infavorof{@linkCorsConfiguration#applyPermitDefaultValues}*/ @Deprecated booleanDEFAULT_ALLOW_CREDENTIALS=false; /**@deprecatedasofSpring5.0,infavorof{@linkCorsConfiguration#applyPermitDefaultValues}*/ @Deprecated longDEFAULT_MAX_AGE=1800 /** *Aliasfor{@link#origins}. */ @AliasFor("origins") String[]value()default{}; @AliasFor("value") String[]origins()default{}; String[]allowedHeaders()default{}; String[]exposedHeaders()default{}; RequestMethod[]methods()default{}; StringallowCredentials()default""; longmaxAge()default-1; }
这个注解可以作用于方法或者类上,实现局部跨域,你会发现除了设置路径(因为没必要了,都定位到局部了)其他的参数与全局类似。
小结
SpringBoot可以基于Cors解决跨域问题,可以设置全局跨域,也可以实现局部跨域,灵活配置方便使用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。