Home > Blockchain >  Is it possible to add a custom `ChannelInboundHandler` in Micronaut?
Is it possible to add a custom `ChannelInboundHandler` in Micronaut?

Time:04-06

I would like to add a custom ChannelInboundHandler in my Micronaut service so that I can listen for the SslHandshakeCompletionEvent produced after a TLS handshake has been attempted.

I seem to be able to add a ChannelOutboundHandler simply enough by annotating it with @Singleton, however when I try to do the same with a ChannelInboundHandler, it does not seem to be added to the pipeline.

What's the correct way to do this?

Edit

This looks promising: https://docs.micronaut.io/snapshot/guide/index.html#nettyPipeline

CodePudding user response:

You can create an implementation of BeanCreatedEventListener<ChannelPipelineCustomizer>, and provide an implementation of the onCreated method, e.g.

    @Override
    public ChannelPipelineCustomizer onCreated(BeanCreatedEvent<ChannelPipelineCustomizer> event) {
        ChannelPipelineCustomizer customizer = event.getBean();

        if (!customizer.isServerChannel()) {
            customizer.doOnConnect(pipeline -> {
                pipeline.addAfter(
                    ChannelPipelineCustomizer.HANDLER_HTTP_CLIENT_CODEC,
                    "my-handler",
                    new MyChannelInboundHandler()
                );
                return pipeline;
            });
        }

        return customizer;
    }

Then, in your MyChannelInboundHandler class, implement the userEventTriggered method and listen to the SslHandshakeCompletionEvent.SUCCESS event. You can then make some assertions on e.g. the public key of some of the certificates in the chain if you're doing HPKP.

  • Related