久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何指定 CORS 的響應(yīng)標(biāo)頭?

How to specify response headers to CORS?(如何指定 CORS 的響應(yīng)標(biāo)頭?)
本文介紹了如何指定 CORS 的響應(yīng)標(biāo)頭?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在春季構(gòu)建一個(gè)后端 REST API,而我的朋友正在構(gòu)建一個(gè) Angular JS 前端應(yīng)用程序來(lái)調(diào)用我的 API.我有一個(gè)帶有鍵 Authorization 的令牌標(biāo)頭和一個(gè)可以訪問(wèn)的值服務(wù),否則它會(huì)拒絕.從郵遞員和 REST 客戶端我能夠接收 API,但是在測(cè)試時(shí)他說(shuō)他在預(yù)檢時(shí)收到 401 Unauthorized Error.下面是我的 doFilterInternal 方法.

I am building a backend REST API in spring and my friend is building a Angular JS front end app to call my API.I have a token header with key Authorization and a value which gives access to the service otherwise it refuses.From Postman and REST client I am able to receive the API but when tested he says he gets 401 Unauthorized Error on preflight.Below is my doFilterInternal method.

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers","Content-Type, Accept, X-Requested-With, Authorization");
}

但是當(dāng)他在 Angular JS 中使用令牌調(diào)用 API 時(shí),他得到了

But when he calls the API with the token in Angular JS he gets

所以我跟著這個(gè)答案這里 我添加了屬性

So I followed this answer here and I added the property

spring.mvc.dispatch-options-request=true

在 application.properties.But stillt 他的錯(cuò)誤似乎就像

in the application.properties.But stillt he error seems to be like

預(yù)檢響應(yīng)包含無(wú)效的 https 狀態(tài)代碼 401

感謝任何幫助.

推薦答案

這是避免預(yù)檢錯(cuò)誤的過(guò)濾器

Here is the filter which avoid the preflight error

        @Override
        protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws ServletException, IOException {
            LOG.info("Adding CORS Headers ........................");        
            res.setHeader("Access-Control-Allow-Origin", "*");
            res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
            res.setHeader("Access-Control-Max-Age", "3600");
            res.setHeader("Access-Control-Allow-Headers", "X-PINGOTHER,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization");
            res.addHeader("Access-Control-Expose-Headers", "xsrf-token");
            if ("OPTIONS".equals(req.getMethod())) {
             res.setStatus(HttpServletResponse.SC_OK);
            } else { 
             chain.doFilter(req, res);
            }        
        }

從帖子中找到它 Cross Origin Request Blocked Spring MVC Restful Angularjs

這篇關(guān)于如何指定 CORS 的響應(yīng)標(biāo)頭?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 欧美精品a∨在线观看不卡 国产精品久久国产精品 | 一级片免费网站 | 久久婷婷国产麻豆91 | 综合成人在线 | av激情在线 | 日韩视频中文字幕 | 中文字幕亚洲在线 | 亚洲视频三 | 久久久久国产一级毛片高清网站 | 欧美日韩国产高清视频 | 国产成人综合在线 | 色综合天天网 | 亚洲视屏 | gav成人免费播放视频 | 黄色毛片在线播放 | 免费性视频| 日本三级电影免费 | 久久免费视频在线 | 玖玖精品视频 | 精品国产31久久久久久 | 亚洲激情综合 | 日韩精品免费在线 | 欧美啪啪| 国产激情一区二区三区 | 日本精品视频一区二区三区四区 | 久久福利电影 | 四虎免费视频 | 超碰在线人人干 | 久久久久久免费毛片精品 | 91综合网| 在线观看视频一区二区三区 | 久久av一区 | 亚洲国产精品美女 | 亚洲精品一区二区三区 | 播放一级毛片 | 久久精品一二三影院 | 欧美日韩国产高清视频 | 亚洲国产一区二区视频 | av在线免费观看网址 | 成人网av| 国产在线中文 |