做了个ClassLoader类加载器,在main方法里可以运行,但配置在listener里运行却报错?什么原因

baisou 发布于 2013/10/31 17:44
阅读 556
收藏 0

做了个ClassLoader类加载器,在main方法里可以运行,但配置在listener里运行却报错?

listener中调用了此方法

@Override
public void contextInitialized(ServletContextEvent event) {
//加载系统自定义缓存
this.initApplicationParam(event);
try {
Class<?> clazz = new Decrypt().findClass("cn.com.erp.common.login.LoginAction") ; 
       System.out.println( clazz.newInstance()+"=======>>"+clazz.getSimpleName()) ; 
} catch (Exception e) {
e.printStackTrace();
}  
}

什么原因:错误如下:

严重: Exception sending context initialized event to listener instance of class cn.com.erp.common.listener.CustomInitListener
java.lang.NoClassDefFoundError: org/apache/struts2/interceptor/SessionAware (wrong name: cn/com/erp/common/login/LoginAction)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.lang.ClassLoader.defineClass(ClassLoader.java:466)
at cn.com.erp.common.classLoader.test.Decrypt.findClass(Decrypt.java:40)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.ClassLoader.defineClass1(Native Method)
下面帖下代码:

//www.baisoujs.com

Key.java

package cn.com.erp.common.classLoader.test;


import java.io.File;
import java.io.FileOutputStream;
import java.security.SecureRandom;


import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
 * 生成加密钥匙
 * @author liyong
 *
 */
public class Key {
private String keyName;  
    public Key() {  
  
    }  
    public Key(String keyName) {  
        this.keyName = keyName;  
    }  
  
    public void createKey(String keyName) throws Exception {  
        SecureRandom sr = new SecureRandom();  
        KeyGenerator kg = KeyGenerator.getInstance("DES");  
        kg.init(sr);  
        SecretKey key = kg.generateKey();  
        System.out.println(key.toString());  
        byte rawKeyData[] = key.getEncoded();  
        FileOutputStream fo = new FileOutputStream(new File(keyName));  
        fo.write(rawKeyData);  
        fo.close();  
    }  
  
    public static void main(String args[]) {  
        try {  
            new Key("").createKey("C:\\Users\\liyong\\Desktop\\JCE\\key.txt");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
  
    }  
}


Crypt.java

package cn.com.erp.common.classLoader.test;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class Crypt {
public static void main(String[] args) throws Exception { 
//生成加密class文件
        SecureRandom sr = new SecureRandom();
        //读取DES钥匙key
        FileInputStream fi = new FileInputStream(new File("C:\\Users\\liyong\\Desktop\\JCE\\key.txt"));  
        byte rawKeyData[] = new byte[fi.available()];  
        fi.read(rawKeyData);  
        fi.close();  
        DESKeySpec dks = new DESKeySpec(rawKeyData);  
        SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(dks);  
        Cipher cipher = Cipher.getInstance("DES");  
        cipher.init(Cipher.ENCRYPT_MODE, key,sr);  
        //读取class文件
        FileInputStream fi2 = new FileInputStream(new File("C:\\Users\\liyong\\Desktop\\JCE\\LoginAction.class"));  
        byte data[] = new byte[fi2.available()];  
        fi2.read(data);  
        fi2.close();  
        byte encryptedData[] = cipher.doFinal(data);  
        //输出加密后的class文件
        FileOutputStream fo = new FileOutputStream(new File("D:\\projects\\TestKeyProject\\WebContent\\WEB-INF\\classes\\cn\\com\\erp\\common\\login\\LoginAction.class"));  
        fo.write(encryptedData);  
        fo.close();  
    }  
}



Decrypt.java

package cn.com.erp.common.classLoader.test;


import java.io.File;
import java.io.FileInputStream;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
 * 解密
 * @author liyong
 *
 */
public class Decrypt extends ClassLoader{
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {  
byte decryptedData[] =null;
try{
SecureRandom sr = new SecureRandom();  
       FileInputStream fi = new FileInputStream(new File("C:\\Users\\liyong\\Desktop\\JCE\\key.txt"));  
       byte rawKeyData[] = new byte[fi.available()];  
       fi.read(rawKeyData);  
       fi.close();  
       
       DESKeySpec dks = new DESKeySpec(rawKeyData);  
       SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(dks);  
       Cipher cipher = Cipher.getInstance("DES");  
       cipher.init(Cipher.DECRYPT_MODE, key, sr);  
       
       FileInputStream fi2 = new FileInputStream(new File("D:\\projects\\TestKeyProject\\WebContent\\WEB-INF\\classes\\cn\\com\\erp\\common\\login\\LoginAction.class"));  
       byte encryptedData[] = new byte[fi2.available()];  
       fi2.read(encryptedData);  
       fi2.close();  
       
       decryptedData = cipher.doFinal(encryptedData);  
}catch(Exception e){
e.printStackTrace();
}
return super.defineClass(name, decryptedData, 0, decryptedData.length);
    }  
public static void main(String[] args) throws Exception {  
       Class<?> clazz = new Decrypt().findClass("cn.com.erp.common.login.LoginAction") ; 
       System.out.println( clazz.newInstance()+"=======>>"+clazz.getSimpleName()) ; 
}  
}


在main方法里运行是正常的,我这几个程序主要用DES生成一个钥匙,然后拿到钥匙加密CLASS 文件,再拿key去解密,解密时通过类加载器加载,在java web中应该如何配置呢?求大神们指点一下
加载中
返回顶部
顶部