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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. 怎樣才能用js生成xmldom對象,并且在firefox中也實現(xiàn)xml數(shù)據(jù)島?2. 基于javaweb+jsp實現(xiàn)企業(yè)車輛管理系統(tǒng)3. 利用ajax+php實現(xiàn)商品價格計算4. ASP.Net MVC利用NPOI導入導出Excel的示例代碼5. jstl 字符串處理函數(shù)6. JSP動態(tài)網(wǎng)頁開發(fā)原理詳解7. PHP中為什么使用file_get_contents("php://input")接收微信通知8. XML CDATA是什么?9. IOS蘋果AppStore內購付款的服務器端php驗證方法(使用thinkphp)10. .Net core Blazor+自定義日志提供器實現(xiàn)實時日志查看器的原理解析
