Spring前后端跨域請求設置代碼實例
前后端項目分離,跨域請求時,后端的兩種配置方式:
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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. 基于python計算滾動方差(標準差)talib和pd.rolling函數差異詳解2. Nodejs 連接 mysql時報Error: Cannot enqueue Query after fatal error錯誤的處理辦法3. java中Servlet監聽器的工作原理及示例詳解4. python檢查目錄文件權限并修改目錄文件權限的操作5. css進階學習 選擇符6. 解決python腳本中error: unrecognized arguments: True錯誤7. ASP基礎知識Command對象講解8. Ajax對xml信息的接收和處理操作實例分析9. JSP實現文件上傳功能10. java不解壓直接讀取壓縮包中文件的實現方法
