Jersey:将值从ContainerRequestFilter传递到端点

我使用的是Jersey 2.9,我创建了一个filter,它将获取一个加密的标头值,然后将其解密,然后将其传递给被调用的端点。 我不知道如何做到这一点,我一直在网上搜索,但没有真正找到我想做的具体例子。 调用filter,我只是将值从它传递到端点时出现问题。

你能帮助我吗!

以下是一些示例代码:

public class MyFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext) throws WebApplicationException { String EncryptedString = requestContext.getHeaderString("Authentication"); /* Doing some methods to remove encryption */ /* Get a string which I want to pass to the endpoint which was called on, in this example: localhost:4883/rest/test */ } } @Path("rest") public class restTest { @Path("test") @GET @Produces(MediaType.APPLICATION_JSON) public String Testing(){ /* Process the value from the MyFilter */ } } 

您可以轻松修改标题或添加另一个标题:

 @Override public void filter(ContainerRequestContext requestContext) throws IOException { requestContext.getHeaders().add("X-Authentication-decrypted", decryptedValue); } 

可以在资源方法中注入此值:

 @GET @Produces(MediaType.APPLICATION_JSON) public String Testing(@HeaderParam("X-Authentication-decrypted") String auth) { }