spring boot 配置允許跨域

2021-10-01 18:06:44 字數 959 閱讀 2381

簡單概述什麼是跨域

跨域是指乙個域下的文件或指令碼試圖去請求另乙個域下的資源,這裡跨域是廣義的。其實我們通常所說的跨域是狹義的,是由瀏覽器同源策略限制的一類請求場景。瀏覽器從乙個網域名稱的網頁去請求另乙個網域名稱的資源時,網域名稱、埠、協議任一不同,都是跨域。跨域的訪問會帶來許多安全性的問題,比如,cookie 一般用於狀態控制,常用於儲存登入的資訊,如果允許跨域訪問,那麼別的**只需要一段指令碼就可以獲取你的 cookie,從而冒充你的身份去登入**,造成非常大的安全問題,因此,現代瀏覽器均推行同源策略。

解決跨域的一種方案

跨域的解決方案有很多,下面簡單貼乙個spring boot 配置允許跨域的一種方案,在開發測試時很方便,僅推薦在開發測試時使用。

import org.springframework.context.annotation.bean;

import org.springframework.context.annotation.configuration;

import org.springframework.web.servlet.config.annotation.corsregistry;

import org.springframework.web.servlet.config.annotation.webmvcconfigurer;

import org.springframework.web.socket.server.standard.serverendpointexporter;

@configuration

public

class

webmvcconfig

implements

webmvcconfigurer

@override

public

void

(corsregistry registry)

}

springboot配置CORS允許跨域訪問

參考 cors 跨域資源共享 springboot配置cors解決跨域請求問題 真實請求允許的方法 access control allow methods get,post,put,delete 伺服器允許使用的字段 access control allow headers x custom he...

Spring Boot允許跨域請求

1 專案下新建乙個config包 專門用於放置配置檔案 但是這只是最佳目錄 實際上spring boot對與檔案結構是沒有要求的 只用註解來判斷每個類的作用 2 新建乙個檔案類corsconfig,編寫以下 package com.xhwl.erp.config import org.springf...

SpringBoot允許跨域請求

例項 import org.springframework beans factory annotation value import org.springframework context annotation bean import org.springframework context ann...