Я пытаюсь выполнить get
запрос с headers
, потому что в моем backEnd есть проверка на токен, который я генерирую при входе в систему. Но по какой-то причине я не могу получить заголовок авторизации, и я печатаю все заголовки в консоли, и я не вижу свой заголовок
вот код моего сервиса в Angular 6
import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient, HttpErrorResponse } from '@angular/common/http';
import { catchError, map, tap } from 'rxjs/operators';
import { Usuario } from '../clases/usuario';
import { throwError } from 'rxjs';
constructor(private http: HttpClient) {}
getAllUsers(token: string) {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'my-auth-token'
})
};
httpOptions.headers = httpOptions.headers.set('Authorization', token);
return this.http.request('GET', this.usuarioUrl, httpOptions)
.pipe(
map((resp: Usuario[]) => {
this.usuarios = resp;
return this.usuarios;
}),
catchError(this.handleError)
);
}
вот мой фильтр в моей задней части
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (!(((HttpServletRequest) request).getRequestURI().endsWith("/auth/login") ||
((HttpServletRequest) request).getRequestURI().endsWith("/auth/signup"))) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String authHeader = httpRequest.getHeader(AuthUtils.AUTH_HEADER_KEY);
Enumeration < String > test = httpRequest.getHeaderNames();
while (test.hasMoreElements()) {
String headerName = test.nextElement();
logger.info("Header " + headerName);
logger.info("getHeader " + httpRequest.getHeader(headerName));
}
logger.info("AuthHeader " + authHeader);
if (StringUtils.isEmpty(authHeader) || authHeader.split(" ").length != 1) {
logger.error("No token");
httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, AUTH_ERROR_MSG);
} else {
JWTClaimsSet claimSet = null;
try {
claimSet = (JWTClaimsSet) AuthUtils.decodeToken(authHeader);
} catch (ParseException e) {
httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, JWT_ERROR_MSG);
return;
} catch (JOSEException e) {
httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, JWT_INVALID_MSG);
return;
}
// ensure that the token is not expired
if (new DateTime(claimSet.getExpirationTime()).isBefore(DateTime.now())) {
httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, EXPIRE_ERROR_MSG);
} else {
chain.doFilter(request, response);
}
}
} else {
chain.doFilter(request, response);
}
и моя консоль это
2018-09-16 19:57:00.877 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : Header host
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : getHeader localhost:8091
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : Header connection
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : getHeader keep-alive
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : Header access-control-request-method
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : getHeader POST
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : Header origin
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : getHeader http://localhost:4200
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : Header user-agent
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : getHeader Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : Header access-control-request-headers
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : getHeader authorization,content-type
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : Header accept
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : getHeader */*
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : Header accept-encoding
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : getHeader gzip, deflate, br
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : Header accept-language
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : getHeader es-ES,es;q=0.9
2018-09-16 19:57:00.878 INFO 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : AuthHeader null
2018-09-16 19:57:00.878 ERROR 8880 --- [nio-8091-exec-7] py.edu.una.rest.filters.AuthFilter : No token
как получить заголовок Авторизация в моем бэкэнде для проверки? пожалуйста помоги! почему он не доходит до моего конца?