【问题标题】:How to Optionally Protect a Resource with Custom Dropwizard Filter如何选择使用自定义 Dropwizard 过滤器保护资源
【发布时间】:2016-03-01 20:02:51
【问题描述】:

我正在使用 Dropwizard 0.9.2,我想创建一个不需要对 GET 进行身份验证并且需要对 POST 进行基本身份验证的资源。

我试过了

@Path("/protectedPing")
@Produces(MediaType.TEXT_PLAIN)
public class ProtectedPing {

@GET
public String everybody() {

    return "pingpong";
}

@PermitAll
@POST
public String authenticated(){
    return "secret pingpong";
}

CachingAuthenticator<BasicCredentials, User> ca = new CachingAuthenticator<>(environment.metrics(), ldapAuthenticator, cbSpec);
AdminAuthorizer authorizer = new AdminAuthorizer();
BasicCredentialAuthFilter<User> bcaf = new BasicCredentialAuthFilter.Builder<User>().setAuthenticator(ca).setRealm("test-oauth").setAuthorizer(authorizer).buildAuthFilter();
environment.jersey().register(bcaf);
environment.jersey().register(RolesAllowedDynamicFeature.class);
environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));
environment.jersey().register(new ProtectedPing());

这似乎导致对“/protectedPing”的所有请求都需要基本身份验证。

在 Dropwizard 0.9.2 中,文档说如果我有一个可选保护的资源,则创建一个自定义过滤器。我假设我需要这样做,但我不知道从哪里开始,或者我是否真的需要这样做。

【问题讨论】:

    标签: java authentication jersey authorization dropwizard


    【解决方案1】:

    这更像是球衣问题而不是 dropwizard 问题。你可以看这里:https://jersey.java.net/documentation/latest/filters-and-interceptors.html

    基本上你想要的是:

    1. 创建一个注释,表明您要测试身份验证(例如@AuthenticatePost)

    2. 创建资源并使用@AuthenticatePost 注释正确的方法

    3. 创建您的身份验证过滤器(可能有点像您在上面所做的)。

    4. 在动态功能中,测试注释是否存在于传入的资源上。这对于 post 是 true,对于 get 是 false。然后直接在资源方法上注册 AuthenticationFilter,而不是在资源上全局注册。

    这将是我如何解决这个问题的半完整示例:

    public class MyDynamicFeature implements DynamicFeature {
    
        @Override
        public void configure(ResourceInfo resourceInfo, FeatureContext context) {
            if(resourceInfo.getResourceMethod().getAnnotation(AuthenticateMe.class) != null ) {
                context.register(MyAuthFilter.class);
            }
        }
    
        public class MyAuthFilter implements ContainerRequestFilter {
    
            @Override
            public void filter(ContainerRequestContext requestContext) throws IOException {
                // do authentication here
            }
    
        }
    
        public @interface AuthenticateMe {
    
        }
    
        @Path("myPath")
        public class MyResource {
    
            @GET
            public String get() {
                return "get-method";
            }
    
            @POST
            @AuthenticateMe
            public String post() {
                return "post-method";
            }
        }
    }
    

    注意,DynamicFeature 会在向功能上下文注册身份验证之前检查是否存在 Authenticate Annotation。

    希望对你有帮助

    如果您有任何问题,请告诉我。

    【讨论】:

    • 我最终做了类似的事情,谢谢!我认为这更像是一个泽西岛的问题,但从 Dropwizard 的方向接近它使得弄清楚泽西岛的单词变得困难
    • 我相信 Jersey 也有类似的方法,您可以使用 Auth 将用户绑定到资源方法中。如果无法创建用户,将无法执行该方法。然而,这并没有考虑所有预匹配过滤器,它们无论如何都会运行。如果您有兴趣,这里有一些来自不同问题的信息:stackoverflow.com/questions/34304323/…
    猜你喜欢
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 2014-01-25
    • 1970-01-01
    相关资源
    最近更新 更多