国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Spring前后端跨域請求設置代碼實例

瀏覽:79日期:2023-08-25 16:27:09

前后端項目分離,跨域請求時,后端的兩種配置方式:

1.配置類:

package com.helq3.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;/** * 跨域全局配置 */@Configurationpublic class CorsConfig { private CorsConfiguration buildConfig(){ CorsConfiguration configuration = new CorsConfiguration(); //設置屬性 //允許跨域請求的地址,*表示所有 configuration.addAllowedOrigin('*'); //配置跨域的請求頭 configuration.addAllowedHeader('*'); //配置跨域的請求方法 configuration.addAllowedMethod('*'); //表示跨域請求的時候使用的是否是同一個session configuration.setAllowCredentials(true); return configuration; } @Bean public CorsFilter corsFilter(){ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration('/**',buildConfig()); return new CorsFilter(source); }}

2.Controller上面配置

@CrossOrigin(origins = '*',allowedHeaders = '*',methods = {},allowCredentials = 'true')public class TestController {}

3.Ant Design Vue 中,在src/util/request.js中增加

axios.defaults.withCredentials = true

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 海原县| 犍为县| 申扎县| 蒙自县| 根河市| 清水县| 德保县| 观塘区| 香港 | 沙湾县| 定兴县| 长武县| 萝北县| 浦城县| 南康市| 雷波县| 江城| 志丹县| 博湖县| 孝义市| 黔南| 三江| 方城县| 靖宇县| 内乡县| 青阳县| 囊谦县| 宜黄县| 眉山市| 辉南县| 清镇市| 拉孜县| 淮滨县| 平定县| 额敏县| 兰州市| 荣成市| 昌都县| 大余县| 延吉市| 鄯善县|