简体   繁体   English

后端Angular 2 + Spring Boot中未收到授权标头

[英]Authorization header is not receiving in the backend Angular 2 + Spring Boot

I am trying to pass the authorization header in the http request : 我正在尝试在http请求中传递授权标头:

My Angular Code is like this: 我的角度代码是这样的:

 let headers = new Headers();
        headers.append('Content-Type', 'application/json');
        headers.append('Authorization', this.uriRequest.appendAuthHeaderValue());
        let options = new RequestOptions({ headers: headers });

        console.log(JSON.stringify(options));

        this.http.post(this.urlManager.BASE_URL + this.urlManager.customerServiceURL, body, options).map(
            (response) => {
                console.log(JSON.stringify(response));
            });

On the console i can found that in options my Authorization header is properly getting set : 在控制台上,我可以发现在选项中我的Authorization标头已正确设置:

{"method":null,"headers":{"Content-Type":["application/json"],"Authorization":["eyJhbGciOiJIUzIiJ9.eyJleHAiOjE1MDMyNTQ4NDIsInN1YiI6Ild25ldyIsInVzZXJJZCI6ImRlbW8iLCJyb2xlIjoiVFUiJ9.OvS69uweYIB96mQLt61RQVyoPnbY0aUhqpzUvHu40"]},"body":null,"url":null,"withCredentials":null,"responseType":null}

But in my backend i cannot able to get the token: 但是在后端,我无法获取令牌:

My COROS Filer: 我的COROS文件管理器:

HttpServletResponse res = (HttpServletResponse) response;
    HttpServletRequest  req = (HttpServletRequest) request;

res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", " x-requested-with, Content-Type, Accept, Accept-Encoding, Accept-Language, Host, Referer, Connection, User-Agent, Authorization, sw-useragent, sw-version");
res.setHeader("Access-Control-Expose-Headers", "Authorization");

    // Just REPLY OK if request method is OPTIONS for CORS (pre-flight)
    if ( req.getMethod().equals("OPTIONS") ) {
    res.setStatus(HttpServletResponse.SC_OK);
    return;
}
    chain.doFilter(request, response);

I am getting my token like this: 我得到这样的令牌:

final String token = request.getHeader("Authorization");

Please help me on this i could not able to proceed from here. 请对此帮助我,我无法从这里继续进行。

Attached Screen: 附加屏幕:

OPTION: 选项: 在此处输入图片说明

POST: 开机自检:

在此处输入图片说明

RESPONSE Header: 响应标题: 在此处输入图片说明

在此处输入图片说明

If you still haven't found the answer. 如果您仍然找不到答案。 Here it is. 这里是。

Headers in angular are immutable. 角度标题是不可变的。 So headers.append() actually returns a new header and the old one remains the same. 所以headers.append()实际上返回一个新的头,而旧的头保持不变。

You have to use it like, 您必须像这样使用它

let headers = new Headers();
headers = headers.append("some-header", "value");
headers = headers.append("another-header", "another-value");

And then just pass this header. 然后只需传递此标头即可。

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

相关问题 Spring 引导后端与 angular 前端 - Spring boot backend with angular frontend Angular 6,HtttpClient,带有后端Spring Boot! 我的标题没有到达后端 - Angular 6, HtttpClient, with BackEnd Spring Boot! My Header don't reach to the backend Spring 引导安全 - 如果缺少授权 header,则使用 Cookies 中的令牌 - Spring Boot Security - Use token from Cookies if Authorization header missing Spring Boot 微服务 - 将授权标头添加到 restTemplate.getForObject - Spring Boot Microservices - Add authorization header to restTemplate.getForObject React + Spring Boot:无法从 Header 获取 Authorization 值 - React + Spring Boot: Can't get Authorization value from Header 在 Spring Boot 中传递给标头后 JWT 授权不起作用 - JWT authorization is not working after passing to header in spring boot 添加授权Header承载认证到Spring启动Controller - Add Authorization Header Bearer Authentication to Spring Boot Controller 如何在所有请求中传递授权令牌 header - Spring 启动 java - How to pass the authorization token in all request header - Spring boot java Angular dont map 我的 API 在后端的请求(Spring Boot) - Request of Angular dont map my API in BackEnd (Spring Boot) Angular 前端和 Java Spring 引导后端中的 CORS 策略阻止了请求? - Request blocked by CORS policy in Angular frontent and Java Spring Boot backend?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM