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

通過 Javascript 發送 Authorization Token Bearer

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

問題描述

我正在嘗試通過 Javascript 將授權令牌承載發送到 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 發送 Authorization Token Bearer的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Is Math.random() cryptographically secure?(Math.random() 在密碼學上是安全的嗎?)
Secure random numbers in javascript?(在javascript中保護隨機數?)
How to avoid multiple token refresh requests when making simultaneous API requests with an expired token(使用過期令牌發出同時 API 請求時如何避免多個令牌刷新請求)
JWT not decoding quot;JWT malformedquot; - Node Angular(JWT 未解碼“JWT malformed;- 節點角度)
How to invalidate a JWT token with no expiry time(如何使沒有到期時間的 JWT 令牌無效)
Authorization header in img src link(img src 鏈接中的授權標頭)
主站蜘蛛池模板: 中文字幕第一页在线 | 国产精品日韩在线观看 | 午夜久久 | 中文字幕人成人 | 亚洲视频一区在线 | 欧美大片黄 | 福利视频网址 | 亚洲精品福利视频 | 影音先锋欧美资源 | 国产精品亚洲视频 | 九九精品影院 | 亚洲精品欧洲 | 日本在线免费看最新的电影 | 喷潮网站| 老司机深夜福利网站 | 国产精品久久久爽爽爽麻豆色哟哟 | 国产精品久久久久久久久久久免费看 | 久草新在线| 国产精品永久在线观看 | a欧美| 国产精品人人做人人爽 | 中文字幕乱码视频32 | 亚洲a一区 | 日韩免费视频一区二区 | 午夜羞羞| 国产成年人视频 | 欧美一级黄色免费看 | 亚洲精品欧美一区二区三区 | 免费久久久久久 | 国产美女在线播放 | 在线欧美日韩 | 久久99精品视频 | 欧美成ee人免费视频 | 人成在线视频 | 日韩中文字幕一区 | 1204国产成人精品视频 | 黄色免费网址大全 | 99精品免费久久久久久日本 | 天天色天天射天天干 | 亚洲一区二区三区四区五区午夜 | 日韩中文字幕在线观看 |