RCP实践之安全模型

    感谢大家最近对本系列的关注和评论,我会继续完善内容,并且总结教训写出更好的东东来。
    今天谈谈最近在研究的RCP安全模型,其实RCP在诞生之初就是建立在一个非常鲁棒的框架之上的---OSGi,它不但有全新的概念,全新的思路,全新的热插拔技术,还有非常好的安全模型( equinox security 项目好像还在孵化中) 。

    解决RCP的安全问题,从最近的调研中看是有四种方式:
1.equinox security项目;
2.使用org.eclipse.ui.activities扩展点定义可访问权限和角色;
3.使用Eclipse-JAAS插件;
4.自己写代码在对功能点进行过滤;

   第一种方案不可行,因为equinox security项目还在孵化中,而且没有充足的文档,因此放弃;
   第二种方案可行,但是灵活性不够,我们很难在项目开始之初充分的定义出所有的角色以及角色对应的权限,而且还要引入大量的配置,所以也放弃;
   第三种方案可能可行,因为从老外的文档中看出,这个插件是可用的,而且是强大的,它的原理就是定义了一堆自己的扩展点,然后通过分析扩展点,过滤掉不可用的功能点。但是这个插件的更新时间竟然是2005年,N久都没有人管过了~而且还是基于Eclipse3.0的实现,所以也放弃了;
    我选择的是第四种方案,自己写代码对功能点进行过滤,其实思路很简单,我们定义基类,在基类中过滤掉有那些功能点可以被显示,或者不能被显示。
    RCP应用中所需要关注安全点主要是:
1.Action
2.Viewer
3.编辑器
4.透视图
    经过讨论,其实我们的关注点主要集中在Action和透视图,因为透视图总会包含一组Viewer出现所以我们只要在透视图布局类中将我们不需要关注的Viewer过滤掉,那么Viewer就可以被管理起来了,而编辑器的出现也主要是针对Viewer出现的,如果一个Viewer不能显示,那么和它对应的编辑器也永远无法显示,所以编辑器几乎不用考虑。
   其实安全模型的核心就是Action,Viewer,透视图。
   对于Action,存在两种,一种是开发人员自己在代码中创建出来的Action(new Action),另一种就是在plugin.xml中配置的。
  第一种情况的基类:
 1 None.gif package  com.glnpu.dmp.client.platform.core.internal.security.absaction;
 2 None.gif
 3 None.gif import  org.apache.log4j.Logger;
 4 None.gif import  org.eclipse.jface.action.Action;
 5 None.gif import  org.eclipse.jface.resource.ImageDescriptor;
 6 None.gif
 7 None.gif import  com.glnpu.dmp.client.platform.core.internal.security.SecurityManager;
 8 None.gif
 9 ExpandedBlockStart.gifContractedBlock.gif /** */ /**
10InBlock.gif * 继承自<code>Action</code>,在构造方法中加入用户权限判断。
11InBlock.gif * @author lign
12InBlock.gif *
13ExpandedBlockEnd.gif */

14 ExpandedBlockStart.gifContractedBlock.gif public   abstract   class  AbstractSecurityAction  extends  Action  dot.gif {
15InBlock.gif    private Logger log = Logger.getLogger(this.getClass());
16InBlock.gif    
17ExpandedSubBlockStart.gifContractedSubBlock.gif    public AbstractSecurityAction(String text, ImageDescriptor image)dot.gif{
18InBlock.gif        super(text, image);
19InBlock.gif        this.setActionId();
20InBlock.gif        log.debug("当前处理的Action为: " + this.getId());
21InBlock.gif        //权限判断
22InBlock.gif        SecurityManager.securityFiltration(this);
23ExpandedSubBlockEnd.gif    }

24ExpandedSubBlockStart.gifContractedSubBlock.gif    /** *//**
25InBlock.gif     * 实现者必须实现此方法,并在方法内调用setId方法,为Action命名。
26ExpandedSubBlockEnd.gif     */

27InBlock.gif    public abstract void setActionId();
28ExpandedBlockEnd.gif}
开发人员自己创建的Action都继承自AbstractSecurityAction类,在此类的构造方法中,调用SecurityManager.securityFiltration(this);方法,判断当前Action的ID是否与当前用户权限的中的ID相同,如果相同则调用action.setEnabled(true);,否则则是action.setEnabled(false);
ExpandedBlockStart.gif ContractedBlock.gif      /** */ /**
InBlock.gif     * 通过传入的IAction实例的id,判断是否与用户对应的权限相同,如果存在与用户权限中,则此IAction为可视,否则为不可视。
InBlock.gif     * 
@param action
ExpandedBlockEnd.gif     
*/

ExpandedBlockStart.gifContractedBlock.gif    
public   static   void  securityFiltration(IAction action) dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(action!=nulldot.gif{
InBlock.gif            action.setEnabled(
false);
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if(action.getId()!=null && !action.getId().equals("")) dot.gif{
InBlock.gif                log.debug(
"当前处理的Action为 : " + action.getId() + " 当前用户可用Action列表长度为 :" + UserInfo.getInstance().getActionList().size());
ExpandedSubBlockStart.gifContractedSubBlock.gif                
for(String str : UserInfo.getInstance().getActionList()) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if(str.equals(action.getId())) dot.gif{
InBlock.gif                        action.setEnabled(
true);
InBlock.gif                        log.debug(
"当前Action : " + action.getId() + "与用户可用Action列表中Action匹配,此Action可显示");
InBlock.gif                        
return;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

   第二种情况的基类:
 1 None.gif package  com.glnpu.dmp.client.platform.core.internal.security.absaction;
 2 None.gif
 3 None.gif import  org.eclipse.jface.action.IAction;
 4 None.gif import  org.eclipse.jface.viewers.ISelection;
 5 None.gif import  org.eclipse.ui.IEditorActionDelegate;
 6 None.gif import  org.eclipse.ui.IEditorPart;
 7 None.gif
 8 None.gif import  com.glnpu.dmp.client.platform.core.internal.security.SecurityManager;
 9 None.gif
10 ExpandedBlockStart.gifContractedBlock.gif /** */ /**
11InBlock.gif * 实现自<code>IEditorActionDelegate</code>重载selectionChanged方法,在其中加入用户权限判断。
12InBlock.gif * @author lign
13InBlock.gif *
14ExpandedBlockEnd.gif */

15 ExpandedBlockStart.gifContractedBlock.gif public   abstract   class  AbstractSecurityEditorActionDelegate  implements  IEditorActionDelegate  dot.gif {
16InBlock.gif
17ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//* (non-Javadoc)
18InBlock.gif     * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
19ExpandedSubBlockEnd.gif     */

20InBlock.gif    public abstract void setActiveEditor(IAction action, IEditorPart targetEditor);
21InBlock.gif
22ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//* (non-Javadoc)
23InBlock.gif     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
24ExpandedSubBlockEnd.gif     */

25InBlock.gif    public abstract void run(IAction action);
26InBlock.gif
27ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//* 
28InBlock.gif     * 重载方法,加入权限判断重置Action是否可用
29ExpandedSubBlockEnd.gif     */

30ExpandedSubBlockStart.gifContractedSubBlock.gif    public void selectionChanged(IAction action, ISelection selection) dot.gif{
31InBlock.gif        //判断权限
32InBlock.gif        SecurityManager.securityFiltration(action);
33ExpandedSubBlockEnd.gif    }

34InBlock.gif
35ExpandedBlockEnd.gif}
以上是扩展editorAction扩展点时要继承的基类,其他两种情况同它相同。
   这种方法虽然笨拙,但是还是有用的。
   Viewer的显示总是依靠某一个透视图的,因此就有了我们的透视图基类:
 1 None.gif package  com.glnpu.dmp.client.platform.core.internal.security.absperspective;
 2 None.gif
 3 None.gif import  java.util.ArrayList;
 4 None.gif import  java.util.List;
 5 None.gif
 6 None.gif import  org.apache.log4j.Logger;
 7 None.gif import  org.eclipse.ui.IFolderLayout;
 8 None.gif import  org.eclipse.ui.IPageLayout;
 9 None.gif import  org.eclipse.ui.IPerspectiveFactory;
10 None.gif
11 None.gif import  com.glnpu.dmp.client.platform.core.internal.security.SecurityManager;
12 None.gif
13 ExpandedBlockStart.gifContractedBlock.gif /** */ /**
14InBlock.gif * 实现自<code>IPerspectiveFactory</code>,提供标准布局,及左侧为Viewers,底部为Viewers
15InBlock.gif * @author lign
16InBlock.gif *
17ExpandedBlockEnd.gif */

18 ExpandedBlockStart.gifContractedBlock.gif public   abstract   class  AbstractSecurityPerspective  implements  IPerspectiveFactory  dot.gif {
19InBlock.gif    
20InBlock.gif    private Logger log = Logger.getLogger(this.getClass());
21InBlock.gif    
22InBlock.gif    protected String [] leftViewerIds = null;
23InBlock.gif    protected String [] bottomViewerIds = null;
24InBlock.gif    protected String perspectiveName = null;
25InBlock.gif    
26ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//* (non-Javadoc)
27InBlock.gif     * @see org.eclipse.ui.IPerspectiveFactory#createInitialLayout(org.eclipse.ui.IPageLayout)
28ExpandedSubBlockEnd.gif     */

29ExpandedSubBlockStart.gifContractedSubBlock.gif    public void createInitialLayout(IPageLayout layout) dot.gif{
30InBlock.gif        List<String> leftViewerList = getLeftViewers(leftViewerIds);
31InBlock.gif        log.debug("当前用户可用的leftViewer列表长度为 : " + leftViewerList.size());
32ExpandedSubBlockStart.gifContractedSubBlock.gif        if(leftViewerList.size()>0dot.gif{
33InBlock.gif            IFolderLayout folderLayoutLeft = layout.createFolder(this.perspectiveName+"Left", IPageLayout.LEFT, 0.25f, layout.getEditorArea()); 
34ExpandedSubBlockStart.gifContractedSubBlock.gif            for(String str : leftViewerList) dot.gif{
35InBlock.gif                folderLayoutLeft.addView(str);
36InBlock.gif                layout.getViewLayout(str).setCloseable(false);
37ExpandedSubBlockEnd.gif            }

38ExpandedSubBlockEnd.gif        }

39InBlock.gif
40InBlock.gif        List<String> bottomViewerList = getBottomViewers(bottomViewerIds);
41InBlock.gif        log.debug("当前用户可用的bottomViewer列表长度为 : " + bottomViewerList.size());
42ExpandedSubBlockStart.gifContractedSubBlock.gif        if(bottomViewerList.size()>0dot.gif{
43InBlock.gif            IFolderLayout&n
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值