在Angular 8中的标头中传递正确的令牌请求时禁止获取403,
我正在使用get方法发送头中的4个数据(id、令牌、角色、EMPCODE) 我收到错误在Angular 8中的标头中传递正确的令牌请求时禁止获取403,,angular,angular8,angular-httpclient,angular-http,Angular,Angular8,Angular Httpclient,Angular Http,我正在使用get方法发送头中的4个数据(id、令牌、角色、EMPCODE) 我收到错误 employeeData( id , token , role , employeeCode){ let headers = new HttpHeaders(); headers.append('id', id); headers.append('TOKEN', token); headers.append('ROLE', role); headers.append('EMPCODE'
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});
}
错误HttpErrorResponse{headers:HttpHeaders,状态:403,
状态文本:“禁止”
我的代币是正确的,我签了邮递员,我收到了状态为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);
})
}
当我签入浏览器的网络头时,我可以检查我正在传递的请求的头是否没有传递到那里。
viewdetails(){
this.rest.employeeData(this.userId,this.token,this.role, this.employeeCode).subscribe(
result => {
console.log('hello');
console.log(result);
})
}
这里可能会发生很多我们不知道的事情,比如你如何获得代币,如何生成代币等等 通常,您需要在授权头中发送它是什么类型的身份验证。例如,承载令牌或类似于“承载{your_token}”的东西 根据, 尝试按如下方式添加标题
let headers = new HttpHeaders();
headers = headers.set('id', id).set('TOKEN', token).set('ROLE', role).set('EMPCODE' , employeeCode).set( 'Content-Type' , 'application/json');
您还可以按如下方式传递多个标头:
return this.http.get(this.emp_data,
{headers:
{ 'id':id, 'TOKEN':token, 'ROLE':role, 'EMPCODE': employeeCode, 'Content-Type': 'application/json'}
});
我在我的项目中也遇到了同样的问题: 问题是服务器没有处理新请求/跨源请求。在spring boot micro服务的后端,我实现了WebMVCConfiguer以启用跨源请求
@Configuration
@EnableWebMvc
public class CorsConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedHeaders("*")
.allowedMethods("GET", "POST")
.allowCredentials(true).maxAge(3600);
}
}
这也有帮助这意味着您的令牌无效。发送请求之前,请检查令牌或使用有效凭据创建一个新令牌。@Channa我的令牌是正确的,如果我签入了邮递员,当我签入浏览器的网络标头时,我可以检查标头,我正在传递的请求未传递到该标头。如果您的令牌有效,其从postman开始工作,您使用的是Angular中的相同令牌,然后尝试在标头中传递userAgent。如果有cloudfare,则当userAgent未在标头中传递时,它可能会抛出403。postman默认情况下会将其添加到请求的标头。方法是get,我已在postman中检查响应,并且该响应将包含相同的数据我提供的,但您的授权头在哪里?我对授权头了解不多,您能详细说明一下吗。据我所知,我在头中传递的数据仅用于授权,而不用于承载,令牌也用于其他具有相同基本URLOk的API,所以您可能正在使用自定义头令牌的er。通常在标准的“授权”标头中使用。如果未显示令牌,则需要对其进行调试,并查看您是否确实在请求中获得令牌仍然获得{“messagecode”:403,“message”:“not valid token!”}{“messagecode”:403,“message”:“not valid token!”}