迫使Jersey Grizzly服务器进行身份验证

时间:2013-04-12 20:53:40

标签: jersey grizzly

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

@Context private SecurityContext securityContext; //at class level

String username = securityContext.getUserPrincipal().getName()  //inside a resource

我正在使用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);

但是,在我的资源中,我在尝试获取用户名时获得了NPE。我想我需要让服务器主动验证领域,但我还没有找到如何配置服务器来执行此操作。有没有人有这方面的经验?

1 个答案:

答案 0 :(得分:2)

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

http://simplapi.wordpress.com/2013/01/24/jersey-jax-rs-implements-a-http-basic-auth-decoder/

相关问题