简体   繁体   English

在 Angular 8 的标头中传递正确的令牌请求时被禁止 403,

[英]getting 403 forbidden on passing correct token request in header in Angular 8 ,

I am using get method to send 4 data (id , token , ROLE , EMPCODE) in header ,我正在使用 get 方法在 header 中发送 4 个数据(id、token、ROLE、EMPCODE),

i am getting error我收到错误

ERROR HttpErrorResponse {headers: HttpHeaders, status: 403, statusText: "Forbidden"错误 HttpErrorResponse {标头:HttpHeaders,状态:403,状态文本:“禁止”

my token is correct , it I checked in Postman , and I am getting response with status 200 , but I am facing this issue in my Project我的令牌是正确的,我在 Postman 中检查过,我收到状态为 200 的响应,但我在我的项目中遇到了这个问题

I am sharing my code for service file我正在分享我的服务文件代码

employeeData( id ,   token , role , employeeCode){
 let headers = new HttpHeaders();
  headers.append('id', id);
  headers.append('TOKEN', token);
  headers.append('ROLE', role);
  headers.append('EMPCODE' , employeeCode);
  headers.append( 'Content-Type' ,  'application/json');


  return this.http.get(this.emp_data, {headers: headers});

}

I am sharing code from Component where I have subscribed for getting response .我正在从订阅获取响应的 Component 共享代码。

viewdetails(){

     this.rest.employeeData(this.userId,this.token,this.role, this.employeeCode).subscribe(
      result => {
        console.log('hello');
        console.log(result);
      })
}

When I checked in Browser's Network Header , I can check Header that I am passing on request is not passed there .当我签入浏览器的网络标头时,我可以检查我在请求中传递的标头没有通过那里。

Can be a lot going on here the we don't know like how you get the token, how you generate it etc.这里可能会发生很多事情,我们不知道你如何获得令牌,如何生成它等。

Usually you need to send in the Authorization header what kind of Auth it is.通常您需要在 Authorization 标头中发送它是什么类型的 Auth。 For example a bearer token or something.例如不记名令牌或其他东西。 Like this "bearer {your_token}".就像这个“持有者 {your_token}”。

headers.append('Authorization', 'bearer ' + token);

as per this question ,根据这个问题

try adding headers as follows,尝试按如下方式添加标题,

let headers = new HttpHeaders();
headers = headers.set('id', id).set('TOKEN', token).set('ROLE', role).set('EMPCODE' , employeeCode).set( 'Content-Type' ,  'application/json');

You can also multiple headers pass like this:您还可以像这样传递多个标头:

return this.http.get(this.emp_data, 
  {headers: 
    { 'id':id, 'TOKEN':token, 'ROLE':role, 'EMPCODE': employeeCode, 'Content-Type': 'application/json'}
  });

I had same problem in my project too :我的项目中也有同样的问题:

在此处输入图像描述

The problem was the server not handling the new requests/Cross origin requests.问题是服务器没有处理新请求/跨源请求。 On backend of spring-boot micro service I have implemented the WebMvcConfigurer to enable crossOrigin在 spring-boot 微服务的后端,我实现了 WebMvcConfigurer 以启用 crossOrigin

@Configuration
@EnableWebMvc
public class CorsConfiguration implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("GET", "POST")
                .allowCredentials(true).maxAge(3600);
    }
}

This helped as well “status”: 403, “error”: “Forbidden”, “message”: “Forbidden”, “path”: “/post/create”这也有帮助“状态”:403,“错误”:“禁止”,“消息”:“禁止”,“路径”:“/post/create”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM