JAX-RS 中的 ReaderInterceptorContext 和 WriterInterceptorContext 未触发

问题描述 投票:0回答:0

import com.google.auto.service.AutoService;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.keycloak.Config;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.services.resource.RealmResourceProvider;
import org.keycloak.services.resource.RealmResourceProviderFactory;

@AutoService(RealmResourceProviderFactory.class)
public class GlobalRequestResponseFilterResourceProvider implements RealmResourceProviderFactory {

    private static final DummyResourceProvider INSTANCE = new DummyResourceProvider();

    private static final String PROVIDER_ID = "global-filter-extension";

    @Override
    public RealmResourceProvider create(KeycloakSession session) {
        return INSTANCE;
    }

    @Override
    public void init(Config.Scope config) {
        ResteasyProviderFactory.getInstance().getContainerRequestFilterRegistry()
                .registerSingleton(GlobalRequestResponseFilter.INSTANCE);

        ResteasyProviderFactory.getInstance().getContainerResponseFilterRegistry()
                .registerSingleton(GlobalRequestResponseFilter.INSTANCE);
        
        ResteasyProviderFactory.getInstance().getClientReaderInterceptorRegistry().registerSingleton(GlobalRequestResponseFilter.INSTANCE);
    ResteasyProviderFactory.getInstance().getClientWriterInterceptorRegistry().registerSingleton(GlobalRequestResponseFilter.INSTANCE);
    }

    @Override
    public void postInit(KeycloakSessionFactory factory) {

    }

    @Override
    public void close() {

    }

    @Override
    public String getId() {
        return PROVIDER_ID;
    }

    static class DummyResourceProvider implements RealmResourceProvider {

        @Override
        public Object getResource() {
            // dummy resource
            return new Object();
        }

        @Override
        public void close() {
            // NOOP
        }
    }
}

import org.apache.commons.io.IOUtils;
import org.keycloak.common.util.Resteasy;
import org.keycloak.models.KeycloakContext;
import org.keycloak.models.KeycloakSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.manh.cp.keycloak.config.EmbeddedKeycloakApplication;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.ReaderInterceptor;
import javax.ws.rs.ext.ReaderInterceptorContext;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@Provider
public class GlobalRequestResponseFilter implements ContainerRequestFilter, ContainerResponseFilter, ReaderInterceptor, WriterInterceptor {

     // Create a Jackson ObjectMapper instance (it can be injected instead)
    private ObjectMapper mapper = new ObjectMapper();
    
    private static final Logger LOG = LoggerFactory.getLogger(GlobalRequestResponseFilter.class);
    protected static final GlobalRequestResponseFilter INSTANCE = new GlobalRequestResponseFilter();

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        
        MultivaluedMap<String, String> map = requestContext.getHeaders();
        
        for (Entry<String, List<String>> entry : map.entrySet()) {
            String k = entry.getKey();
            List<String> v = entry.getValue();
            System.out.println("Key: " + k + ", Value: " + v);
        }
        
          
        LOG.info("Before request : request=%s"+requestContext.getUriInfo());

        KeycloakSession keycloakSession = Resteasy.getContextData(KeycloakSession.class);
        KeycloakContext context = keycloakSession.getContext();

        
        
        

//        String traceId = requestContext.getHeaderString("X-TraceID");
//        MDC.put("traceId", traceId);

    }

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        responseContext.getHeaders();
        LOG.info("After request: request=%s response=%s", requestContext.getHeaders(), responseContext.getDate());
//        MDC.remove("traceId");
    }
    
    


    @Override
    public void aroundWriteTo (WriterInterceptorContext context) 
        throws IOException, WebApplicationException {
        LOG.info("WriterInterceptor");
        context.proceed();
    }

    
    @Override
    public Object aroundReadFrom(ReaderInterceptorContext ric) 
        throws IOException, WebApplicationException {
         LOG.info("ReaderInterceptor");
        return ric.proceed();
    }    
    
    
}

我的问题:

我正在尝试使用“WriterInterceptorContext”和“ReaderInterceptorContext”,并使用下面的方法注册了它。但是,我无法触发“WriterInterceptorContext”和“ReaderInterceptorContext”。我也使用@provider 来注册。我错过了什么?感谢所有帮助。请注意,“ContainerRequestFilter”和“ContainerResponseFilter”正在按预期工作,我能够获取详细信息。

ResteasyProviderFactory.getInstance().getClientReaderInterceptorRegistry().registerSingleton(GlobalRequestResponseFilter.INSTANCE);
    ResteasyProviderFactory.getInstance().getClientWriterInterceptorRegistry().registerSingleton(GlobalRequestResponseFilter.INSTANCE);

java jersey jax-rs
© www.soinside.com 2019 - 2024. All rights reserved.