1.实现接口WebMvcConfigurer的addCorsMappings方法,配置addMapping,allowedOrigins,allowedHeaders,allowedMethods的属性。设置“*”表示“所有”的意思对于只开放某个域访问,通过allowedOrigins方法设置。如下:
package com.lz.hehuorenservice.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/** Create by hyhweb on 2021/6/1 22:13 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedHeaders("*").allowedMethods("*");
}
}