當(dāng)前使用spring版本是4.3.9
讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、網(wǎng)頁(yè)空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、德安網(wǎng)站維護(hù)、網(wǎng)站推廣。
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", "*");//允許跨域訪(fǎng)問(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è)置允許跨域訪(fǎng)問(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)跨域訪(fǎng)問(wèn)實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!
本文題目:SpringBoot實(shí)現(xiàn)跨域訪(fǎng)問(wèn)實(shí)現(xiàn)代碼
當(dāng)前網(wǎng)址:http://www.chinadenli.net/article38/jcodsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、虛擬主機(jī)、面包屑導(dǎo)航、小程序開(kāi)發(fā)、、App開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)