国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁 > news >正文

php網(wǎng)站開發(fā)案例論文搜狗seo刷排名軟件

php網(wǎng)站開發(fā)案例論文,搜狗seo刷排名軟件,網(wǎng)站開發(fā)地址,做360手機(jī)網(wǎng)站快文章目錄 SpringBoot 項(xiàng)目中后端實(shí)現(xiàn)跨域的5種方式!!!一、為什么會出現(xiàn)跨域問題二、什么是跨域三、非同源限制四、Java后端 實(shí)現(xiàn) CORS 跨域請求的方式1、返回新的 CorsFilter(全局跨域)2、重寫 WebMvcConfigurer(全局跨域)3、使用注解 (局部跨…

文章目錄

  • SpringBoot 項(xiàng)目中后端實(shí)現(xiàn)跨域的5種方式!!!
    • 一、為什么會出現(xiàn)跨域問題
    • 二、什么是跨域
    • 三、非同源限制
    • 四、Java后端 實(shí)現(xiàn) CORS 跨域請求的方式
      • 1、返回新的 CorsFilter(全局跨域)
      • 2、重寫 WebMvcConfigurer(全局跨域)
      • 3、使用注解 (局部跨域)
      • 4、手動設(shè)置響應(yīng)頭(局部跨域)
      • 5、使用自定義filter實(shí)現(xiàn)跨域

SpringBoot 項(xiàng)目中后端實(shí)現(xiàn)跨域的5種方式!!!

一、為什么會出現(xiàn)跨域問題

出于瀏覽器的同源策略限制。同源策略(Sameoriginpolicy)是一種約定,它是瀏覽器最核心也最基本的安全功能,如果缺少了同源策略,則瀏覽器的正常功能可能都會受到影響??梢哉fWeb是構(gòu)建在同源策略基礎(chǔ)之上的,瀏覽器只是針對同源策略的一種實(shí)現(xiàn)。

同源策略
同源策略會阻止一個域的javascript腳本和另外一個域的內(nèi)容進(jìn)行交互。所謂同源(即指在同一個域)就是兩個頁面具有相同的協(xié)議(protocol),主機(jī)(host)和端口號(port)

二、什么是跨域

舉例說明:

當(dāng)一個請求url的協(xié)議、域名、端口三者之間任意一個與當(dāng)前頁面url不同即為跨域
在這里插入圖片描述

三、非同源限制

【1】無法讀取非同源網(wǎng)頁的 Cookie、LocalStorage 和 IndexedDB

【2】無法接觸非同源網(wǎng)頁的 DOM

【3】無法向非同源地址發(fā)送 AJAX 請求

四、Java后端 實(shí)現(xiàn) CORS 跨域請求的方式

對于 CORS的跨域請求,主要有以下幾種方式可供選擇:

1、返回新的CorsFilter
2、重寫 WebMvcConfigurer
3、使用注解 @CrossOrigin
4、手動設(shè)置響應(yīng)頭 (HttpServletResponse)
5、自定web filter 實(shí)現(xiàn)跨域
注意

  • CorFilter / WebMvConfigurer / @CrossOrigin 需要 SpringMVC 4.2以上版本才支持,對應(yīng)springBoot 1.3版本以上
  • 上面前兩種方式屬于全局 CORS 配置,后兩種屬于局部 CORS配置。如果使用了局部跨域是會覆蓋全局跨域的規(guī)則,所以可以通過 @CrossOrigin 注解來進(jìn)行細(xì)粒度更高的跨域資源控制。
  • 其實(shí)無論哪種方案,最終目的都是修改響應(yīng)頭,向響應(yīng)頭中添加瀏覽器所要求的數(shù)據(jù),進(jìn)而實(shí)現(xiàn)跨域

1、返回新的 CorsFilter(全局跨域)

在任意配置類,返回一個 新的 CorsFIlter Bean ,并添加映射路徑和具體的CORS配置路徑。

@Configuration
public class GlobalCorsConfig {@Beanpublic CorsFilter corsFilter() {//1. 添加 CORS配置信息CorsConfiguration config = new CorsConfiguration();//放行哪些原始域config.addAllowedOrigin("*");//是否發(fā)送 Cookieconfig.setAllowCredentials(true);//放行哪些請求方式config.addAllowedMethod("*");//放行哪些原始請求頭部信息config.addAllowedHeader("*");//暴露哪些頭部信息config.addExposedHeader("*");//2. 添加映射路徑UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();corsConfigurationSource.registerCorsConfiguration("/**",config);//3. 返回新的CorsFilterreturn new CorsFilter(corsConfigurationSource);}
}

2、重寫 WebMvcConfigurer(全局跨域)

@Configuration
public class CorsConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**")//是否發(fā)送Cookie.allowCredentials(true)//放行哪些原始域.allowedOrigins("*").allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"}).allowedHeaders("*").exposedHeaders("*");}
}

3、使用注解 (局部跨域)

在控制器(類上)上使用注解 @CrossOrigin:,表示該類的所有方法允許跨域。

@RestController
@CrossOrigin(origins = "*")
public class HelloController {@RequestMapping("/hello")public String hello() {return "hello world";}
}

在方法上使用注解 @CrossOrigin:

@RequestMapping("/hello")@CrossOrigin(origins = "*")//@CrossOrigin(value = "http://localhost:8081") //指定具體ip允許跨域public String hello() {return "hello world";}

4、手動設(shè)置響應(yīng)頭(局部跨域)

使用 HttpServletResponse 對象添加響應(yīng)頭(Access-Control-Allow-Origin)來授權(quán)原始域,這里 Origin的值也可以設(shè)置為 “*”,表示全部放行。

@RequestMapping("/index")
public String index(HttpServletResponse response) {response.addHeader("Access-Allow-Control-Origin","*");return "index";
}

5、使用自定義filter實(shí)現(xiàn)跨域

ssm的寫法
首先編寫一個過濾器,可以起名字為MyCorsFilter.java

package cn.fpl.aop;import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
@Component
public class MyCorsFilter implements Filter {public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {HttpServletResponse response = (HttpServletResponse) res;response.setHeader("Access-Control-Allow-Origin", "*");response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");response.setHeader("Access-Control-Max-Age", "3600");response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");chain.doFilter(req, res);}public void init(FilterConfig filterConfig) {}public void destroy() {}
}

在web.xml中配置這個過濾器,使其生效

<!-- 跨域訪問 START-->
<filter><filter-name>CorsFilter</filter-name><filter-class>cn.fpl.aop.MyCorsFilter</filter-class>
</filter>
<filter-mapping><filter-name>CorsFilter</filter-name><url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 跨域訪問 END  -->

springboot可以簡化

import org.springframework.context.annotation.Configuration;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(filterName = "CorsFilter ")
@Configuration
public class CorsFilter implements Filter {@Overridepublic void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {HttpServletResponse response = (HttpServletResponse) res;response.setHeader("Access-Control-Allow-Origin","*");response.setHeader("Access-Control-Allow-Credentials", "true");response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");response.setHeader("Access-Control-Max-Age", "3600");response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");chain.doFilter(req, res);}
}
http://aloenet.com.cn/news/46928.html

相關(guān)文章:

  • 鄭州網(wǎng)站推廣公司排名武漢百度百科
  • 東莞公司網(wǎng)站策劃怎么建立網(wǎng)站
  • 赤峰網(wǎng)站建設(shè)培訓(xùn)app制作
  • 網(wǎng)站設(shè)計(jì)方案和技巧網(wǎng)絡(luò)暴力事件
  • 成都醫(yī)院做網(wǎng)站建設(shè)太原seo排名收費(fèi)
  • 日木女人做爰視頻網(wǎng)站淘寶搜索關(guān)鍵詞排名
  • 做網(wǎng)站的電腦最好的免費(fèi)建站網(wǎng)站
  • c#網(wǎng)站開發(fā)案例源碼app如何推廣
  • 哪里做網(wǎng)站做得好網(wǎng)站怎么做優(yōu)化排名
  • 公司制作網(wǎng)站價格長春最新發(fā)布信息
  • 定制型網(wǎng)站制作明細(xì)報價表百度應(yīng)用中心
  • 東坑網(wǎng)頁設(shè)計(jì)seo技巧
  • 做外貿(mào)要自己建網(wǎng)站嗎網(wǎng)頁免費(fèi)制作網(wǎng)站
  • 桂林賣手機(jī)網(wǎng)站seo網(wǎng)站優(yōu)化快速排名軟件
  • 市場營銷的八個理論seo系統(tǒng)培訓(xùn)課程
  • 做外貿(mào)對學(xué)歷要求高嗎seo經(jīng)典案例分析
  • 南京本地網(wǎng)站建設(shè)視頻專用客戶端app
  • wordpress 圖片鏈接下載成都seo整站
  • 國內(nèi)外做gif的網(wǎng)站網(wǎng)絡(luò)營銷推廣的方式
  • 湘潭學(xué)校網(wǎng)站建設(shè) 磐石網(wǎng)絡(luò)專注整合營銷傳播成功案例
  • 徐州方案公示在哪個網(wǎng)站西地那非片吃了多久會硬起來
  • 松江做營銷網(wǎng)站開封網(wǎng)絡(luò)推廣哪家好
  • 中文域名注冊報價表網(wǎng)站優(yōu)化怎么操作
  • 網(wǎng)站建設(shè)優(yōu)化推廣教程今日新聞大事件
  • 海外產(chǎn)品網(wǎng)站建設(shè)上海網(wǎng)絡(luò)推廣聯(lián)盟
  • 做外貿(mào)網(wǎng)站要多少錢國外免費(fèi)網(wǎng)站服務(wù)器
  • 官方網(wǎng)站內(nèi)容更新需要怎么做建站之星
  • 人民南路建設(shè)廳網(wǎng)站咨詢電話營銷網(wǎng)站的宣傳、推廣與運(yùn)作
  • 淘寶客為什么做網(wǎng)站東莞疫情最新情況
  • 哪個網(wǎng)站做視頻有錢掙長春網(wǎng)站提升排名