前後端完全分離之後的跨域問題應該如何解決

2021-08-29 00:00:23 字數 2142 閱讀 9729

記得剛剛進公司的時候遇到的第乙個問題就是前後端完全分離,在apache伺服器的頁面要發請求給tomcat伺服器的後端,那麼後端要怎麼處理才能讓不同伺服器的頁面能傳送請求過來?

根據上任留下來的筆記,這裡面有兩個版本。

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;

@configuration

public

class

corsconfig

@bean

public corsfilter corsfilter()

}

>

>

corsfilterfilter-name

>

>

org.apache.catalina.filters.corsfilterfilter-class

>

>

>

cors.allowed.originsparam-name

>

>

*param-value

>

init-param

>

>

>

cors.allowed.methodsparam-name

>

>

get,post,head,options,putparam-value

>

init-param

>

>

>

cors.allowed.headersparam-name

>

>

content-type,x-requested-with,accept,origin,access-control-request-method,access-control-request-headersparam-value

>

init-param

>

>

>

cors.exposed.headersparam-name

>

>

access-control-allow-origin,access-control-allow-credentialsparam-value

>

init-param

>

>

>

cors.support.credentialsparam-name

>

>

trueparam-value

>

init-param

>

>

>

cors.preflight.maxageparam-name

>

>

10param-value

>

init-param

>

filter

>

>

>

corsfilterfilter-name

>

>

/*url-pattern

>

>

值得注意的是

我自己這個包的路徑卻一直沒有找到,儘管demo跨域是成功的,但是在idea編輯器裡還是顯示出路徑錯誤

這個就是我做的乙個成功跨域的demo的所有jar包,裡面看不出來有哪個包跟上面路徑是有關聯的,但能夠執行,還是暫時放下這個疑問了。

前後端分離跨域問題

access to xmlhttprequest at localhost 8081 user gettoken?username hyly password hyly from origin http localhost 63342 has been blocked by cors policy ...

前後端分離跨域問題

public class loginhandlerinterceptor implements handlerinterceptor response.sendredirect request.getcontextpath login.html catch ioexception e return ...

前後端分離跨域問題總結

跨域問題一般是後端處理,前端只需要在請求時追加,一般就能解決跨域問題。後端 新增corsfilter過濾器,實現filter介面 跨域請求配置 override public void dofilter servletrequest servletrequest,servletresponse se...