⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 securitydemo.java

📁 Java p2p程序设计2002年版
💻 JAVA
字号:
package com.sams.jxta.security;

import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.StructuredDocument;
import net.jxta.exception.PeerGroupException;
import net.jxta.exception.ProtocolNotSupportedException;
import net.jxta.id.ID;
import net.jxta.id.IDFactory;
import net.jxta.impl.peergroup.StdPeerGroup;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.platform.Module;
import net.jxta.platform.ModuleClassID;
import net.jxta.platform.ModuleSpecID;
import net.jxta.protocol.ModuleImplAdvertisement;

import jxta.security.signature.Signature;
import jxta.security.util.URLBase64;

import java.net.*;
import java.io.IOException;
import com.sams.jxta.groups.*;

/**
 * This class runs a JXTA Group which uses the SecureMembershipService
 * The SecureMembershipService demonstrates the use of the JXTA Crypto APIs
 */

public class SecurityDemo implements Module{
    
    private static final org.apache.log4j.Category LOG =   org.apache.log4j.Category.getInstance(SecurityDemo.class.getName());
    protected static PeerGroup netPeerGroup = null;
    /**
     * we need to understand jxta protocols. This is used if you use "Java Web Start"
     * or if you launch via a non-forked JVM in NetBeans or other IDE.
     */
    static {
        URL.setURLStreamHandlerFactory( new URLStreamHandlerFactory() {
            public URLStreamHandler createURLStreamHandler(
            String protocol ) {
                if( "urn".equals( protocol ) ||
                "jxta".equals( protocol )) {
                    return new URLStreamHandler() {
                        protected URLConnection openConnection(
                        URL u )
                        throws IOException {
                            return null;
                        }
                    };
                }
                else
                    return null;
            }
        } );
    }
    
    
    public SecurityDemo(){
    }
    /*
     * This method is called when the module is initialized.
     * This is when we use the Group Manager.
     */
    public void init(PeerGroup parentGroup,ID assignedID,Advertisement implAdv)
    throws PeerGroupException{
        
        GroupManager manager = new GroupManager(netPeerGroup, parentGroup);
        // This is to create a new SecurityDemo group
        PeerGroup p=null;
        try{
            p =manager.addGroup("SecurityDemoGroup",
            "com.sams.jxta.security.SecureMembershipService",
            "To illustrate JXTA Crypto APIs",
            null,
            false);
            
        }catch(Exception e){
            e.printStackTrace();
            System.exit(-1);
        }
        StructuredDocument document = null;
        try{
            
            // The EncryptedDocumentsFactory is used to create an Encrypted Document.
            // RC4 symmetric encryption is used by the factory.
            // The document is supplied as credential to the group manager.The Group Manager
            // inturn uses this credentials document in the join process
            document = EncryptedDocumentsFactory.getEncryptedDocument();
            // A successful membership to the group returns a signed SecureCredential
            SecureCredential credential =(SecureCredential)manager.joinGroup(p,document,"Apply");
            // The SecureCredential is signed using RSA algorithm.
            // Now we will verify that the credential is indeed signed by the Membership Service
            byte[] messageBytes = credential.getMessageBytes();
            byte[] signatureArray =credential.getMessageSignatureBytes();
            // In order to verify we need a signature object.
            // For our convienence we take the Signature Object available in the
            // credential to verify
            Signature signature = credential.getSignature();
            // Initialize the signature to mode VERIFY.
            signature.init(Signature.MODE_VERIFY);
            // The actual verification
            boolean verified = signature.verify(messageBytes, 0, messageBytes.length,
            signatureArray, 0, signatureArray.length);
            LOG.debug(" SecureCredential Verified !!");
            
            
        } catch (jxta.security.exceptions.CryptoException cryptoException) 	{
            LOG.error("Exception Occured",cryptoException);
            System.exit(1);
        } catch (com.sams.jxta.groups.GroupManagerAuthenticationException managerExcp){
            LOG.error("Exception Occured",managerExcp);
            System.exit(1);
        } catch (net.jxta.exception.ProtocolNotSupportedException managerExcp){
            LOG.error("Exception Occured",managerExcp);
            System.exit(1);
        }catch (Exception exception){
            LOG.error("Exception Occured",exception);
            System.exit(1);
        }
        
    }
    
    /*
     * The main method which serves as the entry point
     */
    
    public static void main(String args[]){
        
        SecurityDemo sample = new SecurityDemo();
        sample.runJXTA();
    }
    
    /*
     * This method starts the JXTA platform.
     *
     */
    
    private void runJXTA(){
        
        
        // Get an instance of the net peer group
        try {
            
            netPeerGroup = PeerGroupFactory.newNetPeerGroup();
        } catch (PeerGroupException peerGroupException) {
            //could not instanciate the group, print the stack and exit
            LOG.error("Group Creation Failure !",peerGroupException);
            System.exit(1);
        }
        // Create a module impl advertisement by using the factory.
        // Set the appropriate fields.
        ModuleClassID mID = IDFactory.newModuleClassID();
        ModuleSpecID moduleSpecID = PeerGroup.refMembershipSpecID;
        ModuleImplAdvertisement adv = (ModuleImplAdvertisement)
        AdvertisementFactory.newAdvertisement(ModuleImplAdvertisement.getAdvertisementType());
        adv.setModuleSpecID(moduleSpecID);
        adv.setCompat(StdPeerGroup.stdCompatStatement);
        adv.setCode("com.sams.jxta.security.SecurityDemo");
        adv.setUri("http://www.jxta.org/download/jxta.jar");
        // This module is loaded by the net peer group.
        try{
            netPeerGroup.loadModule(mID,adv);
        } catch (ProtocolNotSupportedException protocolException) {
            LOG.error("Could not load Module !",protocolException);
            System.exit(1);
        } catch (PeerGroupException peerGroupException) {
            LOG.error("Could not load Module !",peerGroupException);
            System.exit(1);
        }
    }
    
    /*
     * Methods to be implemented by virtue of being a module.
     * Not used in this example
     */
    public int startApp(String[] args){return 0;}
    public void stopApp(){};
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -