强制Jersey Grizzly服务器进行身份验证

强制Jersey Grizzly服务器进行身份验证,jersey,grizzly,Jersey,Grizzly,我正在对使用Jersey编写的REST服务执行验收测试。在“我的服务”中,我需要使用以下方法获取成功通过身份验证(基本)的用户的名称: 我正在使用Jersey客户端API并在Grizzly服务器上运行。我按如下方式设置服务器: public class BaseResourceTest extends JerseyTest { public BaseResourceTest() throws Exception { super(new WebAppDescriptor.Buil

我正在对使用Jersey编写的REST服务执行验收测试。在“我的服务”中,我需要使用以下方法获取成功通过身份验证(基本)的用户的名称:

我正在使用Jersey客户端API并在Grizzly服务器上运行。我按如下方式设置服务器:

public class BaseResourceTest extends JerseyTest {

   public BaseResourceTest() throws Exception {

    super(new WebAppDescriptor.Builder("net.tds.adm.na.batchcut")
        .contextPath(baseUri.getPath())
        .contextParam("contextConfigLocation","classpath:testContext.xml")
        .initParam("com.sun.jersey.api.json.POJOMappingFeature",
            "true").servletClass(SpringServlet.class)
        .contextListenerClass(ContextLoaderListener.class)
        .build());

     }

.......

}
WebResource resource = resource().path(_url);
return resource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).header(HttpHeaders.AUTHORIZATION, new String(Base64.encode("judge:smails"))).post(ClientResponse.class, _formData);
在我的测试中,我通过了授权头,如下所示:

public class BaseResourceTest extends JerseyTest {

   public BaseResourceTest() throws Exception {

    super(new WebAppDescriptor.Builder("net.tds.adm.na.batchcut")
        .contextPath(baseUri.getPath())
        .contextParam("contextConfigLocation","classpath:testContext.xml")
        .initParam("com.sun.jersey.api.json.POJOMappingFeature",
            "true").servletClass(SpringServlet.class)
        .contextListenerClass(ContextLoaderListener.class)
        .build());

     }

.......

}
WebResource resource = resource().path(_url);
return resource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).header(HttpHeaders.AUTHORIZATION, new String(Base64.encode("judge:smails"))).post(ClientResponse.class, _formData);

然而,在我的资源中,我在尝试获取用户名时得到了一个NPE。我想我需要让服务器主动地对领域进行身份验证,但我还没有找到如何配置服务器来实现这一点。有人对此有过经验吗?

不幸的是,Grizzly HttpServer目前不支持身份验证。 但在这里,您可以找到一个简单的示例,说明如何使用Jersey过滤器实现身份验证