欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

使用Spring CROS解决项目中的跨域问题详解

时间:2021-02-17 14:06:20|栏目:JAVA代码|点击:

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 Boot中利用JavaMailSender发送邮件的方法示例(附源码)

栏    目:JAVA代码

下一篇:SpringBoot使用RabbitMQ延时队列(小白必备)

本文标题:使用Spring CROS解决项目中的跨域问题详解

本文地址:http://www.codeinn.net/misctech/64879.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有