温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Spring CROS项目中解决跨域问题

发布时间:2021-01-18 16:16:54 来源:亿速云 阅读:161 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关如何在Spring CROS项目中解决跨域问题,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

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
@EnableWebMvc
public 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>

看完上述内容,你们对如何在Spring CROS项目中解决跨域问题有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI