Babak
Babak

Reputation: 198

put and post method dropwizard-auth-jwt

I implemented Resful application using dropwizard framework. I use dropwizard-auth-jwt for my authentication with maven package:

<dependency>
<groupId>com.github.toastshaman</groupId>
<artifactId>dropwizard-auth-jwt</artifactId>
<version>1.0.2-0 </version>
</dependency>

for adding authentication for resources, I implemented sampleAuthenticator which is implemented Authenticator class that uses Principal class for it's authentication check.

public class UserAuthenticate implements Authenticator &ltJwtContext, MyUser> {

    @Override
    public Optional&ltMyUser> authenticate(JwtContext context) {
        try {
            final String subject = context.getJwtClaims().getSubject();
            if ("authentication".equals(subject)) {
                return Optional.of(new MyUser("admin", "pass"));
            }
            return Optional.empty();
        }
        catch (MalformedClaimException e) { return Optional.empty(); }
    }
}

when MyUser implemented Principal:

public class MyUser implements Principal {

    private  String pass;
    private  String name;

    public MyUser(String name, String pass) {
        this.pass = pass;
        this.name = name;
    }
    public MyUser( String name){
        this.name = name;
    }
    public MyUser(){}

    public String getPass() {
        return pass;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "MyUser{" +
                "pass='" + pass + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        final MyUser myUser = (MyUser) o;
        return Objects.equals(pass, myUser.pass) && Objects.equals(name, myUser.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(pass, name);
    }
}

with this configurations, I needed add resource for crud operations. for get and delete there is no probelm. but when I add post or put when needs to add new object for body of request, I have error.

post:

@POST
@Path("/")
public Response create(@Auth MyUser admin, Body body) {
   return Response
           .status(Response.Status.OK)
           .type(MediaType.APPLICATION_JSON)
           .entity(true)
           .build();
}

error:

Caused by: 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 javax.ws.rs.core.Response myGroup.resources.BodyResource.create(myGroup.api.MyUser,myGroup.api.Body) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[application/json], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class myGroup.resources.BodyResource, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@20411320]}, definitionMethod=public javax.ws.rs.core.Response myGroup.resources.BodyResource.create(myGroup.api.MyUser,myGroup.api.Body), parameters=[Parameter [type=class myGroup.api.MyUser, source=null, defaultValue=null], Parameter [type=class myGroup.api.Body, source=null, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}', [WARNING] The (sub)resource method create in myGroup.resources.BodyResource contains empty path annotation.; source='public javax.ws.rs.core.Response myGroup.resources.BodyResource.create(myGroup.api.MyUser,myGroup.api.Body)'

Upvotes: 0

Views: 407

Answers (1)

zloster
zloster

Reputation: 1157

Jersey wants the @Path annotation to be at class level. See my answer here: Parse request parameters without writing wrapper class

I don't know which version of dropwizard you are using but I couldn't make the combination of @POST and @Path("/something") annotation to behave when a method is annotated. I'm getting HTTP ERROR 404.

Upvotes: 1

Related Questions