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

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

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

問題描述

我正在嘗試通過 Javascript 將授權令牌承載發(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 容器下運行,所以我正在獲取 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鍵添加標題

$.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

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

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

相關文檔推薦

Is Math.random() cryptographically secure?(Math.random() 在密碼學上是安全的嗎?)
Secure random numbers in javascript?(在javascript中保護隨機數(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 鏈接中的授權標頭)
主站蜘蛛池模板: 8x8ⅹ国产精品一区二区 | 欧日韩av | 99热精品在线 | 亚洲成人国产 | 久久网页| 欧美综合久久 | 国产午夜精品一区二区三区视频 | 嫩草国产 | 国产一区二区在线播放 | 老司机精品福利视频 | 日韩精品一区在线 | 亚洲一区二区免费视频 | 一区中文字幕 | 偷拍一区二区三区 | 亚洲一级精品 | 97国产在线视频 | 日日夜夜噜 | 欧美激情xxx | 一区二区三区精品 | 免费高清av| 午夜成人影片 | www.久久久久| www色| 成人免费在线播放 | 福利小视频 | 国产丝袜av | 天天干天天色天天射 | 国产欧美一区二区精品性色超碰 | 欧美一区二区在线观看 | 国产乱码精品一区二区三 | 中日韩毛片 | 国产一区二区网站 | 日韩av手机在线观看 | 亚洲欧美精品 | √8天堂资源地址中文在线 av网址在线免费观看 | 欧美午夜理伦三级在线观看 | 国产a精品 | 亚洲精品成人 | av片在线观看 | 欧美激情xxxx | 看片黄全部免费 |