如何使用SpringCROS解决项目中的跨域问题

这篇文章主要讲解了“如何使用SpringCROS解决项目中的跨域问题”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何使用SpringCROS解决项目中的跨域问题”吧!

成都创新互联是一家集网站建设,镇海企业网站建设,镇海品牌网站建设,网站定制,镇海网站建设报价,网络营销,网络优化,镇海网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

CROS(Cross-Origin Resource Sharing) 用于解决浏览器中跨域请求的问题。简单的Get请求可以使用JSONP来解决,而对于其它复杂的请求则需要后端应用的支持CROS。Spring在4.2版本之后提供了@CrossOrigin 注解来实现对Cross的支持。

在Controller方法上配置

@CrossOrigin(origins = {"http://loaclhost:8088"})@RequestMapping(value = "/crossTest",method = RequestMethod.GET)public String greeting() {  return "corss test";}

在Controller上配置,那么这个Controller中的所有方法都会支持CORS

import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.CrossOrigin;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@CrossOrigin(origins = "http://localhost:8088",maxAge = 3600)@Controller@RequestMapping("/api")public class AppController {    @RequestMapping(value = "/crossTest",method = RequestMethod.GET)    public String greeting() {      return "corss test";    }}

Java Config全局配置

import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.CorsRegistry;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configuration@EnableWebMvcpublic class SpringWebConfig extends WebMvcConfigurerAdapter {  /**   * {@inheritDoc}   * <p>This implementation is empty.   *   * @param registry   */  @Override  public void addCorsMappings(CorsRegistry registry) {    super.addCorsMappings(registry);    // 对所有的URL配置    registry.addMapping("/**");    // 针对某些URL配置    registry.addMapping("/api/**").allowedOrigins("http:///localhost:8088")        .allowedMethods("PUT","DELETE")        .allowedHeaders("header1","header2","header3")        .exposedHeaders("header1","header2")        .allowCredentials(false).maxAge(3600);  }}

XML全局配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd    http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc.xsd">  <mvc:cors>    <!--<mvc:mapping path=""/>-->    <mvc:mapping path="/api/**"           allowed-origins="http://localhost:8088,http://localhost:8888"           allowed-methods="GET,PUT"           allowed-headers="header1,header2"           exposed-headers="header1,header2"           allow-credentials="false"           max-age="3600" />  </mvc:cors></beans>

感谢各位的阅读,以上就是“如何使用SpringCROS解决项目中的跨域问题”的内容了,经过本文的学习后,相信大家对如何使用SpringCROS解决项目中的跨域问题这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!

网站标题:如何使用SpringCROS解决项目中的跨域问题
网页URL:https://www.cdcxhl.com/article28/jjigjp.html

成都网站建设公司_创新互联,为您提供域名注册营销型网站建设建站公司关键词优化手机网站建设网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

搜索引擎优化