最近苦逼的又开始写java了,作为一个前端开发者最讨厌的就是配置代理,不灵活,来java后,直接把它改了,但是网上试了各种跨域都不好用,唯独这份好使,废话不说看下图:
package com.tdkj.modules.system.utils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")//项目中的所有接口都支持跨域
.allowedOriginPatterns("*")//所有地址都可以访问,也可以配置具体地址
.allowCredentials(true)
.allowedMethods("*")//"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"
.maxAge(3600);// 跨域允许时间
}
}
转:SpringBoot 优雅配置跨域多种方式及Spring Security跨域访问配置的坑 - kenx - 博客园