【问题标题】:Authorization in dropwizarddropwizard中的授权
【发布时间】:2016-02-08 17:44:49
【问题描述】:

我想在 0.8.0-rc3-SNAPSHOT 中使用 dropwizard 制作一个小型应用程序。我想如果任何用户调用我的 api 用户应该在标题部分传递一个 authtoken。到目前为止我所做的是---

@Override
public void run(HelloWorldConfigurationhelloWorldConfiguration,Environment environment) throws Exception{
environment.jersey().register(new ViewResource());      
environment.servlets().addFilter("MyCustomRequestFilter", new MyCustomRequestFilter())
            .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),false, "/*");
}

public class MyCustomRequestFilter implements ContainerRequestFilter {
@Override
public ContainerRequest filter(ContainerRequest request) {
    System.out.print("test");
    if ( request.getQueryParameters().containsKey("validateMeParam") ) {
              /* validation logic */
    }
    // finished validation
    return request;
}
}

我不知道我做错了什么。它不起作用。

【问题讨论】:

标签: java jersey dropwizard


【解决方案1】:

ContainerRequestFilter 不是 Servlet Filter,这是您通过执行 environment.servlets().addFilter 所假设的。这应该添加到 Jersey 配置中。

environment.jersey().register(MyCustomRequestFilter.class);

不要忘记过滤器类上的@Provider注解。

  • 在 Dropwizard 文档中的 Jersey Filters 中查看有关过滤器的更多信息。

更新

我看到另一个严重的问题。您说您使用的是 Dropwizard 0.8.0,它使用 Jersey 2。在这种情况下,您发布的 ContainerRequestFilter 甚至不应该存在。在 Jersey 1 中,filter 方法的参数是 ContainerRequest,而 Jersey 2 中的参数是 ContainerRequestContext。请向您展示依赖关系,并验证您上面的类是实际的类

【讨论】:

  • 你看链接了吗?该示例使用DynamicFeature 映射某些方法,并使用注释,但如果您不需要它(即您希望所有请求都通过过滤器),那么您不需要实现@ 987654332@,只需如上所示注册过滤器。除了你只需要确保你使用正确的ContainerRequestFilter。您在帖子中看到的似乎是 Jersey 1 接口,它以com.sun 开头。 Jersey 2 所需的过滤器应以 javax.ws.rs 开头。
  • 为什么@Provider 注释很重要?我的代码中没有它,它似乎工作得很好(Dropwizard 1.0.2)。
【解决方案2】:

我希望你正在寻找这种类型的样品

https://github.com/stevenalexander/dropwizard-security

/* 一个示例安全提供程序,当端点使用 auth 属性接收到每个请求时,它将查看每个请求 */

public class ExampleSecurityProvider<T> implements InjectableProvider<Auth, Parameter> {

public final static String CUSTOM_HEADER = "custom-security-token";

private final Authenticator<ExampleCredentials, T> authenticator;

public ExampleSecurityProvider(Authenticator<ExampleCredentials, T> authenticator) {
    this.authenticator = authenticator;
}

private static class ExampleSecurityInjectable<T> extends AbstractHttpContextInjectable<T> {

    private final Authenticator<ExampleCredentials, T> authenticator;
    private final boolean required;

    private ExampleSecurityInjectable(Authenticator<ExampleCredentials, T> authenticator, boolean required) {
        this.authenticator = authenticator;
        this.required = required;
    }

    @Override
    public T getValue(HttpContext c) {
        // This is where the credentials are extracted from the request
        final String header = c.getRequest().getHeaderValue(CUSTOM_HEADER);

        try {
            if (header != null) {
                final Optional<T> result = authenticator.authenticate(new ExampleCredentials(header));
                if (result.isPresent()) {
                    return result.get();
                }
            }
        } catch (AuthenticationException e) {
            throw new WebApplicationException(Response.Status.UNAUTHORIZED);
        }

        if (required) {
            throw new WebApplicationException(Response.Status.UNAUTHORIZED);
        }

        return null;
    }
}

【讨论】:

猜你喜欢
  • 2016-03-22
  • 2016-05-07
  • 2018-10-06
  • 1970-01-01
  • 2017-02-06
  • 2018-11-21
  • 2012-05-31
  • 2011-08-05
  • 2016-05-06
相关资源
最近更新 更多