如何在DropWizard Auth中获取HttpServletRequest @Context

时间:2015-05-06 11:24:11

标签: dropwizard

我在Dropwizard 0.8中使用Basic Auth 我需要在SimpleAuthenticator类中访问请求上下文,超出基本凭据。

也许我需要自己的AuthFactory实现来执行此操作?

我想根据请求的资源路径实现Basic Auth的基本访问控制。

由于

乔恩

2 个答案:

答案 0 :(得分:1)

    @PermitAll
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getResponse(**@Context** SecurityContext context) {
        SimplePrincipal principal = (SimplePrincipal)context.getUserPrincipal();

        return Response.ok("{\"Hello\": \"" + principal.getUsername() + "\"}").build();
    }

使用@Context足以完成Dropwizard 0.9.2版的基本授权

答案 1 :(得分:0)

我还没有使用0.8,但是在0.9+中,您可以使用@Auth@PermitAll来控制哪些资源具有身份验证功能以及哪些资源不具备身份验证功能。这不符合你的要求吗?或者您更喜欢更精细的控制?

这里是0.9中auth的变更集。您可以参考Dropwizard: BasicAuth获取示例代码。