Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101923891
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类:

2008-04-14 13:27:06

    来源:developerWorks 中国    作者:Samantha Tran Manoj Mohan

XA 应用程序中的受信任连接

应用程序可以在分布式事务中使用受信任连接。XA 分布式事务处理为每个进程启动一个应用服务器。在每个应用服务器进程中,可以使用 XA API(xa_open)建立连接。本节描述环境的配置和在含受信任上下文的环境下运行 DB2 CLI 应用程序的一些考虑事项。

通过以下方法,可以为另一个用户建立和切换受信任连接:

将 xa_open 字符串中的 TCTX 参数设置为 true 或 false,以表明客户机是否在受信任上下文模式中运行。

然后,应用程序必须调用 SQLConnect() 将 Transaction Manager(TM)打开的连接与 CLI/Open Database Connectivity(ODBC)连接句柄相关联。应用程序可以在 SQLConnect 字符串中指定用户 ID 和密码。

随后,应用程序可以调用 xa_start 将一个事务 ID(XID)传递给 Resource Manager(RM),将调用者线程与一个事务分支相关联。

为了切换一个受信任连接上的用户,应用程序必须首先调用 xa_end (TM_SUCCESS),并通过调用 SQLSetConnectAttr 指定新的用户 ID 和可选的密码。

这个例子展示如何在 XA 应用程序环境中启用一个受信任上下文和切换用户 ID。为了建立与数据库服务器的受信任连接,应用程序必须以 TCTX=TRUE 设置调用 xa_open。在使用 SQLConnect 字符串建立一个受信任连接之后,应用程序可以调用 SQLSetConnectAttr,并将 SQL_ATTR_TRUSTED_CONTEXT_USERID 设置为 newton 来切换用户 ID。一旦应用程序调用 xa_start 开始事务,接下来的工作就是在受信任用户 ID newton 之下进行的。当执行了 xa_close 之后,底层的受信任连接不复存在。如果 CLI 句柄仍然存在,它也不再被标记为受信任连接,因为当创建连接时,XA 在 xa_open 期间执行受信任上下文设置。xa_close 断开受信任上下文,CLI 建立的任何未使用 XA 的新连接都不受信任。

例 3. 在 XA 应用程序中使用受信任连接
					
#---------------------------------------------------------------------------
#-- db2cli example
#-- Test XA with Trusted Context on the connection
#---------------------------------------------------------------------------

#-- Allocate the environment handle
sqlallocenv 1

#-- Set the Trusted Context bit, System Authid and Password
xaopen 10 "DB=stlec1,sreg=t,SPM=domino,TCTX=TRUE,uid=zurbie,PWD=xxxxxxxx" TMNOFLAGS

#-- Allocate the connection handle
sqlallocconnect 1 1
sqlconnect 1 stlec1 -3 zurbie -3 xxxxxxxx -3

#-- switch the userid to newton & set the password
sqlsetconnectattr 1 SQL_ATTR_TRUSTED_CONTEXT_USERID newton
sqlsetconnectattr 1 SQL_ATTR_TRUSTED_CONTEXT_PASSWORD yyyyy

#-- Start a transaction
#-- This will switch the user to newton
xastart 10 99 gtrid bqual TMNOFLAGS

sqlgetconnectattr 1 SQL_ATTR_USE_TRUSTED_CONTEXT

#-- Allocate the statement handle and do some work
sqlallocstmt 1 1

sqlexecdirect 1 "create table temp (int1 int)" -3

sqlexecdirect 1 "insert into temp values ( -99 )" -3

sqlexecdirect 1 "select * from temp" -3
fetchall 1
sqlclosecursor 1

sqlexecdirect 1 "delete from temp where int1 < 0" -3

sqlfreestmt 1 SQL_DROP

#-- Commit the transaction using 2PC
xaend 10 99 gtrid bqual TMSUCCESS
xaprepare 10 99 gtrid bqual TMNOFLAGS
xacommit 10 99 gtrid bqual TMNOFLAGS

#-- Disconnect and free the connection handle
sqldisconnect 1
sqlfreeconnect 1

xaclose 10 TMNOFLAGS

#-- Free the environment handle
sqlfreeenv 1

JDBC 应用程序中的受信任连接

IBM DB2 Driver for JDBC 和 SQLJ 提供了允许在 Java 程序中建立和使用受信任连接的方法。为了避免对安全漏洞的攻击,使用这些受信任方法的应用服务器不应该使用不受信任的连接方法。

DB2ConnectionPoolDataSource 类提供了几种版本的 getDB2TrustedPooledConnection 方法,DB2XADataSource 类提供了几种版本的 getDB2XAConnection 方法,这些方法使应用服务器可以建立初始受信任连接。可以根据传递的连接属性的类型以及是否使用 Kerberos 安全性,选择其中一个方法。当应用服务器调用其中一个方法时,IBM DB2 Driver for JDBC 和 SQLJ 返回一个包含两个元素的 Object[] 数组:

第一个元素包含初始连接的一个连接实例。

第二个元素包含连接实例的一个惟一的 cookie。这个 cookie 是由 JDBC 驱动程序生成的,用于随后的连接重用的身份验证。

DB2PooledConnection 类提供了几种版本的 getDB2Connection 方法,DB2Connection 类提供了几种版本的 reuseDB2Connection 方法,这些方法使应用服务器可以以新用户的身份重用已有的受信任连接。应用服务器使用该方法将以下项目传递给新用户:

来自初始连接的 cookie。

被重用连接的新的连接属性。

JDBC 驱动程序检查提供的 cookie 是否与底层受信任物理连接相匹配,以确保连接请求是由建立受信任的物理连接的应用服务器发起的。如果 cookie 匹配,则这个新用户可以直接用新的连接属性使用该连接。

例 4. 在 JDBC 应用程序中使用受信任连接

#---------------------------------------------------------------------------
#-- JDBC example
#-- Test a Trusted Context on the connection
#---------------------------------------------------------------------------

/* The first item that was obtained from the previous */ 
getTrustedPooledConnection

/* Call is a connection object. Cast it to a PooledConnection object. */
javax.sql.PooledConnection pooledCon = (javax.sql.PooledConnection)objects[0];
properties = new java.util.Properties();

// Set new properties for the reused object using
// properties.put("property", "value");
// The second item that was obtained from the previous
getTrustedPooledConnection

/* call is the cookie for the connection. Cast it as a byte array. */
byte[] cookie = ((byte[])(objects[1]);

/* Supply the user ID for the new connection. */
String newuser = "newuser";

// Supply the name of a mapping service that maps a workstation user
// ID to a z/OS RACF ID
String userRegistry = "registry";

/* Do not supply any security token data to be traced. */
byte[] userSecTkn = null;

/* Do not supply a previous user ID. */
String originalUser = null;

// Call getDB2Connection to get the connection object for the new
// user.
java.sql.Connection con = ((com.ibm.db2.jcc.DB2PooledConnection)pooledCon).
getDB2Connection(cookie,newuser,password,userRegistry,userSecTkn,originalUser,properties);

结束语

在电子商务领域,很多应用程序依赖于 Domino 和 WebSphere Application Server 之类的中间件服务器提供的安全性,而受信任上下文是为那些应用程序提供安全环境,同时又不必过多降低性能的理想方法。可以通过调优受信任上下文的安全属性,确保对数据库服务器的无懈可击的访问。而且,由于可以以很少的连接资源切换用户,而不需要身份验证,受信任上下文是商业应用程序的理想选择。

阅读(423) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~