Spring Boot實(shí)現(xiàn)跨域訪問(wèn)實(shí)現(xiàn)代碼
當(dāng)前使用spring版本是4.3.9
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class CorsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setHeader("Access-Control-Allow-Origin", request.getHeader("origin"));
// response.setHeader("Access-Control-Allow-Origin", "*");//允許跨域訪問(wèn)的域
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");//允許使用的請(qǐng)求方法,以逗號(hào)隔開(kāi)
response.setHeader("Access-Control-Max-Age", "3600");// 緩存此次請(qǐng)求的秒數(shù)
//允許使用的請(qǐng)求方法,以逗號(hào)隔開(kāi)
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Cache-Control,Pragma,Content-Type,Token");
response.setHeader("Access-Control-Allow-Credentials","true");//是否允許請(qǐng)求帶有驗(yàn)證信息
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
PS: spring boot 服務(wù)器端設(shè)置允許跨域訪問(wèn)
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
*
* 跨域過(guò)濾器
* @author meng
* @version
* @since 2016年6月19日
*/
@Component
public class CorsFilter implements Filter {
final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CorsFilter.class);
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");
System.out.println("*********************************過(guò)濾器被使用**************************");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
2017-04-13更新:
第二種方法,在Appplication.java添加:
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
return corsConfiguration;
}
/**
* 跨域過(guò)濾器
* @return
*/
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig()); // 4
return new CorsFilter(source);
}
總結(jié)
以上所述是小編給大家介紹的Spring Boot實(shí)現(xiàn)跨域訪問(wèn)實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- SpringBoot解決ajax跨域問(wèn)題的方法
- Spring boot 總結(jié)之跨域處理cors的方法
- vue+springboot前后端分離實(shí)現(xiàn)單點(diǎn)登錄跨域問(wèn)題解決方法
- Spring boot跨域設(shè)置實(shí)例詳解
- 淺談spring-boot 允許接口跨域并實(shí)現(xiàn)攔截(CORS)
- Spring Boot Web應(yīng)用開(kāi)發(fā) CORS 跨域請(qǐng)求支持
- spring boot配合前端實(shí)現(xiàn)跨域請(qǐng)求訪問(wèn)
- Java Spring boot 2.0 跨域問(wèn)題的解決
相關(guān)文章
基于Listener監(jiān)聽(tīng)器生命周期(詳解)
下面小編就為大家?guī)?lái)一篇基于Listener監(jiān)聽(tīng)器生命周期(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Spring-data-redis操作redis知識(shí)總結(jié)
這篇文章主要介紹了Spring-data-redis操作redis知識(shí)總結(jié),spring-data-redis是spring-data模塊的一部分,專(zhuān)門(mén)用來(lái)支持在spring管理項(xiàng)目對(duì)redis的操作。2017-04-04
IntelliJ IDEA2019 安裝lombok的實(shí)現(xiàn)
這篇文章主要介紹了IntelliJ IDEA2019 安裝lombok的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
例舉fastJson和jackson轉(zhuǎn)json的區(qū)別
今天小編就為大家分享一篇關(guān)于例舉fastJson和jackson轉(zhuǎn)json的區(qū)別,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
MyBatis常見(jiàn)報(bào)錯(cuò)問(wèn)題及解決方案
這篇文章主要介紹了MyBatis常見(jiàn)報(bào)錯(cuò)問(wèn)題及解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
springboot接受前端請(qǐng)求的方法實(shí)現(xiàn)
本文主要介紹了springboot接受前端請(qǐng)求的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

