|
irk RestSSN:999999999 [ADMIN]
denyseAccountant@accumulouniversitydata> user chiefDoug
Enter password foruser chiefDoug: *********
chiefDoug@accumulouniversitydata> scan
Kirk Rest PoliceCharge:Curfew Violation [ADMIN|POLICE] Pending Hearing
Kirk Rest Police Charge:DUI Arrest[ADMIN|POLICE] Guilty
正如从代码清单12-3中看到的那样,Accumulo能够基于每个用户的授权控制来限制访问,你已经通过使用Accumuloshell演示了这一点。现在,让我们用Java构建一个Accumulo客户端,该客户端将为本例演示相同级别的访问控制,并与企业中的身份和访问管理基础设施相集成。
当涉及与企业基础设施集成时,Accumulo有一个灵活的模型。如前面提到的,Accumulo客户端的职责是认证用户和获取用户的授权凭据,并将其展示给Accumulo以供处理之用。只要使用企业属性存储中的相同属性或角色标记了Accumulo表中的数据可见性,它就可以优雅地工作。如果没有标记的话,那么很可能需要对从客户端属性存储中获取的属性进行一些处理,以确保它们是完全相同的角色。
为了演示这一点,让我们浏览一个如何编写简单客户端的示例,该客户端通过自选的认证机制来认证用户,并从自选的属性服务或LDAP目录拉取授权凭据。
代码清单12-4展示了一个编写连接到Accumulo的Java类的示例。为了连接,必须使用Connector类建立一个Accumulo连接。为此,必须首先通过初始化ZookeeperInstance类,连接到跟踪Accumulo的Zookeeper实例,而ZookeeperInstance类将会返回一个连接器。
代码清单12-4:Accumulo客户端代码示例
import java.util.Collection;
importjava.util.Collections;
importjava.util.Map.Entry;
import org.apache.accumulo.core.client.Connector;
importorg.apache.accumulo.core.client.ZooKeeperInstance;
importorg.apache.accumulo.core.client.Scanner;
importorg.apache.accumulo.core.data.Key;
importorg.apache.accumulo.core.data.Range;
importorg.apache.accumulo.core.data.Value;
importorg.apache.accumulo.core.security.Authorizations;
public classQueryExample
{
public static void main(String[] args)throws Exception
{
//Simple Example of the name of youraccumulo instance & zookeeper
ZooKeeperInstanceinst = new ZooKeeperInstance("accumulo", "localhost");
//Obviously this is just an example
Connector connector =inst.getConnector("root", "secret");
//Scan in the username and password forthis simple example
java.util.Scanner in = newjava.util.Scanner(System.in);
System.out.println("Username:");
String username = in.nextLine();
System.out.println("Password:");
String password = in.nextLine();
Authorizations auths = null;
try
{
//An example of how you can interactwith other systems (LDAP,etc)
CustomAuthenticator authenticator = newCustomAuthenticator();
authenticator.authenticate(username,password);
//Retrieve credentials from externalsystem
auths =authenticator.getAuthorizationInfo(username);
}
catch (ExceptionauthenticationException)
{
System.out.println("AuthenticationFailure.");
System.exit(-1);
}
// Search our university data example& print out everything
Scanner scanner =connector.createScanner("universitydata", auths);
for (Entry entry :scanner) {
System.out.println( entry.getKey().toString());
}
}
}
一旦建立了连接,就要认证用户。在这种情况下,对于这样一个简单的示例,从命令行抓取用户信息,并将其传递给一个叫做CustomAuthenticator的外部类,编写此类仅是为了展示可以使用Accumulo之外的另一种认证和授权机制。在该类中,将从命令行中扫描到的用户名和密码传入该类的authenticate()方法。如果用户认证成功,那么接着从外部存储拉取用户的授权凭据,并返回Accumulo期望的org.apache.accumulo.core.security. Authorizations类中的值。最后,如之前示例中所示,创建一个扫描器对相同表中的值进行迭代,并简单地打印结果。
代码清单12-5展示了命令行中的结果。在此示例中,设置了一个外部的LDAP目录,其中包含一个叫做joeUser的用户,其角色为ADMIN。
代码清单12-5:Accumulo客户端的结果
Script started on Fri 03 May 2013 12:45:09 AM EDT
$ java QueryExample
13/05/03 00:45:16INFO zookeeper.ZooKeeper:
Clientenvironment:zookeeper.version=3. |