随笔-193  评论-715  文章-1  trackbacks-0

环境:
BlazeDS 3.2.0.3978
Tomcat 6.0.29 (本例在Tomcat 5.5中不能正常运行,因为Tomcat 5.5的context部分的设计与6.0不一样)
Java5

1,在service-config.xml中加入认证和角色(组)的定义:
<security>
        
<login-command class="flex.messaging.security.TomcatLoginCommand" server="Tomcat" />
        
<security-constraint id="trusted">
        
<auth-method>Basic</auth-method>
            
<roles>
                
<role>tomcat</role>
            
</roles>
        
</security-constraint>
        
<security-constraint id="users">
            
<auth-method>Basic</auth-method>
            
<roles>
                
<role>role1</role>
                
<role>tomcat</role>
            
</roles>
        
</security-constraint>
    
</security>
需要说明的是,这里的security-constraint tag相当于一个角色组,其id属性就是角色组的ID,如users角色组包含role1和tomcat两个角色。
auth-method tag表示认证的方式,Basic即采用HTTP Basic认证方式,也可以自己实现认证方式,可调定为Custom,关于此种认证方式,我将在稍后分享给大家。

这些角色和用户,均被定义在Tomcat的conf/tomcat-users.xml配置中:
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<!--
  NOTE:  By default, no user is included in the "manager" role required
  to operate the "/manager" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.
-->
<!--
  NOTE:  The sample user and role entries below are wrapped in a comment
  and thus are ignored when reading this file. Do not forget to remove
  <!.. ..> that surrounds them.
-->

  
<role rolename="tomcat"/>
  
<role rolename="role1"/>
  
<role rolename="manager"/>
  
<user username="admin" password="admin" roles="manager"/>
  
<user username="tomcat" password="tomcat" roles="tomcat"/>
  
<user username="both" password="tomcat" roles="tomcat,role1"/>
  
<user username="role1" password="tomcat" roles="role1"/>

</tomcat-users>


2,在remote-config.xml中配置具体的destination的权限约束:
<destination id="Domain">
        
<properties>
            
<source>com.robin.service.domain.DomainService</source>
            
<include-methods>
            
<method name="getAllDomains"/>
            
<method name="addOrUpdateDomain" security-constraint="trusted"/>
            
</include-methods>
        
</properties>
        
<security>
            
<security-constraint ref="users"/>
        
</security>
    
</destination>
在此配置中,定义所有的方法只要是users角色组中的用户即可访问,但是addOrUpdateDomain方法需要trusted角色组中的用户才能访问。

3,将BlazeDS与Tomcat结合所依赖的lib复制到lib目录中,包含flex-tomcat-common.jar和flex-tomcat-server.jar两个JAR。

4,修改Tomcat目录下的conf/context.xml文件,加入如下Context(在Tomcat5.5中无法成功):
<Valve className="flex.messaging.security.TomcatValve"/>

好了,就这么简单,重启Tomcat,即可验证了。

总结一下:
1,引入了角色组的概念,这个比较好。
2,角色和用户定义依赖于Tomcat,无法定义在如DBMS中,不方便修改。
3,跨平台性不好。
4,资源与权限的映射关系在开发时已经绑定死,无法动态修改。


PS:2010.4.28. 分享给大家不依赖Tomcat容器实现认证与授权的例子,如有需要,请参考:《BlazeDS自定义认证与权限控制

本Blog所有内容不得随意转载,版权属于作者所有。如需转载请与作者联系( fastzch@163.com    QQ:9184314)。
未经许可的转载,本人保留一切法律权益。
一直以来,发现有某些人完全不尊重我的劳动成果,随意转载,提醒一下那些人小心哪天惹上官司。

posted on 2010-04-27 09:56 Robin's Programming World 阅读(2313) 评论(0)  编辑  收藏 所属分类: JavaFlex & Flash

只有注册用户登录后才能发表评论。


网站导航: