久久久久久久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 中的組件周?chē)h(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(使用線程逐塊處理文件)
主站蜘蛛池模板: 久久电影一区 | 不卡一区| 中国毛片免费 | www.色综合 | 精品国产乱码久久久久久果冻传媒 | 国产黄色电影 | 国产欧美日韩久久久 | 国产一区二区高清在线 | 精品日韩在线观看 | 欧美xxxx黑人又粗又长 | 一区二区三区日韩 | 日韩免费一区二区 | 我要看免费一级毛片 | 精品亚洲永久免费精品 | 日韩精品一区二 | 成人精品在线观看 | 亚洲午夜av久久乱码 | 国产精品美女一区二区 | 日韩久久久久久 | 日本一道本视频 | 亚洲国产成人精品女人久久久 | 日本精品一区二区三区四区 | 国产精品乱码一二三区的特点 | 中文字幕免费在线 | 精品国产欧美一区二区三区不卡 | 91在线免费视频 | 亚洲精品日韩在线 | 欧美亚洲国产一区二区三区 | 国产精品亚洲片在线播放 | 黄免费观看视频 | 色片在线观看 | 伊人久久精品一区二区三区 | 欧美性受xxxx白人性爽 | 久久新 | 日韩综合在线 | 久久久九九九九 | 国产精品成人一区二区 | 嫩草影院黄 | 九色视频网站 | 国产精品久久久久久久久久久久 | 亚洲+变态+欧美+另类+精品 |