mercredi 3 mars 2021

How to get request header in weblux aspect?

ReactiveRequestContextFilter

@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public class ReactiveRequestContextFilter implements WebFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        return chain.filter(exchange)
                .subscriberContext(ctx -> ctx.put(ReactiveRequestContextHolder.CONTEXT_KEY, request));
    }
}

ReactiveRequestContextHolder

public class ReactiveRequestContextHolder {
    public static final Class<ServerHttpRequest> CONTEXT_KEY = ServerHttpRequest.class;

    public static Mono<ServerHttpRequest> getRequest() {
        return Mono.subscriberContext()
                // TODO error reactor.core.Exceptions$ErrorCallbackNotImplemented: java.util.NoSuchElementException: Context is empty
                .map(ctx -> ctx.get(CONTEXT_KEY));
    }

}
@Slf4j
@Aspect
@Component
@AllArgsConstructor
public class SecurityInnerAspect {

    @Around("@annotation(test)")
    // 不用JoinPoint 用 ProceedingJoinPoint,因为ProceedingJoinPoint可以拦截请求
    public Object around(ProceedingJoinPoint point, Inner inner) throws Throwable {
        ReactiveRequestContextHolder.getRequest().subscribe(serverHttpRequest -> {
            String header = Objects.requireNonNull(serverHttpRequest.getHeaders().get("testHeaderStr")).get(0);
            log.info("print testHeader: {}", header);
        });
        return point.proceed();
    }
}

I want to get the request from the aspect ? How to get request header in weblux aspect? I want to get the request from the aspect ? How to get request header in weblux aspect?

I want to get the request from the aspect ? How to get request header in weblux aspect?

I want to get the request from the aspect ? How to get request header in weblux aspect?




Aucun commentaire:

Enregistrer un commentaire