当前位置: 首页 > 知识库问答 >
问题:

Spring Cloud Gateway添加标头,使用响应WebClient

阎阳
2023-03-14

我正在开发一个带有Spring Cloud的网关,我需要拦截消耗Oauth的请求,然后获取响应并添加带有jwt值的标头(Ouath的响应)

我的筛选代码:

@Autowired
private IConsumoInstrospectService consumoIntrospect;

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

    Mono<ResponseIntrospectDTO> rep = consumoIntrospect
            .callInstrospect("firmaCnLlavePrivada", "getAccessToken()","getScope()", "IN");
    // How Can I get the value?
    String token=rep.block().getJwt();
    
    //String token="x";
    
    log.info("token "+token);
    String authHeader = "Bearer " +token ;

    ServerHttpRequest mutatedRequest = exchange.getRequest().mutate().header(HttpHeaders.AUTHORIZATION, authHeader)
            .build();
    exchange = exchange.mutate().request(mutatedRequest).build();
    return chain.filter(exchange);
}

我的服务:

@Value("${endPoint}")
private String endPoint;

@Bean
public WebClient getWebClient() {
    return WebClient.builder().build();
}

@Override
public Mono<ResponseIntrospectDTO> callInstrospect(String Jwt, String AccessToken, String Scope, String tipoAccessToken) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content_Type", "application/x-www-form-urlencoded");
    headers.add(AUTHORIZATION, "Bearer " + Jwt);
    headers.add("need-jwt", "yes");

    LinkedMultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();
    body.add("token", AccessToken);
    body.add("token_type_hint", "access_token");
    body.add("iss", Scope);

    Mono<ResponseIntrospectDTO> respService= getWebClient().post()
            .uri(endPoint)
            .accept(MediaType.APPLICATION_JSON)
            .body(BodyInserters.fromFormData(body))
            .headers(x -> x.addAll(headers))
            .acceptCharset(StandardCharsets.UTF_8)
            .retrieve()
            .bodyToMono(ResponseIntrospectDTO.class);
    return respService;
}

完整代码Gitlab

错误:

java.lang.IllegalStateException:块()/块第一()/块最后()被阻塞,线程reactor-超文本传输协议-nio-3不支持

当我使用String token=rep.block()时。getJwt();

共有1个答案

农永宁
2023-03-14

Reactor不允许在并行调度程序中使用块。试着让你的代码反应。

下面是一个从过滤器中的身份验证令牌中提取属性并将其添加到标头的示例

   @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        return ReactiveSecurityContextHolder.getContext()
                .filter(c -> c.getAuthentication() != null)
                .flatMap(c -> {
                    JwtAuthenticationToken jwt = (JwtAuthenticationToken) c.getAuthentication();

                    String tokenTenant = (String) jwt.getTokenAttributes().get("tenant");
                    if (Strings.isNullOrEmpty(tokenTenant)) {
                        return Mono.error(
                                new AccessDeniedException("Invalid token. Tenant is not present in token.")
                        );
                    }

                    ServerHttpRequest request = exchange.getRequest().mutate()
                            .header("x-tenant", tokenTenant).build();

                    return chain.filter(exchange.mutate().request(request).build());
                })
                .switchIfEmpty(chain.filter(exchange));
    }
 类似资料:
  • 问题内容: 我有一个Flask Web应用程序,它使用render_template如下,我需要在响应中添加一个Content-Security-Policy作为附加的HTTP响应标头。我尝试了以下方法,但都失败了,并给了我500。 1。 2。 这里有什么问题? 在终端上,以localhost:3001的身份访问Web应用程序时,我看到以下内容 127.0.0.1–[2015年4月6日01:45:

  • 我有一个flask web应用程序,它使用render_template如下所示。我需要在响应中添加一个Content-Security-Policy作为额外的http响应头。我试着按照方法走,但都失败了,给了我500。 1. 在终端上,当我以localhost身份访问web应用程序时,会看到以下内容:3001 127.0.0.1--[06/APR/2015 01:45:01]“GET/HTTP/

  • 我正在实现Apiary的其他同事制作的API。io,在Windows应用商店应用程序项目中。 它们显示了我必须实现的方法的示例: 在这个和其他一些方法中,我需要有一个带有我之前获得的令牌的标头。 如何将该授权标头添加到请求中?

  • 我非常确定“Expires”是有效的HTTP响应头类型。但是当我尝试在代码中设置它时:(这是在ActionFilter.OnActionExecuted方法中) 我最后有一个例外: InvalidOperationException:错误使用的标头名称。确保请求头与HttpRequestMessage一起使用,响应头与HttpResponseMessage一起使用,内容头与HttpContent对

  • 我需要在每个响应中添加标题。我正打算在下面做 我想在之后这样做,以便一旦控制器处理它,我只是在返回客户端之前添加标头。是正确的吗? 但如何编写响应过滤器? 在已返回,对响应执行任何操作都为时已晚。此时,整个响应已经发送到客户端,您的代码无法访问它。 我觉得上面的说法不对。我不能在过滤链之后添加标题。doFilter(请求、响应)?如果不是,为什么? 我使用的是spring mvc。

  • 我知道SiteMinder可以将HTTP标头添加到HTTP请求中,以便后端设备(Web服务器、应用服务器等)可以拥有用于处理的标头。但是在返回客户端(浏览器)的路上,SiteMinder可以添加HTTP响应标头以便客户端可以使用它吗? 我看到了WebAgent-HTTP-Header-Variable的响应类型,但似乎只能在服务器端访问。 我快速搜索了一些留档和谷歌搜索,但SiteMinder留档