为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 920 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信