Spring Cloud項(xiàng)目前后端分離跨域的操作
跨域問題,其實(shí)百度上面有一堆的解決方案
針對(duì)普通的情況其實(shí)百度上面的方案都是可行的。
我這里主要介紹2種情況。
當(dāng)然我這里的配置都是基于網(wǎng)關(guān)的,而不是基于服務(wù)的。
1、沒有增加權(quán)限驗(yàn)證。
2、增加了spring security的權(quán)限驗(yàn)證(我這里是基于keyCloak),增加了Authorization
首先我們介紹第一種情況的解決方法,這個(gè)很簡單,只需要在啟動(dòng)類里面配置過濾器就可以解決。
@Bean public CorsFilter corsFilter() { //1.添加CORS配置信息 CorsConfiguration config = new CorsConfiguration(); //放行哪些原始域 config.addAllowedOrigin("*"); //是否發(fā)送Cookie信息 config.setAllowCredentials(true); //放行哪些原始域(請(qǐng)求方式) config.addAllowedMethod("*"); //放行哪些原始域(頭部信息) config.addAllowedHeader("*"); //暴露哪些頭部信息(因?yàn)榭缬蛟L問默認(rèn)不能獲取全部頭部信息) config.addExposedHeader("*"); //2.添加映射路徑 UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource(); configSource.registerCorsConfiguration("/**", config); //3.返回新的CorsFilter. return new CorsFilter(configSource); }
我遇到情況就是第二種了,這種情況上面的方式基本沒有作用,我這里使用的是keyCloak做的權(quán)限驗(yàn)證。
首先增加過濾器配置:
@Component public class CorsControllerFilter implements Filter{ @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletResponse res = (HttpServletResponse) response; res.setContentType("text/html;charset=UTF-8"); res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE ,PUT"); res.setHeader("Access-Control-Max-Age", "3600"); res.setHeader("Access-Control-Allow-Headers", "*"); res.setHeader("Access-Control-Allow-Credentials", "true"); res.setHeader("XDomainRequestAllowed", "1"); chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
在啟動(dòng)類中增加配置
@Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(); CorsControllerFilter corsControllerFilter = new CorsControllerFilter(); registrationBean.setFilter(corsControllerFilter); return registrationBean; }
但是針對(duì)某些請(qǐng)求,他會(huì)先請(qǐng)求OPTIONS請(qǐng)求,造成權(quán)限驗(yàn)證失敗。所以增加攔截器配置,對(duì)所有的OPTIONS的請(qǐng)求直接放行,返回200的狀態(tài)。
public class OptionsInterceptor implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // TODO Auto-generated method stub if(request.getMethod().equals("OPTIONS")){ response.setStatus(HttpServletResponse.SC_OK); return false; } return true; } }
配置web配置文件,加載攔截器。
@Configuration public class WebMvcConfiguration extends WebMvcConfigurationSupport{ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new OptionsInterceptor()).addPathPatterns("/**"); } }
本來以為這樣配置了應(yīng)該是可以了,但是在請(qǐng)求的時(shí)候OPTIONS的請(qǐng)求居然還是報(bào)跨域的問題,增加攔截器允許跨域配置
public class CrossInterceptor implements HandlerInterceptor{ @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // TODO Auto-generated method stub response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT, HEAD"); response.setHeader("Access-Control-Allow-Headers", "*"); response.setHeader("Access-Control-Max-Age", "3600"); return true; } }
在WebMvcConfiguration里面增加配置,注意要寫在OptionsInterceptor的前面
registry.addInterceptor(new CrossInterceptor()).addPathPatterns("/**");
繼續(xù)測試,跨域問題解決。對(duì)于原理其實(shí)我也不太清楚,歡迎各位溝通交流。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合Mybatis-Plus實(shí)現(xiàn)關(guān)聯(lián)查詢
Mybatis-Plus(簡稱MP)是一個(gè)Mybatis的增強(qiáng)工具,只是在Mybatis的基礎(chǔ)上做了增強(qiáng)卻不做改變,MyBatis-Plus支持所有Mybatis原生的特性,本文給大家介紹了SpringBoot整合Mybatis-Plus實(shí)現(xiàn)關(guān)聯(lián)查詢,需要的朋友可以參考下2024-08-08spring-cloud-gateway動(dòng)態(tài)路由的實(shí)現(xiàn)方法
這篇文章主要介紹了spring-cloud-gateway動(dòng)態(tài)路由的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01springboot詳解實(shí)現(xiàn)車險(xiǎn)理賠信息管理系統(tǒng)代碼
本系統(tǒng)基于Springboot開發(fā)實(shí)現(xiàn)了一個(gè)為用戶車險(xiǎn)進(jìn)行理賠信息管理的一個(gè)信息化管理系統(tǒng),核心的業(yè)務(wù)主要是用戶申請(qǐng)保險(xiǎn)理賠,管理員審核進(jìn)入理賠程序,事故調(diào)查員對(duì)事故進(jìn)行調(diào)查和現(xiàn)場勘察,這其中共涉及到三類用戶,購買保險(xiǎn)的客戶,事故調(diào)查員和系統(tǒng)管理員2022-06-06Java Arrays.sort和Collections.sort排序?qū)崿F(xiàn)原理解析
這篇文章主要介紹了Java Arrays.sort和Collections.sort排序?qū)崿F(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Java遞歸實(shí)現(xiàn)評(píng)論多級(jí)回復(fù)功能
這篇文章主要介紹了Java遞歸實(shí)現(xiàn)評(píng)論多級(jí)回復(fù)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06一文帶你熟練掌握J(rèn)ava中的日期時(shí)間相關(guān)類
我們在開發(fā)時(shí),除了數(shù)字、數(shù)學(xué)這樣的常用API之外,還有日期時(shí)間類,更是會(huì)被經(jīng)常使用,比如我們項(xiàng)目中必備的日志功能,需要記錄異常等信息產(chǎn)生的時(shí)間,本文就帶各位來學(xué)習(xí)一下相關(guān)的日期時(shí)間類有哪些2023-05-05Java使用JDBC連接postgresql數(shù)據(jù)庫示例
這篇文章主要介紹了Java使用JDBC連接postgresql數(shù)據(jù)庫,結(jié)合實(shí)例形式分析了jdbc連接postgresql數(shù)據(jù)庫及數(shù)值插入、更新、查詢等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01