我的角度应用程序正在向zuul服务发送http请求,我的问题是,当我尝试发送授权头时,zuul服务没有接收到该头,这是角度代码:
obtenerAvisos() {
const token = localStorage.getItem('token');
const headers = new HttpHeaders(
{
'Content-Type': 'application/json',
'Authorization': token
}
);
return this.http.get(
environment.url + environment.msavisos,
{
headers: headers
}
);
}
在zuul上,我创建了一个预过滤器,在那里我试图捕捉标题:
package com.filtro;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpStatusCodeException;
import com.netflix.zuul.context.RequestContext;
import com.utilidades.JwtUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import com.netflix.zuul.ZuulFilter;
public class PreFilter extends ZuulFilter {
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Autowired
private JwtUtil jwtUtil;
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String ip = request.getLocalAddr();
String authorization = request.getHeader("Authorization");
String content = request.getHeader("Content-Type");
System.out.println(content);
try {
if ( request.getRequestURL().toString().contains("/usuarios/sesion")) {
} else if ( authorization != null ) {
Claims claims = null;
try {
claims = jwtUtil.parseToken(authorization);
} catch (ExpiredJwtException e) {
// Sesion expirada
ctx.unset();
ctx.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
}
if (claims != null) {
if (!JwtUtil.esIpCorrecta(claims, ip)) {
// Ip sin acceso
ctx.unset();
ctx.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
} else {
// Acceso concedido
ctx.addZuulRequestHeader("authorization", jwtUtil.generateToken(claims, ip));
}
} else {
// Token Invalido
ctx.unset();
ctx.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
}
} else {
ctx.unset();
ctx.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
}
} catch (HttpStatusCodeException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
我使用postman发出请求并发送头,每次都正常工作,我已经看到
this
和
this
但找不到答案,有人能帮我解决这个问题吗?,提前谢谢。