定制基于Tomcat的Flex认证
首先,我们先来实现基本认证(Basic)
1.编辑Tomcat的conf目录下的tomcat-users.xml添加用户名及角色定义
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="managers"/>
<user username="abc" password="abc" roles="managers"/>
</tomcat-users>
2.编辑BlazeDS的Samples应用的Services-config.xml添加安全设置
<security>
<login-command class="flex.messaging.security.TomcatLoginCommand" server="Tomcat">
<per-client-authentication>false</per-client-authentication>
</login-command>
<security-constraint id="trusted">
<auth-method>Custom</auth-method>
<roles>
<role>managers</role>
</roles>
</security-constraint>
</security>
3.编辑Samples应用的remoting-config.xml对指定的Service添加安全设置
<destination id="consultantService">
<properties>
<source>com.adobe.demo.ConsultantService
</source>
</properties>
<security>
<security-constraint ref="trusted"/>
</security>
</destination>
4.然后修改Samples应用的Web.xml,添加安全设置定义
<security-constraint>
<web-resource-collection>
<web-resource-name>Protected Sample</web-resource-name>
<url-pattern>/messagebroker/amf/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>managers</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<security-role>
<role-name>managers</role-name>
</security-role>
5.创建一个简单的Flex应用,实现自定义的登录
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:view="com.adobe.demo.view.*"
backgroundColor="#FFFFFF"
initialize="creationCompleteHandler();"
>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.messaging.config.ServerConfig;
import mx.rpc.AsyncToken;
import mx.rpc.AsyncResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.messaging.ChannelSet;
// Define a ChannelSet object.
public var cs:ChannelSet;
// Define an AsyncToken object.
public var token:AsyncToken;
// Initialize ChannelSet object based on the
// destination of the RemoteObject component.
private function creationCompleteHandler():void {
if (cs == null)
cs = ServerConfig.getChannelSet(loaderService.destination);
//login
Alert.show("Init");
}
// Login and handle authentication success or failure.
private function ROLogin():void {
// Make sure that the user is not already logged in.
if (cs.authenticated == false) {
token = cs.login("abc", "abc");
// Add result and fault handlers.
token.addResponder(new AsyncResponder(LoginResultEvent, LoginFaultEvent));
}
}
// Handle successful login.
private function LoginResultEvent(event:ResultEvent, token:Object=null):void {
switch(event.result) {
case "success":
authenticatedCB.selected = true;
Alert.show("login successfully");
break;
default:
}
}
// Handle login failure.
private function LoginFaultEvent(event:FaultEvent, token:Object=null):void {
switch(event.fault.faultCode) {
case "Client.Authentication":
default:
authenticatedCB.selected = false;
Alert.show("Login failure: " + event.fault.faultString);
}
}
// Logout and handle success or failure.
private function ROLogout():void {
// Add result and fault handlers.
token = cs.logout();
token.addResponder(new AsyncResponder(LogoutResultEvent,LogoutFaultEvent));
}
// Handle successful logout.
private function LogoutResultEvent(event:ResultEvent, token:Object=null):void {
switch (event.result) {
case "success":
//authenticatedCB.selected = false;
Alert.show("logout successfully");
break;
default:
}
}
// Handle logout failure.
private function LogoutFaultEvent(event:FaultEvent, token:Object=null):void {
Alert.show("Logout failure: " + event.fault.faultString);
}
// Handle message recevied by RemoteObject component.
private function resultHandler(event:ResultEvent):void {
//ta.text += "Server responded: "+ event.result + "\n";
Alert.show("Ok loading data");
}
// Handle fault from RemoteObject component.
private function faultHandler(event:FaultEvent):void {
//ta.text += "Received fault: " + event.fault + "\n";
Alert.show("error loading data");
}
]]>
</mx:Script>
<mx:HBox>
<mx:Button label="Login"
click="ROLogin();"/>
<mx:Button label="Load Data"
click="loaderService.loadConsultants();"/>
<mx:Button label="Logout"
click="ROLogout();"/>
<mx:CheckBox id="authenticatedCB"
label="Authenticated?"
enabled="false"/>
</mx:HBox>
<mx:RemoteObject id="loaderService" destination="consultantService" result="resultHandler(event)" fault="faultHandler(event)" showBusyCursor="true" />
</mx:Application>
接下来我们实现基于JDBC的用户登录认证
1.创建用户管理表
CREATE TABLE users ( username varchar(15) NOT NULL PRIMARY KEY, password varchar(15) NOT NULL ); CREATE TABLE user_roles ( username varchar(15) NOT NULL, role varchar(15) NOT NULL, PRIMARY KEY (username, role)
);
2.复制Mysql 的JDBC Driver到Tomcat lib目录下,编辑Server.xml文件,将Realm的类从UserDatabase
改成JDBCRealm
<!-- Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/ -->
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/consultant_db?user=root&password=root"
userTable="users" userNameCol="username" userCredCol="password"
userRoleTable="user_roles" roleNameCol="role"/>
3.在Samples应用目录下创建Context.xml
<!--
Enable the TomcatValve for custom login support from this webapp.
-->
<Context>
<Realm className="org.apache.catalina.realm.JDBCRealm" />
<Valve className="flex.messaging.security.TomcatValve"/>
</Context>
重新运行上面的Flex客户端,你会发现我们已经可以使用Datbase中的用户名和密码进行验证了。

最近在忙什么,我是李涛,有空email me
litao_free@126.com
<p>改成基于指针的引用就可以了,文中的最后一部分给出了解决方法</p>
这个没什么办法。循环引用是设计上的问题,应该手动避免。
最近比较闲,所以玩玩股票了。如果下周马上台的话,应该是一周阳线,下下周就不好说了,我觉得周K线,应该是十字星。
天亮了,台湾今天也要公布大选了,马英九也要上台了,三通概念股也该继续大涨了,呵呵。下周应该不会那么惨了吧。
我在写Delphi程序,喜欢并佩服你的《Delphi深度探索》:)
希望能成为你的朋友:)
MSN:xifengge@hotmail.com
QQ:744131. 大雪
您好,我目前刚刚接触sqlite,也希望把它移植到vxworks上面去.可惜,这方面的资料太少,自己以前对c工程化的东西做的很少,所以自己光闭门造车,时间花费了,还是没办法取得突破性进展.您能指点我一下吗?
我的QQ为:690053911,邮箱地址为:tanleiyu_81@163.com
非常感激,谢谢!!!!!!
我的email:bestyaq@21cn.com,您能把这程序源码给我吗?很急,研究一星期没有结果,郁闷中!!!
如何改变自绘制的ComboBox 的item的高度,能把item的位图绘制到ComboBox的edit框内吗?
在移植过程中 access函数和fsync函数如何处理
请指教
frank233_cn@yahoo.com.cn
很久没写东西了哈很久没写东西了哈很久没写东西了哈很久没写东西了哈很久没写东西了哈很久没写东西了哈很久没写东西了哈很久没写东西了哈很久没写东西了哈很久没写东西了哈很久没写东西了哈