Spring前后端跨域請(qǐng)求設(shè)置代碼實(shí)例
前后端項(xiàng)目分離,跨域請(qǐng)求時(shí),后端的兩種配置方式:
1.配置類(lèi):
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(); //設(shè)置屬性 //允許跨域請(qǐng)求的地址,*表示所有 configuration.addAllowedOrigin('*'); //配置跨域的請(qǐng)求頭 configuration.addAllowedHeader('*'); //配置跨域的請(qǐng)求方法 configuration.addAllowedMethod('*'); //表示跨域請(qǐng)求的時(shí)候使用的是否是同一個(gè)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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. docker容器調(diào)用yum報(bào)錯(cuò)的解決辦法2. Docker容器如何更新打包并上傳到阿里云3. idea重置默認(rèn)配置的方法步驟4. IntelliJ IDEA安裝插件的方法步驟5. 原生JS實(shí)現(xiàn)音樂(lè)播放器的示例代碼6. Net core中使用System.Drawing對(duì)上傳的圖片流進(jìn)行壓縮(示例代碼)7. 如何學(xué)習(xí)html的各種標(biāo)簽8. IDEA自動(dòng)生成TestNG的testng.xml的插件方法9. asp批量添加修改刪除操作示例代碼10. 利用CSS制作3D動(dòng)畫(huà)
