方式1:编写config文件
package com.manager.config;/**
* @author tian
* @date 2023/10/16 11:25
*/
import com.manager.interceptor.LoginAuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
import java.util.TimeZone;
/**
* @author tian
* @date 2023/10/16 11:25
*/
@Component
public class WebMvcConfiguration implements WebMvcConfigurer {
//跨域
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") //添加路径规则
.allowCredentials(true) //是否允许在跨域的情况下传递cookie
.allowedOriginPatterns("*") //允许请求来源的域规则
.allowedMethods("*")
.allowedHeaders("*");
}
}
方式2:Controller上添加@CrossOrigin注解