在Angular 8中,通过正确的令牌请求头传递后,收到403禁止访问的错误。

huangapple go评论59阅读模式
英文:

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

问题

我正在使用GET方法在头部发送4个数据(id,token,ROLE,EMPCODE),但我遇到了错误。

> 错误 HttpErrorResponse {headers: HttpHeaders,状态:403,状态文本:"禁止"}

我的token是正确的,我在Postman中进行了检查,并且获得了状态200的响应,但是在我的项目中遇到了这个问题。

我正在分享我的服务文件代码

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 });
}

我正在分享我的订阅获取响应的组件代码

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

> 当我在浏览器的网络头部检查时,我可以看到我发送的请求头部没有传递。

英文:

I am using get method to send 4 data (id , token , ROLE , EMPCODE) in header ,

i am getting error

> ERROR HttpErrorResponse {headers: HttpHeaders, status: 403,
> statusText: "Forbidden"

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

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 .

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 .

答案1

得分: 1

可以有很多不清楚的地方,比如我们不知道如何获取令牌,如何生成它等等。

通常,您需要在授权头部发送身份验证信息的类型。例如,Bearer 令牌或其他类型的令牌,像这样:"bearer {your_token}"。

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

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. For example a bearer token or something. Like this "bearer {your_token}".

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

答案2

得分: 0

根据这个问题

尝试按照以下方式添加头部:

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

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');

答案3

得分: 0

你还可以像这样传递多个标头:

return this.http.get(this.emp_data, 
  {headers: 
    { 'id': id, 'TOKEN': token, 'ROLE': role, 'EMPCODE': employeeCode, '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'}
  });

答案4

得分: 0

在我的项目中,我也遇到了相同的问题:

问题是服务器没有处理新请求/跨域请求。在Spring Boot微服务的后端,我实现了WebMvcConfigurer以启用跨域:

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

这也有帮助:["status": 403, "error": "Forbidden", "message": "Forbidden", "path": "/post/create"]。

英文:

I had same problem in my project too :

在Angular 8中,通过正确的令牌请求头传递后,收到403禁止访问的错误。

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

@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”

huangapple
  • 本文由 发表于 2020年1月6日 14:39:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定