全局开启跨域
package com.pug.security.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
// 设置是否允许cookie
.allowCredentials(true)
// 设置允许的请求的方式
.allowedMethods("GET","POST","DELETE","PUT")
// 设置允许的header的属性
.allowedHeaders("*")
// 设置允许时间
.maxAge(3600);
}
}
也可以添加注解@CrossOrigin在controller上解决跨域