2017-02-12 12 views
0

私は粗粒度の認可についてのRestletチュートリアルの例に取り組んでいます:私はhttp://localhost:9000/v1/resourceTypePublic/resource1/資源に行くときリクエストからユーザーの資格情報を取得するにはどうすればよいですか?

public class Resource1 extends ServerResource{ 

@Get 
public String represent() throws Exception { 
    User user = getRequest().getClientInfo().getUser(); 
    String identifier = user.getIdentifier(); 
    char[] pass = user.getSecret(); 

    return this.getClass().getSimpleName() + " found ! User: " + identifier + 
      "; password = " + charArrayToString(pass) ; 

} 

private String charArrayToString(char[] chars) { 
String result = ""; 
for (char c : chars){ 
    result += c; 
} 
return result; 

} 

} 

:私は、ユーザーの資格情報をチェックするためのクラスを作成

public class MyApiWithRoleAuthorization extends Application { 

//Define role names 
public static final String ROLE_USER = "user"; 
public static final String ROLE_OWNER = "owner"; 

@Override 
public Restlet createInboundRoot() { 
    //Create the authenticator, the authorizer and the router that will be protected 
    ChallengeAuthenticator authenticator = createAuthenticator(); 
    RoleAuthorizer authorizer = createRoleAuthorizer(); 
    Router router = createRouter(); 

    Router baseRouter = new Router(getContext()); 

    //Protect the resource by enforcing authentication then authorization 
    authorizer.setNext(Resource0.class); 
    authenticator.setNext(baseRouter); 

    //Protect only the private resources with authorizer 
    //You could use several different authorizers to authorize different roles 
    baseRouter.attach("/resourceTypePrivate", authorizer); 
    baseRouter.attach("/resourceTypePublic", router); 
    return authenticator; 
} 

private ChallengeAuthenticator createAuthenticator() { 
    ChallengeAuthenticator guard = new ChallengeAuthenticator(
      getContext(), ChallengeScheme.HTTP_BASIC, "realm"); 

    //Create in-memory users with roles 
    MemoryRealm realm = new MemoryRealm(); 
    User user = new User("user", "user"); 
    realm.getUsers().add(user); 
    realm.map(user, Role.get(this, ROLE_USER)); 
    User owner = new User("owner", "owner"); 
    realm.getUsers().add(owner); 
    realm.map(owner, Role.get(this, ROLE_OWNER)); 

    //Attach verifier to check authentication and enroler to determine roles 
    guard.setVerifier(realm.getVerifier()); 
    guard.setEnroler(realm.getEnroler()); 
    return guard; 
} 

private RoleAuthorizer createRoleAuthorizer() { 
    //Authorize owners and forbid users on roleAuth's children 
    RoleAuthorizer roleAuth = new RoleAuthorizer(); 
    roleAuth.getAuthorizedRoles().add(Role.get(this, ROLE_OWNER)); 
    roleAuth.getForbiddenRoles().add(Role.get(this, ROLE_USER)); 
    return roleAuth; 
} 

private Router createRouter() { 
    //Attach Server Resources to given URL 
    Router router = new Router(getContext()); 
    router.attach("/resource1/", Resource1.class); 
    router.attach("/resource2/", Resource2.class); 
    return router; 
} 

public static void main(String[] args) throws Exception { 
    //Attach application to http://localhost:9000/v1 
    Component c = new Component(); 
    c.getServers().add(Protocol.HTTP, 9000); 
    c.getDefaultHost().attach("/v1", new MyApiWithRoleAuthorization()); 
    c.start(); 
} 

}

をアプリケーションが資格情報を要求し、 "user"、 "user"(または "owner"、 "owner")を入力します。しかし、内部サーバーエラーが発生します。理由は、return文での可変パスがnull値を持つためです。この変数のないステートメントは正常に機能します:

return this.getClass().getSimpleName() + " found ! User: " + identifier; 

返されます。しかし、その秘密はどうですか?ユーザーのシークレットが入力されたにもかかわらず、null値を返すのはなぜですか?

答えて

0

Userオブジェクトは、それがフィールド秘密を持っているにもかかわらず、パスワードに関する情報が含まれていない文

User user = getRequest().getClientInfo().getUser();

で作成しました。ユーザー資格情報を取得する別の方法があります。

char[] pass = getChallengeResponse().getSecret();

関連する問題