简体   繁体   English

将参数传递给安全的dropwizard资源

[英]Passing arguments to a secured dropwizard resource

I have a resource, which is secured, if I remove the authentication, it appears to work, but then without the security, then what is the point? 我有一个安全的资源,如果删除身份验证,它似乎可以正常工作,但是如果没有安全性,那又有什么意义呢?

Here is my code : 这是我的代码:

@POST
@Path("/secured")
@Timed
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@UnitOfWork
@RolesAllowed("mainUser")
public Response aliveSecure(@Auth User user, CustomRequest test)
{
    CustomResponse resp = new CustomResponse();

    System.out.println(test.getMessage());
    return Response.status(Response.Status.ACCEPTED).entity(resp).build();
}

The CustomRequest and CustomResponse types are pretty standard POJOs, they just hold a string called "Message" - they are actually identical, but this is just an exercise I am trying to complete for the sake of learning DropWizard. CustomRequest和CustomResponse类型是非常标准的POJO,它们只包含一个称为“消息”的字符串-它们实际上是相同的,但这只是为了学习DropWizard而试图完成的一项练习。

Now, if I remove the @Auth stuff here, and the @RolesAllowed, etc - making it a insecure, then the method performs as normal - but as is, this is the error I get when trying to start the application. 现在,如果我在这里删除@Auth东西和@RolesAllowed等(使其变得不安全),则该方法将按正常方式执行-但按原样,这是我尝试启动应用程序时遇到的错误。

org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
! [[FATAL] No injection source found for a parameter of type public CustomRequest at index 0.;

The auth manual reads it clear - 认证手册将其清楚地读出-

If you want to use @Auth to inject a custom principal type into your resource. 如果要使用@Auth将自定义主体类型注入资源中。

Hence you shall ensure adding the following to your Service that extends io.dropwizard.Application 因此,您应确保将以下内容添加到扩展io.dropwizard.Application Service

@Override
public void run(SomeConfigThatExtendsConfiguration config, Environment environment) throws Exception {
    ....
    environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM