最近我已經介紹了智威湯遜驗證我的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);}服務器仍然抱怨找不到授權頭。我在這里想念任何東西嗎?感謝您的幫助
HTTP狀態代碼401,即使我在請求中發送憑據
aluckdog
2019-11-04 10:10:41