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. ASP.Net MVC利用NPOI導(dǎo)入導(dǎo)出Excel的示例代碼2. ThinkPHP5 通過(guò)ajax插入圖片并實(shí)時(shí)顯示(完整代碼)3. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼4. Python OpenCV實(shí)現(xiàn)測(cè)量圖片物體寬度5. asp文件用什么軟件編輯6. Python使用openpyxl復(fù)制整張sheet7. python opencv把一張圖片嵌入(疊加)到另一張圖片上的實(shí)現(xiàn)代碼8. Android通過(guò)Java sdk的方式接入OpenCv的方法9. python 基于opencv 實(shí)現(xiàn)一個(gè)鼠標(biāo)繪圖小程序10. PHP擴(kuò)展之APC——Alternative PHP Cache(可選PHP緩存)
