关于配置cors跨域

来源:互联网 发布:puppy linux吧 编辑:程序博客网 时间:2024/06/06 01:45

普通web项目:

1.首先引入jar包

在lib文件夹中添加jar

1)cors-filter-2.4.jar

2)java-property-utils-1.9.1.jar

2.在web.xml中加入配置

webapp节点下加入如下编码:
        <filter><filter-name>CORS</filter-name><filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class></filter><filter-mapping><filter-name>CORS</filter-name><servlet-name>/*</servlet-name></filter-mapping><filter-mapping><filter-name>CORS</filter-name><url-pattern>/*</url-pattern></filter-mapping>

相关网页:http://software.dzhuvinov.com/cors-filter-installation.html

springboot项目:
经过尝试在pom.xml引入依赖,再配置web.xml,这个方法没有跨域成功。
正确方式:在src/package任意地方新建类
package com.monetware.config;import org.springframework.context.annotation.Configuration;  import org.springframework.web.servlet.config.annotation.CorsRegistry;  import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;      @Configuration  public class CorsConfig extends WebMvcConfigurerAdapter {        @Override      public void addCorsMappings(CorsRegistry registry) {          registry.addMapping("/**")                  .allowedOrigins("*")                  .allowCredentials(true)                  .allowedMethods("GET", "POST", "DELETE", "PUT")                  .maxAge(3600);      }    }  

这样就解决了跨域问题
0 0
原创粉丝点击