让你的JXTA应用更加安全.建立真正你自己的私有的组!

在JXTA应用中有对等组的概念,当你加入了对等组之后.你在这个对等组内发布的信息就只有这个组的成员才能收到,所谓的成员资格服务^_^.(差不多这个意思吧.不完全对.)
但是你不能建立你自己的应用中所私有的组的话.那么别人通过搜索 * 匹配来得到你的组通告就可以加入你建立的对等组.如果在你告诉了我你自己的应用中组的名字的话.我就可以自己写一个应用来加入你的组进行任意的破坏.^_^.
那么不就很不安全了吗?应用的维护管理也太不方便了.
虽然我之前写过哪个关于通过认证加入对等组的文章,但是哪个就是不安全.
其实可以通过建立ModuleImplAdvertisement 来为你的对等组增加帐号密码的认证

代码如下:

    private ModuleImplAdvertisement createPasswdMembershipPeerGroupModuleImplAdv(PeerGroup rootPeerGroup) {
        ModuleImplAdvertisement allPurposePeerGroupImplAdv
=null;
        
try {
            allPurposePeerGroupImplAdv
=rootPeerGroup.getAllPurposePeerGroupImplAdvertisement();
        }
 catch (java.lang.Exception e) {
            System.exit(
1);
        }

        ModuleImplAdvertisement passwdMembershipPeerGroupModuleImplAdv
=allPurposePeerGroupImplAdv;
        ModuleImplAdvertisement passwdMembershipServiceModuleImplAdv
=null;
        StdPeerGroupParamAdv passwdMembershipPeerGroupParamAdv
=null;

        
try {
            passwdMembershipPeerGroupParamAdv 
=
                
new StdPeerGroupParamAdv(allPurposePeerGroupImplAdv.getParam());
        }
 catch (net.jxta.exception.PeerGroupException e) {
            System.exit(
1);
        }


        Hashtable allPurposePeerGroupServicesHashtable 
= passwdMembershipPeerGroupParamAdv.getServices();
        Enumeration allPurposePeerGroupServicesEnumeration 
= allPurposePeerGroupServicesHashtable.keys();
        
boolean membershipServiceFound=false;
        
while ((!membershipServiceFound) && (allPurposePeerGroupServicesEnumeration.hasMoreElements())) {
            Object allPurposePeerGroupServiceID 
= allPurposePeerGroupServicesEnumeration.nextElement();
            
if (allPurposePeerGroupServiceID.equals(PeerGroup.membershipClassID)) {
                ModuleImplAdvertisement allPurposePeerGroupMemershipServiceModuleImplAdv
= (ModuleImplAdvertisement) allPurposePeerGroupServicesHashtable.get(allPurposePeerGroupServiceID);
                passwdMembershipServiceModuleImplAdv
=this.createPasswdMembershipServiceModuleImplAdv(allPurposePeerGroupMemershipServiceModuleImplAdv);
                allPurposePeerGroupServicesHashtable.remove(allPurposePeerGroupServiceID);
                allPurposePeerGroupServicesHashtable.put(PeerGroup.membershipClassID,passwdMembershipServiceModuleImplAdv);
                membershipServiceFound
=true;

                passwdMembershipPeerGroupModuleImplAdv.setParam((Element) passwdMembershipPeerGroupParamAdv.getDocument(
new MimeMediaType("text/xml")));

                
if (!passwdMembershipPeerGroupModuleImplAdv.getModuleSpecID().equals(PeerGroup.allPurposePeerGroupSpecID)) {
                    passwdMembershipPeerGroupModuleImplAdv.setModuleSpecID(IDFactory.newModuleSpecID(passwdMembershipPeerGroupModuleImplAdv.getModuleSpecID().getBaseClass()));
                }
 else {
                    ID passwdGrpModSpecID
= ID.nullID;
                    
try {
                        passwdGrpModSpecID 
= IDFactory.fromURL(new URL("urn","","jxta:uuid-"+"DeadBeefDeafBabaFeedBabe00000001" +"04" +"06" ) );
                    }
 catch (java.net.MalformedURLException e) {}
                    
catch (java.net.UnknownServiceException ee) {}
                    passwdMembershipPeerGroupModuleImplAdv.setModuleSpecID((ModuleSpecID) passwdGrpModSpecID);
                }
 
                membershipServiceFound
=true;
            }
 
        }


        
return passwdMembershipPeerGroupModuleImplAdv;
    }


    
private  ModuleImplAdvertisement createPasswdMembershipServiceModuleImplAdv( ModuleImplAdvertisement allPurposePeerGroupMemershipServiceModuleImplAdv) {
        ModuleImplAdvertisement passwdMembershipServiceModuleImplAdv 
= (ModuleImplAdvertisement) AdvertisementFactory.newAdvertisement(ModuleImplAdvertisement.getAdvertisementType());
        passwdMembershipServiceModuleImplAdv.setModuleSpecID(PasswdMembershipService.passwordMembershipSpecID);
        passwdMembershipServiceModuleImplAdv.setCode(PasswdMembershipService.
class.getName());
        passwdMembershipServiceModuleImplAdv.setDescription(
" Module Impl Advertisement for the PasswdMembership Service");
        passwdMembershipServiceModuleImplAdv.setCompat(allPurposePeerGroupMemershipServiceModuleImplAdv.getCompat());
        passwdMembershipServiceModuleImplAdv.setUri(allPurposePeerGroupMemershipServiceModuleImplAdv.getUri());
        passwdMembershipServiceModuleImplAdv.setProvider(allPurposePeerGroupMemershipServiceModuleImplAdv.getProvider());
        
return passwdMembershipServiceModuleImplAdv;
    }

好了.之后你就可以利用这个ModuleImplAdvertisement 来建立你的应用中专用的对等组了.

PS:关于如何让JXTA在不同的网络中进行通讯的问题,虽然JXTA里面已经说明了自己有这样的能力可以做到.但是到底是如何实现呢? 下次再说 ^_^
posted @ 2006-08-30 19:51  T.t.T!Ck.¢#  阅读(951)  评论(0编辑  收藏  举报