2017-02-05 5 views
0

管理者以外のユーザーが自分のデータだけにアクセスできるようにするユーザーリソースがあるDropwizardアプリケーションがあります。私はまた、 "管理者"が任意のユーザーのデータにアクセスできるようにしたい。Dropwizardでパス内のIDに基づいてリソースへのアクセスを制御する方法

@GET 
@Path("https://stackoverflow.com/users/{userId}") 
@RolesAllowed(value="admin") 
public Response getUser(@Auth final Client client, @PathParam("userId") final String userId) 
     throws InterruptedException { 
    return userDAO.getUser(userId); 
} 

私はDropwizardのAuthorizer<Principal>インターフェイスを実装しているし、これは、少なくともリソースにアクセスするには、「管理者」の役割を持つユーザーを許可します。

@Override 
public boolean authorize(Principal principal, String allowedRolesForResource) { 
    Set<Roles> userRoles = ((Client) principal).getRoles(); 
    String userId = ((Client) principal).getUserId(); 

    // Create set of all the allowed roles for the resource 
    Set<Roles> allowedRoles = Arrays.asList(allowedRolesForResource.split("\\s*,\\s*")) 
      .stream() 
      .map(Roles::fromName) 
      .collect(Collectors.toSet()); 

    if(Collections.disjoint(userRoles, allowedRoles)) { 
     LOGGER.info("User {} does not have any of the allowed roles [{}] for the resource", userId, allowedRolesForResource); 
     return false; 
    } 
    return true; 
} 

しかし、管理者以外のユーザーが自分のuserIdのリソースにアクセスする権限をどのように認めることができるかわかりません。私はauthorizeメソッドの範囲に個人のuserIdを持っていますが、要求されているリソースパスはありません。つまり/ users/123です。

要求されたリソースパスとユーザーのIDに基づいてアクセスを許可できるように、要求コンテキストをAuthorizerクラスのスコープに取得する方法はありますか?

答えて

0

ただ、みんなのために管理者のパスとを作成

@GET 
@Path("https://stackoverflow.com/users/{userId}") 
@RolesAllowed(value="admin") 
public Response getUser(@Auth final Client client, @PathParam("userId") final String userId) 
     throws InterruptedException { 
    return userDAO.getUser(userId); 
} 


@GET 
@Path("/user") 
public Response getUser(@Auth final Client client) 
     throws InterruptedException { 
    return userDAO.getUser(client.getUserId()); 
} 
関連する問題