亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

HTTP狀態代碼401,即使我在請求中發送憑據

HTTP狀態代碼401,即使我在請求中發送憑據

aluckdog 2019-11-04 10:10:41
最近我已經介紹了智威湯遜驗證我的Springboot和基于Angualr2應用。在There中,我嘗試通過在Angualr代碼中傳遞JWT令牌來發出POST請求save(jobId: number, taskId: number, note: Note) {   return this.http.post(environment.APIENDPOINT + '/jobs/' + jobId + '/tasks/' + taskId + '/notes', note, this.setHeaders()).map((response: Response) => response.json());}private setHeaders() {        // create authorization header with jwt token        let currentUser = JSON.parse(localStorage.getItem('currentUser'));        console.log("Current token---"+ currentUser.token);        if (currentUser && currentUser.token) {  let headers = new Headers();  headers.append('Content-Type', 'application/json');  headers.append('authorization','Bearer '+ currentUser.token);               let r = new RequestOptions({ headers: headers })   return r;        }    }但是在服務器端,它返回狀態碼401。問題是在Springboot端,它如下所示檢查授權標頭,并返回nullString authToken = request.getHeader("authorization ");然后,我看了一眼請求頭,它具有在訪問控制請求報頭的授權頭下面。但是它對服務器端不可見。然后,我進一步閱讀,發現這可能與CORS配置有關。所以我修改了我的CORS配置過濾器,使其具有addExposedHeader,如下所示@Beanpublic CorsFilter corsFilter() {    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();    CorsConfiguration config = new CorsConfiguration();    config.setAllowCredentials(true);    config.addAllowedOrigin("*");    config.addAllowedHeader("*");    config.addExposedHeader("authorization");    config.addAllowedMethod("OPTIONS");    config.addAllowedMethod("GET");    config.addAllowedMethod("POST");    config.addAllowedMethod("PUT");    config.addAllowedMethod("DELETE");    //config.addExposedHeader("Content-Type");    source.registerCorsConfiguration("/**", config);    return new CorsFilter(source);}服務器仍然抱怨找不到授權頭。我在這里想念任何東西嗎?感謝您的幫助
查看完整描述

2 回答

  • 2 回答
  • 0 關注
  • 1023 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號