reactor.core.publisher.Mono.subscriberContext()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(959)

本文整理了Java中reactor.core.publisher.Mono.subscriberContext()方法的一些代码示例,展示了Mono.subscriberContext()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Mono.subscriberContext()方法的具体详情如下:
包路径:reactor.core.publisher.Mono
类名称:Mono
方法名:subscriberContext

Mono.subscriberContext介绍

[英]Create a Mono emitting the Context available on subscribe. If no Context is available, the mono will simply emit the Context#empty().
[中]创建一个Mono,在subscribe上提供上下文。如果没有可用的上下文,mono只会发出上下文#empty()。

代码示例

代码示例来源:origin: spring-projects/spring-security

@Override
  public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    return chain.filter(exchange)
        .subscriberContext(Context.of(ServerWebExchange.class, exchange));
  }
}

代码示例来源:origin: spring-projects/spring-security

private Mono<ServerWebExchange> currentServerWebExchange() {
  return Mono.subscriberContext()
      .filter(c -> c.hasKey(ServerWebExchange.class))
      .map(c -> c.get(ServerWebExchange.class));
}

代码示例来源:origin: spring-projects/spring-security

private Mono<ServerWebExchange> currentServerWebExchange() {
  return Mono.subscriberContext()
      .filter(c -> c.hasKey(ServerWebExchange.class))
      .map(c -> c.get(ServerWebExchange.class));
}

代码示例来源:origin: spring-projects/spring-security

/**
 * Gets the {@code Mono<SecurityContext>} from Reactor {@link Context}
 * @return the {@code Mono<SecurityContext>}
 */
public static Mono<SecurityContext> getContext() {
  return Mono.subscriberContext()
    .filter( c -> c.hasKey(SECURITY_CONTEXT_KEY))
    .flatMap( c-> c.<Mono<SecurityContext>>get(SECURITY_CONTEXT_KEY));
}

代码示例来源:origin: reactor/reactor-core

@Test
public void currentContextWithEmpty() throws InterruptedException {
  StepVerifier.create(Mono.just("foo")
              .flatMap(d -> Mono.subscriberContext()
                       .map(c -> d + c.get(Integer.class))))
        .verifyErrorMatches(t -> t instanceof NoSuchElementException
            && "Context is empty".equals(t.getMessage()));
}

代码示例来源:origin: spring-projects/spring-security

private Object resolveArgument(MethodParameter methodParameter) {
  return this.argumentResolver.resolveArgument(methodParameter, null, null)
      .subscriberContext(this.authentication == null ? Context.empty() : ReactiveSecurityContextHolder.withAuthentication(this.authentication))
      .block();
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void setContextAndClearAndGetContextThenEmitsEmpty() {
  SecurityContext expectedContext = new SecurityContextImpl(
    new TestingAuthenticationToken("user", "password", "ROLE_USER"));
  Mono<SecurityContext> context = Mono.subscriberContext()
    .flatMap( c -> ReactiveSecurityContextHolder.getContext())
    .subscriberContext(ReactiveSecurityContextHolder.clearContext())
    .subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedContext)));
  StepVerifier.create(context)
    .verifyComplete();
}

代码示例来源:origin: spring-projects/spring-security

@Test
  public void setAuthenticationAndGetContextThenEmitsContext() {
    Authentication expectedAuthentication = new TestingAuthenticationToken("user",
      "password", "ROLE_USER");

    Mono<Authentication> authentication = Mono.subscriberContext()
      .flatMap( c -> ReactiveSecurityContextHolder.getContext())
      .map(SecurityContext::getAuthentication)
      .subscriberContext(ReactiveSecurityContextHolder.withAuthentication(expectedAuthentication));

    StepVerifier.create(authentication)
      .expectNext(expectedAuthentication)
      .verifyComplete();
  }
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void monoPostAuthorizeWhenNotAuthorizedThenDenied() {
  when(this.delegate.monoPostAuthorizeBeanFindById(1L)).thenReturn(Mono.just("not-authorized"));
  Mono<String> findById = this.messageService.monoPostAuthorizeBeanFindById(1L)
      .subscriberContext(withUser);
  StepVerifier
      .create(findById)
      .expectError(AccessDeniedException.class)
      .verify();
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void monoPreAuthorizeHasRoleWhenGrantedThenSuccess() {
  when(this.delegate.monoPreAuthorizeHasRoleFindById(1L)).thenReturn(Mono.just("result"));
  Mono<String> findById = this.messageService.monoPreAuthorizeHasRoleFindById(1L)
      .subscriberContext(withAdmin);
  StepVerifier
      .create(findById)
      .expectNext("result")
      .verifyComplete();
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void monoPostAuthorizeWhenBeanAndAuthorizedThenSuccess() {
  when(this.delegate.monoPostAuthorizeBeanFindById(2L)).thenReturn(Mono.just("user"));
  Mono<String> findById = this.messageService.monoPostAuthorizeBeanFindById(2L)
      .subscriberContext(withUser);
  StepVerifier
      .create(findById)
      .expectNext("user")
      .verifyComplete();
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void monoPreAuthorizeBeanWhenGrantedThenSuccess() {
  when(this.delegate.monoPreAuthorizeBeanFindById(2L)).thenReturn(Mono.just("result"));
  Mono<String> findById = this.messageService.monoPreAuthorizeBeanFindById(2L)
      .subscriberContext(withAdmin);
  StepVerifier
      .create(findById)
      .expectNext("result")
      .verifyComplete();
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void monoPostAuthorizeWhenAuthorizedThenSuccess() {
  when(this.delegate.monoPostAuthorizeFindById(1L)).thenReturn(Mono.just("user"));
  Mono<String> findById = this.messageService.monoPostAuthorizeFindById(1L)
      .subscriberContext(withUser);
  StepVerifier
      .create(findById)
      .expectNext("user")
      .verifyComplete();
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void monoPostAuthorizeWhenBeanAndNotAuthorizedThenDenied() {
  when(this.delegate.monoPostAuthorizeBeanFindById(1L)).thenReturn(Mono.just("not-authorized"));
  Mono<String> findById = this.messageService.monoPostAuthorizeBeanFindById(1L)
      .subscriberContext(withUser);
  StepVerifier
      .create(findById)
      .expectError(AccessDeniedException.class)
      .verify();
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void setContextAndGetContextThenEmitsContext() {
  SecurityContext expectedContext = new SecurityContextImpl(
    new TestingAuthenticationToken("user", "password", "ROLE_USER"));
  Mono<SecurityContext> context = Mono.subscriberContext()
    .flatMap( c -> ReactiveSecurityContextHolder.getContext())
    .subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedContext)));
  StepVerifier.create(context)
    .expectNext(expectedContext)
    .verifyComplete();
}

代码示例来源:origin: spring-projects/spring-security

@GetMapping("/**")
  Mono<String> pathWithinApplicationFromContext() {
    return Mono.subscriberContext()
      .filter(c -> c.hasKey(ServerWebExchange.class))
      .map(c -> c.get(ServerWebExchange.class))
      .map(e -> e.getRequest().getPath().pathWithinApplication().value());
  }
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void demo() {
  Authentication authentication = new TestingAuthenticationToken("user", "password", "ROLE_USER");
  Mono<String> messageByUsername = ReactiveSecurityContextHolder.getContext()
    .map(SecurityContext::getAuthentication)
    .map(Authentication::getName)
    .flatMap(this::findMessageByUsername)
    .subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication));
  StepVerifier.create(messageByUsername)
    .expectNext("Hi user")
    .verifyComplete();
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void monoPreAuthorizeHasRoleWhenNotAuthorizedThenDenied() {
  when(this.delegate.monoPreAuthorizeHasRoleFindById(1L)).thenReturn(Mono.from(result));
  Mono<String> findById = this.messageService.monoPreAuthorizeHasRoleFindById(1L)
      .subscriberContext(withUser);
  StepVerifier
      .create(findById)
      .expectError(AccessDeniedException.class)
      .verify();
  result.assertNoSubscribers();
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void monoPreAuthorizeBeanWhenNotAuthorizedThenDenied() {
  when(this.delegate.monoPreAuthorizeBeanFindById(1L)).thenReturn(Mono.from(result));
  Mono<String> findById = this.messageService.monoPreAuthorizeBeanFindById(1L)
      .subscriberContext(withUser);
  StepVerifier
      .create(findById)
      .expectError(AccessDeniedException.class)
      .verify();
  result.assertNoSubscribers();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void fluxSubscriberContextWithMergedEmpty() {
  StepVerifier.create(
      Flux.just("foo")
        .flatMap(v -> Mono.subscriberContext())
        .subscriberContext(Context.empty())
        .subscriberContext(Context.of("initial", "value"))
  )
        .expectNextMatches(c -> c.hasKey("initial"))
        .verifyComplete();
}

相关文章

微信公众号

最新文章

更多

Mono类方法