2016-11-29 2 views
0

dropwizard framework을 사용하여 Resful 응용 프로그램을 구현했습니다. 나는 받는다는 패키지 내 인증 dropwizard - 인증 - JWT를 사용put 및 post 메소드 dropwizard-auth-jwt

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

자원에 대한 인증을 추가, 나는 그것이 인증 검사의에 대한 주요 클래스를 사용하여 인증 자 클래스를 구현 sampleAuthenticator을 구현했습니다. 참고 MyUser가 교장 구현 될 경우

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(); } 
    } 
} 

:이 구성으로

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); 
    } 
} 

을, 나는 CRUD 작업을위한 추가 리소스를 필요로했다. get과 delete에는 문제가 없다. 하지만 게시물을 추가하거나 요청시 몸체에 새 객체를 추가해야하는 경우에는 오류가 발생합니다.

게시물 :

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

오류 :

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)'

답변

1

Jersey@Path 주석이 클래스 레벨에 있고 싶어. 내 대답보기 : 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.

+0

tnx. 나는 가지고 있지만 문제는 dropwizard-auth-jwt로 생각합니다. 내가 말했듯이, 나는 어떤 에러도없이 메소드를 가져오고 삭제하지만, 저지는 초기화의 에러를 말합니다. – Divuneh

+0

dropwizard 1.0.5를 사용했습니다. – Divuneh

+0

제가 말했듯이, 문제는 dropwizard-auth-jwt로 생각합니다. 그 때문에 대신 https://github.com/milpol/jwt-dropwizard를 사용합니다. – Divuneh