读书人

JXTA中经过身份认证来加入“群”(三)

发布时间: 2012-10-19 16:53:37 作者: rapoo

JXTA中通过身份认证来加入“群”(三)

本文是接前面两篇,结束本篇就完成密码对等组创建,发现及加入的过程。

发现一个对等组在以前关于JXTA中的文章中都有说到,没有什么大的差别,在这里主要是注意验证器如何验证用户和密码的过程,不过也相对比较简单。代码的注释中都有详细的说明。直接秀出代码:

1、搜索对等组方法

?//搜索到该“群”
?private PeerGroup discoveryPeerGroup(PeerGroup netpg,
???PeerGroupID satellaPID) {
??//要加入”群“,首先,得搜索到”群“
??//在很多情况下,我们一般使用发现监听及时发现
??//在这里我不这样,因为已经确定对等组广告是在本地缓存中,相对比较简单
??PeerGroup satellaPeerGroup = null;
??DiscoveryService netpgService = null;
??if(null!=netpg){
???netpgService = netpg.getDiscoveryService();
??}else{
???System.out.println("父对等组为空,不能加入目标对等组");
???System.exit(-1);
??}
??boolean isGroupFound = false;
??Enumeration localPGAdv = null;
??PeerGroupAdvertisement spgAdv = null;
??while(!isGroupFound){
???try {
????localPGAdv = netpgService.getLocalAdvertisements(DiscoveryService.GROUP, "GID", satellaPID.toString());
???} catch (IOException e) {
????System.out.println("本地没有发现该对等组广告");
????e.printStackTrace();
???}
???if(null!=localPGAdv){
????while(localPGAdv.hasMoreElements()){
?????PeerGroupAdvertisement pgAdv = null;
?????pgAdv = (PeerGroupAdvertisement)localPGAdv.nextElement();
?????if(pgAdv.getPeerGroupID().equals(satellaPID)){
??????spgAdv = pgAdv;
??????isGroupFound = true;
??????break;
?????}
????}
???}
???try {
????Thread.sleep(5*1000);
???} catch (InterruptedException e) {
????// TODO Auto-generated catch block
????e.printStackTrace();
???}
??}
??try {
???satellaPeerGroup = netpg.newGroup(spgAdv);
??} catch (PeerGroupException e) {
???System.out.println("不能用对等组广告创建目标对等组");
???e.printStackTrace();
???return null;
??}
??return satellaPeerGroup;
?}

2、验证器对身份认证

?/**
? * 要执行该方法而且是符合要求完认证后才能加入到对等组。为了完成验证,验证方法需求从验证器(auth)中提取。
? * 这些方法的名称开始是"setAuth"。其中setAuth1Identity方面支持验证login,而setAuth2_Password支持验证passwd
? * @param auth
? * @param login
? * @param passwd
? */
?private void completeAuth(Authenticator auth, String login, String passwd) {
??Method[] methods = auth.getClass().getMethods();
??Vector authMethods = new Vector();
??//下面for循环只是对auth中的方法过滤和排序
??for(int eachMethod = 0;eachMethod<methods.length;eachMethod++){
???if(methods[eachMethod].getName().startsWith("setAuth")){
????if(Modifier.isPublic(methods[eachMethod].getModifiers())){
?????for(int doInsert = 0;doInsert<=authMethods.size();doInsert++){
??????int insertHere = 1;
??????if(doInsert == authMethods.size()){
???????insertHere = doInsert;
??????}else{
???????if(methods[eachMethod].getName().compareTo(((Method)authMethods.elementAt(doInsert)).getName())<=0){
????????insertHere = doInsert;
???????}
??????}
??????if(-1!=insertHere){
???????authMethods.insertElementAt(methods[eachMethod], insertHere);
???????break;
??????}//end if (-1 != insertHere)
?????}//end for(int doInsert =0
????}//end if (modifier.isPublic
???}//end if (methods[eachMethod]
??}//end for (int eachMethod)
??
??Object[] AuthId = {login};
??Object[] AuthPasswd = {passwd};
??//重点是下面,调用验证器的验证方法对用户和密码进行确认
??for(int eachAuthMethod = 0;eachAuthMethod<authMethods.size();eachAuthMethod++){
???Method doingMethod = (Method)authMethods.elementAt(eachAuthMethod);

???if(doingMethod.getName().equals("setAuth1Identity")){
????//
????try {
?????doingMethod.invoke(auth, AuthId);
????} catch (Exception e) {
?????e.printStackTrace();
????}
???}else
????if(doingMethod.getName().equals("setAuth2_Password")){
?????try {
??????doingMethod.invoke(auth, AuthPasswd);
?????} catch (Exception e) {
??????e.printStackTrace();
?????}
????}
??}
?}

3、确认身份,通过认证则加入,不通过则不能加入

?//加入“群”
?private void joinPeerGroup(PeerGroup spg, String login, String passwd) {
??StructuredDocument creds = null;
??AuthenticationCredential authCred = new AuthenticationCredential(spg,null,creds);
??MembershipService membershipService = spg.getMembershipService();
??Authenticator auth = null;
??try {
???auth = membershipService.apply(authCred);
???completeAuth(auth,login,passwd);
???if(!auth.isReadyForJoin()){
????System.out.println("身份认证失败");
????System.out.println("没有完成认证,不能加入对等组");
???}
???membershipService.join(auth);
??} catch (Exception e) {
???System.out.println("认证失败");
???System.out.println("登录信息不正确,不能加入对等组");
???e.printStackTrace();
???System.exit(-1);
??}

??
?}

?

加上这篇,“JXTA中通过身份认证来加入“群””的全部过程完成。代码也已经全部共享。

读书人网 >软件架构设计

热点推荐