Springboot 實(shí)現(xiàn)跨域訪問無需使用jsonp的實(shí)現(xiàn)代碼
更新時(shí)間:2018年09月30日 10:10:33 作者:qq_39628595
這篇文章主要介紹了Springboot 實(shí)現(xiàn)跨域訪問 無需使用jsonp的實(shí)現(xiàn)代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
Springboot 實(shí)現(xiàn)跨域訪問 無需使用jsonp
在springboot的攔截器中添加respone的頭信息即可
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //String origin = (String) request.getRemoteHost()+":"+request.getRemotePort(); response.addHeader("Access-Control-Allow-Origin", "*"); //System.out.println("Access-Control-Allow-Origin"); response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); //System.out.println("Access-Control-Allow-Methods"); response.addHeader("Access-Control-Max-Age", "3600"); //System.out.println("Access-Control-Max-Age"); response.addHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization"); //System.out.println("Access-Control-Allow-Headers"); response.addHeader("Access-Control-Allow-Credentials","true"); //System.out.println("Access-Control-Allow-Credentials"); String api_key = request.getParameter("api_key"); String api_secret = request.getParameter("api_secret"); ; if (check(api_key,api_secret)){ return true; } response.sendError(400,"api_key or api_secret are error"); return false; } @Configuration public class MyWebMvcConfigurer implements WebMvcConfigurer { @Bean APIIntercepter apiIntercepter() { return new APIIntercepter(); } @Override public void addInterceptors(InterceptorRegistry registry) { // 添加一個(gè)攔截器,連接以/v1為前綴的 url路徑 registry.addInterceptor(loginIntercepter()).addPathPatterns("/admin/**"); registry.addInterceptor(apiIntercepter()).addPathPatterns("/v1/**"); } }
總結(jié)
以上所述是小編給大家介紹的Springboot 實(shí)現(xiàn)跨域訪問無需使用jsonp的實(shí)現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
基于指針pointers和引用references的區(qū)別分析
本篇文章介紹了,基于指針pointers和引用references的區(qū)別分析。需要的朋友參考下2013-05-05Java計(jì)時(shí)新姿勢StopWatch的使用方法詳解
這篇文章主要給大家介紹了關(guān)于Java計(jì)時(shí)新姿勢StopWatch的相關(guān)資料,以及java 中使用StopWatch來計(jì)算時(shí)間差的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01Jenkins如何實(shí)現(xiàn)自動打包部署linux
這篇文章主要介紹了Jenkins如何實(shí)現(xiàn)自動打包部署linux,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Java多態(tài)中的向上轉(zhuǎn)型與向下轉(zhuǎn)型淺析
多態(tài)是指不同類的對象在調(diào)用同一個(gè)方法是所呈現(xiàn)出的多種不同行為,下面這篇文章主要給大家介紹了關(guān)于Java多態(tài)中向上轉(zhuǎn)型與向下轉(zhuǎn)型的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02