Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot Mono defaultItEmpty未被调用_Spring Boot_Reactive Programming_Spring Webflux - Fatal编程技术网

Spring boot Mono defaultItEmpty未被调用

Spring boot Mono defaultItEmpty未被调用,spring-boot,reactive-programming,spring-webflux,Spring Boot,Reactive Programming,Spring Webflux,我写了以下代码:- @Override public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, T object) { ServerWebExchange serverWebExchange = (ServerWebExchange) object; return authentication. filter((auth) ->

我写了以下代码:-

  @Override
  public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, T object) {
    ServerWebExchange serverWebExchange = (ServerWebExchange) object;
    return authentication.
        filter((auth) -> auth.isAuthenticated() && auth.getPrincipal()
        .equals(serverWebExchange.getRequest().getHeaders().getFirst("X-Tenant")))
        .flatMapIterable(authentication1 -> authentication1.getAuthorities())
        .map(GrantedAuthority::getAuthority)
        .any(authority -> this.authorities.contains(authority))
        .map(aBoolean -> new AuthorizationDecision(aBoolean))
        .defaultIfEmpty(new AuthorizationDecision(true));
  }
@覆盖
公共单声道检查(单声道身份验证,T对象){
ServerWebExchange ServerWebExchange=(ServerWebExchange)对象;
返回身份验证。
过滤器((auth)->auth.isAuthenticated()&auth.getPrincipal()
.equals(serverWebExchange.getRequest().getHeaders().getFirst(“X租户”))
.flatMapIterable(authentication1->authentication1.getAuthories())
.map(GrantedAuthority::getAuthority)
.any(authority->this.authorities.contains(authority))
.map(aBoolean->新授权决策(aBoolean))
.defaultIfEmpty(新授权决策(true));
}

当身份验证对象为空时,为什么底部的
defaultIfEmpty
没有被调用?

因为
any
操作符。当
流量
为空时,它将返回一个
Mono
值,其中包含
false
值,因此当流量达到
defaultIfEmpty
运算符时,它将永远不会为空。因此是否需要在此处添加defaultIfEmpty?这取决于您的目标。在您共享的代码示例中,它对结果没有影响:如果身份验证Mono为空,则在任何情况下都将返回
AuthorizationDecision(false)
。如果这满足了您的要求,那么您可以安全地省略defaultIfEmpty,而无需寻找其他选项。谢谢@MartinTarjányi