spring cloud gateway 2添加身份验证信息标题

时间:2018-03-16 09:33:33

标签: project-reactor spring-cloud-gateway

我需要添加一些从exchange.getPrincipal()转发到转发请求的用户信息,然后写一个GlobalFilter,如下所示:

public class UserInfoFilter implements GlobalFilter, Ordered {

@Override
public int getOrder() {
    return Ordered.HIGHEST_PRECEDENCE;
}

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    ServerHttpRequest.Builder mutate = exchange.getRequest().mutate();
    return exchange.getPrincipal().map(principal -> {
        if (principal instanceof Authentication) {
            Authentication authentication = (Authentication) principal;
            Object principal1 = authentication.getPrincipal();
            if (principal1 instanceof UserDetails) {
                UserDetails user = (UserDetails) principal1;
                mutate.header("userName", user.getUsername());
            }
        }
        return mutate;
    }).doOnSuccess(builder -> chain.filter(exchange.mutate().request(builder.build()).build())).then();
}

}

但是当我启用此过滤器时,我将不会得到任何响应只是200状态代码而没有任何异常抛出。我想我使用错误的反应堆方式,我尝试了很多但是无法使它工作。

0 个答案:

没有答案