【问题标题】:forcing the Jersey Grizzly server to authenicate强制 Jersey Grizzly 服务器进行身份验证
【发布时间】:2013-04-12 20:53:39
【问题描述】:

我正在对使用 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。我想我需要让服务器主动对领域进行身份验证,但我还没有找到如何配置服务器来执行此操作。有人有这方面的经验吗?

【问题讨论】:

    标签: jersey grizzly


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-14
      • 2012-09-06
      • 1970-01-01
      • 2012-05-08
      • 2010-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多