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

通過 Javascript 發(fā)送 Authorization Token Bearer

Sending Authorization Token Bearer through Javascript(通過 Javascript 發(fā)送 Authorization Token Bearer)
本文介紹了通過 Javascript 發(fā)送 Authorization Token Bearer的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試通過 Javascript 將授權(quán)令牌承載發(fā)送到 REST 端點,所以我這樣做:

I'm trying to send a Authorization Token Bearer through Javascript to a REST Endpoint, so i doing in this way:

$.ajax( {
    url: 'http://localhost:8080/resourceserver/protected-no-scope',
    type: 'GET',
    beforeSend : function( xhr ) {
        xhr.setRequestHeader( "Authorization", "Bearer " + token );
    },
    success: function( response ) {
        console.log(response);
    }

我的端點在 SpringBoot 容器下運(yùn)行,所以我正在獲取 HttpServletRequest 并嘗試獲取 AUthorization Header 但始終為空:

My endpoint is running under a SpringBoot container, so i'm getting the HttpServletRequest and trying to get AUthorization Header but is always null:

static Authentication getAuthentication(HttpServletRequest request) {
        String token = request.getHeader(HEADER_STRING);
        //token is always null
...

編輯 1這是客戶端(瀏覽器)中的錯誤

Edit 1 This is the error in Client-Side (Browser

OPTIONS http://localhost:8080/resourceserver/protected-no-scope 403 ()
Failed to load http://localhost:8080/resourceserver/protected-no-scope: Response for preflight has invalid HTTP status code 403.

編輯 2為了在后端啟用 CORS,我在 spring 中使用了以下注釋:

Edit 2 To enable CORS in backend i'm using the following annotation with spring:

@RestController
@CrossOrigin(origins = "*", maxAge = 3600, allowCredentials = "true", allowedHeaders = "Authorization", methods =
        {RequestMethod.GET, RequestMethod.OPTIONS, RequestMethod.POST})
public class MyResource {

編輯 3我嘗試在我的過濾器中添加 CORS,但沒有成功:

Edit 3 I tried added the CORS in my Filter but no success:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;

        httpServletResponse.setHeader("Access-Control-Allow-Origin", httpServletRequest.getHeader("Origin"));
        httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        httpServletResponse.setHeader("Access-Control-Max-Age", "3600");
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");


        Authentication authentication = TokenAuthenticationService
                .getAuthentication(httpServletRequest);

        SecurityContextHolder.getContext().setAuthentication(authentication);
        filterChain.doFilter(request, response);
    }

推薦答案

您可以使用headers鍵添加標(biāo)題

$.ajax({
   url: 'http://localhost:8080/resourceserver/protected-no-scope',
   type: 'GET',
   contentType: 'application/json'
   headers: {
      'Authorization': 'Bearer <token>'
   },
   success: function (result) {
       // CallBack(result);
   },
   error: function (error) {

   }
});

您需要在后端啟用 CORS

You need to enable CORS on backend

https://stackoverflow.com/a/32320294/5567387

這篇關(guān)于通過 Javascript 發(fā)送 Authorization Token Bearer的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Is Math.random() cryptographically secure?(Math.random() 在密碼學(xué)上是安全的嗎?)
Secure random numbers in javascript?(在javascript中保護(hù)隨機(jī)數(shù)?)
How to avoid multiple token refresh requests when making simultaneous API requests with an expired token(使用過期令牌發(fā)出同時 API 請求時如何避免多個令牌刷新請求)
JWT not decoding quot;JWT malformedquot; - Node Angular(JWT 未解碼“JWT malformed;- 節(jié)點角度)
How to invalidate a JWT token with no expiry time(如何使沒有到期時間的 JWT 令牌無效)
Authorization header in img src link(img src 鏈接中的授權(quán)標(biāo)頭)
主站蜘蛛池模板: 1级黄色大片 | 亚洲国产情侣 | 国产91在线 | 亚洲 | 国产成人综合一区二区三区 | av电影手机版 | 国产免费一区 | 91精品国产日韩91久久久久久 | 国产精品一区二区av | 日韩在线观看网站 | 久久国产高清视频 | 特级特黄特色的免费大片 | 男人天堂av网 | 欧美国产一区二区三区 | 成人网av| 久久免费看 | 国产精品视频一区二区三区 | 91免费看片 | 99久久久国产精品 | 欧美成人一区二区三区 | 国产精品视频www | 在线免费看毛片 | 天堂色网 | 中文在线一区二区 | 日韩中文字幕一区二区 | 一区二区三区免费观看 | 午夜亚洲 | 欧美激情亚洲天堂 | 日韩欧美不卡 | 亚洲精品电影在线观看 | 精品免费国产一区二区三区四区 | 国产一区二区在线免费观看 | 日韩精品在线视频 | 在线观看av不卡 | 81精品国产乱码久久久久久 | 亚洲精品第一国产综合野 | 久久大香| 国产激情视频网站 | 久久天堂 | av性色全交蜜桃成熟时 | 人人玩人人干 | av黄色国产 |