Spring前后端跨域請(qǐng)求設(shè)置代碼實(shí)例
前后端項(xiàng)目分離,跨域請(qǐng)求時(shí),后端的兩種配置方式:
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(); //設(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
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 詳解瀏覽器的緩存機(jī)制2. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)3. ASP基礎(chǔ)知識(shí)VBScript基本元素講解4. UDDI FAQs5. XML入門的常見問題(四)6. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)7. XML解析錯(cuò)誤:未組織好 的解決辦法8. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼9. 利用CSS3新特性創(chuàng)建透明邊框三角10. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁的方法
