1.新建配置类WebMvcConfigurer
package com.lvxk.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
/**
* WebMvcConfigurer CORS实现
* Description: <br/>
* date: 2020/5/5 11:47<br/>
*
* @author lvxk<br />
* @since JDK 1.8
*/
@Configuration
public class WebMvcConfigurer implements org.springframework.web.servlet.config.annotation.WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")//允许跨域访问的路径
.allowedOrigins("*")//允许跨域访问的源
.allowedMethods("POST","GET","PUT","OPTIONS","DELETE")//允许的请求方法
.maxAge(168000)//允许头部设置
.allowCredentials(true);//默认发送cookie
}
}
网友评论