久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

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

瀏覽:73日期: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
相關文章:
主站蜘蛛池模板: 廊坊市| 正镶白旗| 仁化县| 阿克苏市| 新巴尔虎右旗| 杨浦区| 池州市| 临潭县| 绥德县| 齐齐哈尔市| 望都县| 旌德县| 丹寨县| 新晃| 墨江| 绥滨县| 华容县| 正蓝旗| 岢岚县| 区。| 齐河县| 肥东县| 汉川市| 信丰县| 略阳县| 开江县| 渭南市| 南丹县| 安新县| 金塔县| 远安县| 石渠县| 福建省| 玉龙| 巴彦县| 登封市| 乌拉特后旗| 南平市| 华蓥市| 梁山县| 年辖:市辖区|