注册跨域过滤器 CorsFilter
@Bean public CorsFilter corsFilter() { CorsConfiguration config = new CorsConfiguration(); config.addAllowedHeader("*"); config.addAllowedMethod("*"); config.addAllowedOriginPattern("*");//当allowCredentials=true是设置该属性 config.setAllowCredentials(true); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", config); return new CorsFilter(source); }
验证:
1、打开浏览器,F12进入控制台
2、粘贴以下代码
var xhr = new XMLHttpRequest(); xhr.open('POST', 'http://localhost:8200/user/login', true); xhr.setRequestHeader('Content-Type', 'application/json'); var data = {"userName": "zs","password": "0192023a7bbd73250516f069df18b500"}; xhr.send(JSON.stringify(data)); xhr.onload = function(e) { var xhr = e.target; console.log(xhr.responseText); }
标签:springboot,source,处理,true,xhr,var,new,config,跨域 From: https://www.cnblogs.com/langty/p/18554180