Ich implementiert Resful-Anwendung mit dropwizard framework. Ich benutze dropwizard-auth-jwt für meine Authentifizierung mit Maven Paket:Put-und Post-Methode Dropwizard-Auth-JWT
<dependency>
<groupId>com.github.toastshaman</groupId>
<artifactId>dropwizard-auth-jwt</artifactId>
<version>1.0.2-0 </version>
</dependency>
für das Hinzufügen von Authentifizierung für Ressourcen, ich implementiert sampleAuthenticator die Authenticator-Klasse implementiert wird, die Hauptklasse verwendet dafür Authentifizierungsprüfung ist.
public class UserAuthenticate implements Authenticator <JwtContext, MyUser> {
@Override
public Optional<MyUser> 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(); }
}
}
wenn MyUser Haupt umgesetzt:
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);
}
}
mit diesen Konfigurationen, brauchte ich Add-Ressource für CRUD-Operationen. Zum Abrufen und Löschen gibt es kein Problem. aber wenn ich hinzufügen oder put wenn muss neues Objekt für Körper der Anfrage hinzufügen, ich habe einen Fehler.
Beitrag:
@POST
@Path("/")
public Response create(@Auth MyUser admin, Body body) {
return Response
.status(Response.Status.OK)
.type(MediaType.APPLICATION_JSON)
.entity(true)
.build();
}
Fehler:
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=[[email protected]]}, 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)'
tnx. Ich habe es aber ich denke, Problem ist mit Dropwizard-Auth-JWT. Wie ich schon sagte, habe ich Methoden ohne Fehler zu bekommen und zu löschen, aber wenn ich Post oder Put, Jersey sagt der Fehler in der Initialisierung – Divuneh
Ich habe Dropwizard 1.0.5 – Divuneh
Wie gesagt, ich denke, das Problem ist mit Dropwizard-Auth-JWT und deshalb verwende ich https://github.com/milpol/jwt-dropwizard stattdessen. – Divuneh