我有一个角度应用程序正在向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;
}
}
我使用邮递员提出了一个请求,并发送了邮件头,并且一切正常,我已经看到this和this,但是找不到答案,有人可以帮我解决这个问题吗?,预先感谢。
答案 0 :(得分:0)
尝试将'withCredentials'标头添加为true:
get(url) {
return this.http.get(url, this.getHttpOptions());
}
getHttpOptions() {
const token = localStorage.getItem('token');
const headers = {
'Content-Type': 'application/json',
'Authorization': token
}
return { withCredentials: true, headers: headers };
}
答案 1 :(得分:0)
您可以尝试:
query for Joe:
room_name | access
room 1 | true
room 2 | true
room 3 | true
room 4 | false
query for Bob:
room_name | access
room 1 | true
room 2 | false
room 3 | false
room 4 | true
答案 2 :(得分:0)
最后的问题是zuul,我需要在主应用程序中添加一个cors配置:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import com.filtro.PostFilter;
import com.filtro.PreFilter;
import com.utilidades.JwtUtil;
@SpringBootApplication
@EnableZuulProxy
public class MsZuulApplication {
public static void main(String[] args) {
SpringApplication.run(MsZuulApplication.class, args);
}
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
希望对其他人有帮助:)