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



我有一个现有的类,我正试图挂接它,以获取一些头参数,从而将用户SSO到我们的系统中。课程如下。

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()?我已经找到了很多我所拥有的类的例子,但没有任何例子表明如何称呼它

我还尝试将注释放在类中,而不是作为参数。

@Context
HttpHeaders httpHeaders;

但是当我尝试访问CCD_ 3时,它返回null。我认为这是因为它实际上并没有创建,因为javax文档说它永远不会返回null。

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

首先编写一个Annotation。

@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;
}

相关内容

  • 没有找到相关文章

最新更新