将 @Context 参数传递给 class 中的方法

Passing @Context argument to method in class

我有一个现有的 class 我正在尝试连接以获取一些 header 参数以将用户 SSO 到我们的系统中。 class如下。

import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Component;

@Component
@Path("/http")
public class HttpResource {
    @GET
    @Path("/getHeaders")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, String> getAllHeaders(@Context HttpHeaders headers) {
        Map<String, String> headerList = new HashMap<String, String>();
        for (String key : headers.getRequestHeaders().keySet()) {
            String value = headers.getRequestHeader(key).get(0);
            System.out.println(key + " : " + value);
            headerList.put(key, value);
        }
        return headerList;
    }
}

我想弄清楚的是如何使用 @Context 参数调用 getAllHeaders()?我找到了大量 class 的示例,但没有任何内容说明如何调用它。

我也试过将注释放在 class 中而不是作为参数。

@Context
HttpHeaders httpHeaders;

但是当我尝试访问 httpHeaders.getAllHeaders() 时,它 return 为空。我假设是因为它实际上并没有创建,因为 javax 文档说它永远不会 return null.

我试图在我的 SSOAuthorizationFilter.java 中调用它,但也尝试通过控制器访问它。

先写个注解

@Retention(RUNTIME)
@Target({ PARAMETER })
@Documented
public @interface SSOAuthorization {}

然后是解析器。

public class SSOAuthorizationResolver {

    public static class SSOAuthorizationInjectionResolver extends
            ParamInjectionResolver<SSOAuthorization> {
        public SSOAuthorizationInjectionResolver() {
            super(SSOAuthorizationValueFactoryProvider.class);
        }
    }


    @Singleton
    public static class SSOAuthorizationValueFactoryProvider extends
            AbstractValueFactoryProvider {

        @Context
        private HttpHeaders httpHeaders;

        @Inject
        public SSOAuthorizationValueFactoryProvider(
                final MultivaluedParameterExtractorProvider mpep,
                final ServiceLocator injector) {
            super(mpep, injector, Parameter.Source.UNKNOWN);
        }

        @Override
        protected Factory<?> createValueFactory(final Parameter parameter) {
            final Class<?> classType = parameter.getRawType();

            if (!Language.class.equals(classType)
                    || parameter.getAnnotation(SSOAuthorization.class) == null) {
                return null;
            }

            return new AbstractContainerRequestValueFactory<String>() {
                @Override
                public String provide() {
                    // Can use httpHeader to get your header here.
                    return httpHeader.getHeaderString("SSOAuthorization");
                }
            };
        }

    }

    // Binder implementation
    public static class Binder extends AbstractBinder {
        @Override
        protected void configure() {

            bind(SSOAuthorizationValueFactoryProvider.class).to(
                    ValueFactoryProvider.class).in(Singleton.class);

            bind(SSOAuthorizationInjectionResolver.class).to(
                    new TypeLiteral<InjectionResolver<SSOAuthorization>>() {
                    }).in(Singleton.class);

        }

    }
}

然后在 ResourceConfig 中注册 Resolver

public class MyResourceConfig extends ResourceConfig {

    public MyResourceConfig(Class... classes) {
        super(classes);
        register(new SSOAuthorizationResolver.Binder());
    }
}

最后在带有 @SSOAuthorization 注释的控制器中使用它。

@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public String someMethod(@SSOAuthorization String auth) {
    return auth;
}