面向 Java 开发人员的 db4o 指南: 事务、分布和安全性
2010-04-01 00:00:00 来源:WEB开发网安全性
自然地,当涉及一个端口时,就得考虑安全性,因为您不能允许 “所有人” 连接服务器并进行查询。在传统的 RDBMS 实现中,供应商提供了一个强大的安全模型,当数据库连接打开时,要访问数据库实例的部分或所有内容,必须向数据库发送凭证(用户名和密码)。
db4o 实现也是这样,至少在效果上是一样的。但是 db4o 数据库实例创建者设置授权安全策略的方法与常见 RDBMS 场景有很大区别,如清单 2 所示:
清单 2. 和其他人进行通信
@Test public void letsTryMultipleNetworkClientConnections()
{
ObjectServer server = Db4o.openServer("persons.data", 2771);
server.grantAccess("client1", "password");
server.grantAccess("client2", "password");
// Yes, "password" is a bad password. Don't do this in production
// code. I get to do it only because I have a special Pedagogical
// Code License from Sun Microsystems. And you don't. So don't do
// this. Don't make me come over there. I'm serious.
// Fuggedaboutit. Never. Not ever. Capice?
try
{
ObjectContainer client1 =
server.openClient("localhost", 2771, "client1", "password");
ObjectContainer client2 =
server.openClient("localhost", 2771, "client1", "password");
Employee ted1 = (Employee)
client1.get(
new Employee("Ted", "Neward", null, null, 0, null))
.next();
System.out.println("client1 found ted: " + ted1);
Employee ted2 = (Employee)
client2.get(
new Employee("Ted", "Neward", null, null, 0, null))
.next();
System.out.println("client2 found ted: " + ted2);
ted1.setTitle("Lord and Most High Guru");
client1.set(ted1);
System.out.println("set(ted1)");
System.out.println("client1 found ted1: " +
client1.get(
new Employee("Ted", "Neward", null, null, 0, null))
.next());
System.out.println("client2 found ted2: " +
client2.get(
new Employee("Ted", "Neward", null, null, 0, null))
.next());
client1.commit();
System.out.println("client1.commit()");
System.out.println("client1 found ted1: " +
client1.get(
new Employee("Ted", "Neward", null, null, 0, null))
.next());
System.out.println("client2 found ted2: " +
client2.get(
new Employee("Ted", "Neward", null, null, 0, null))
.next());
client2.ext().refresh(ted2, 1);
System.out.println("After client2.refresh()");
System.out.println("client2 found ted2: " +
client2.get(
new Employee("Ted", "Neward", null, null, 0, null))
.next());
client1.close();
client2.close();
}
finally
{
server.close();
}
}
更多精彩
赞助商链接