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

📄 securitymgr.java

📁 陕西电信sp客户端
💻 JAVA
字号:
// ----------------------------------------------------------------------------
// $Source: /cvs/vas2006/webpro2/webpro_java/src/com/onewaveinc/portalman/webpro/security/SecurityMgr.java,v $
// ----------------------------------------------------------------------------
// Copyright (c) 2002 by Onewave Inc.
// ----------------------------------------------------------------------------
// $Id: SecurityMgr.java,v 1.1.1.1 2006/08/01 05:49:34 zhengx Exp $
// ----------------------------------------------------------------------------
// $Log: SecurityMgr.java,v $
// Revision 1.1.1.1  2006/08/01 05:49:34  zhengx
// no message
//
// Revision 1.1  2006/06/02 03:33:17  wuyan
// *** empty log message ***
//
// Revision 1.2  2005/12/09 08:37:51  lufang
// no message
//
// Revision 1.1  2005/12/08 10:37:42  like
// no message
//
// Revision 1.1  2003/07/28 06:30:32  zengc
// no message
//
// ----------------------------------------------------------------------------

package com.onewaveinc.portalman.webpro.security;

/**
 * <p>Title: PortalMAN SDK API Documentation</p>
 * <p>Description: OneWave Technologies., Inc. PortalMAN Value-add Management Platform 3rd Software Development Kit</p>
 * <p>Copyright: Copyright (c) 2002 </p>
 * <p>Company: OneWave Technologies., Inc.</p>
 * @author 3rd AAA & ICP Integration Developement Team
 * @version 1.5
 */

import java.util.Properties;
import java.lang.reflect.*;
import java.security.*;

import cryptix.provider.Cryptix;

import com.onewaveinc.portalman.aaa.v20.soapentity.*;
//import com.onewaveinc.portalman.aaa.*;
//import com.onewaveinc.portalman.init.ServiceIF;

public class SecurityMgr {


    public SecurityMgr() {
    }

    /**
     * description            动态加载security provider
     */
    static {
        Security.addProvider(new cryptix.provider.Cryptix());
    }

    public void init(Properties properties) {
        //init();
    }

    public void start() {
    }

    public void stop() {
    }

    /**
     * description            解析soap传过来的经过加密的对象
     * @param obj             对象的参数被加密
     * @param icpode          用于从mapping关系中找到icpkey的
     * @return
     */
    //public static Object getDecryptedObject(Object obj,String icpCode)throws Exception{

    //String icpKey = ICPCodeMgr.getICPKeyfromEncrypt(icpCode);
//	return getDecyptedObjUseICPKey(obj,icpKey,null);
    // }

    /**
     * description            解析soap传过来的经过加密的对象,使用icpkey作为密钥
     * 如果oldEncryptICPCode为null,是AAA端的解密需求,
     * 调用aaa端的判断ICPCode逻辑
     * 如果不为空,是webPro的请求,调用webPro判断ICPCode的逻辑
     *
     * @param obj               对象的参数被加密
     * @param icpKey            用于解密的webPro方保存的密钥
     * @param oldEncryptICPCode
     * @return
     * @throws Exception
     */

    public static Object getDecyptedObjUseICPKey(Object obj, String icpKey, String oldEncryptICPCode) throws Exception {

        Method[] methods;
        Field[] fields;
        methods = getMethods(obj.getClass());
        fields = getFields(obj.getClass());

        for (int i = 0; i < fields.length; i++) {
            Method getMethod = getMethod("get", fields[i], methods);
            Method setMethod = getMethod("set", fields[i], methods);
            String getResult = null;
            if (getMethod != null) {//防止有参数没有get 或者 set方法引起出错
                getResult = (String) getMethod.invoke(obj, null);
            }
            if (getResult != null) {
                String setResult = null;

                if ("versionNO".equalsIgnoreCase(fields[i].getName())) {
                    setResult = getResult;

                } else if ("icpCode".equalsIgnoreCase(fields[i].getName())) {

                    //AAA端验证ICPCode正确性的逻辑
                    ICPCodeMgr.validateICPCode(getResult, icpKey);
                    setResult = ICPCodeMgr.getOriginIcpCode(getResult);

                } else {

                    //需要解密的普通字段
                    try {
                        setResult = DesMgr.decrypt(getResult, icpKey);
                    }
                    catch (Exception ex) {
                        throw new Exception("Decrypt Error ! Decrypt Value = " + getResult);
                    }
                }


                Object[] args = new Object[]{setResult};
                if (setMethod != null) {
                    setMethod.invoke(obj, args);
                }
            }

        }


        return obj;
    }

    /**
     * description            加密给soap传输使用的对象,加密过去的ICPCode用webPro;主要由AAA验证逻辑调用
     *                        传过来的icpCode的前32位作为加密的随机数
     * @param obj             对象的icpcode不加密,通过icpcode
     * @param icpCode         用于从mapping关系中找到icpkey
     * @return
     */
    //public static Object getEncyptedObject(Object obj,String icpCode) {

//	try {
    //System.out.println("*****  debug  getEncyptedObject(Object obj,String icpCode)");
//	  String icpKey = ICPCodeMgr.getICPKeyfromEncrypt(icpCode);
    //System.out.println("*****  debug ****   icpCode is "+icpCode);
    //System.out.println("*****  debug ****   icpKey is "+icpKey);
//	  return getEncyptedObjUseICPKey(obj,ICPCodeMgr.getEncryptedICPCode(icpCode),icpKey);
//	}
//	catch (Exception ex) {
//	  ex.printStackTrace();
//	  return null;
//	}
//  }

    /**
     * description            加密给soap传输使用的对象,使用webPro保存的密钥;主要由webPro端调用
     *
     * @param obj    对象的icpcode不加密,通过icpcode
     * @param icpKey 用于解密的webPro方保存的密钥
     * @return Object
     */
    public static Object getEncyptedObjUseICPKey(Object obj, String randomNo, String icpKey) throws Exception {
        Method[] methods;
        Field[] fields;
        //get All methods and field from object
        methods = getMethods(obj.getClass());
        fields = getFields(obj.getClass());

        for (int i = 0; i < fields.length; i++) {
            Method getMethod = getMethod("get", fields[i], methods);
            Method setMethod = getMethod("set", fields[i], methods);
            String getResult = null;
            if (getMethod != null) {//防止有参数没有get 或者 set方法引起出错
                getResult = (String) getMethod.invoke(obj, null);
            }
            if (getResult != null) {//非空字段
                String setResult = null;
                if ("versionNO".equalsIgnoreCase(fields[i].getName())) {
                    setResult = getResult;
                } else if ("icpCode".equalsIgnoreCase(fields[i].getName())) {
                    //icpCode字段,单向加密
                    setResult = ICPCodeMgr.generateEncryptedICPCode(getResult, randomNo, icpKey);

                } else {
                    //需要加密的普通字段
                    try {
                        setResult = DesMgr.encrypt(getResult, icpKey);
                    } catch (Exception ex) {
                        throw new Exception("Encrypt Error ! Encrypt Value = " + getResult);
                    }

                }


                Object[] args = new String[]{setResult};
                if (setMethod != null) {
                    setMethod.invoke(obj, args);
                }
            }
        }
        return obj;
    }

    /**
     * 根据送过来的string加密生成一个token
     * @param str
     * @return
     * @throws Exception
     */
//  public static String getEncyptedToken(String str)throws Exception{

//	return TokenMgr.getEncryptedToken(str);
//  }

    /**
     * description           根据field选出合适的set get Method
     *
     * @param methodPerfix "get" or "set"
     * @param aField
     * @param methods
     * @return Method
     */
    private static Method getMethod(String methodPerfix, Field aField, Method[] methods) {

        Method objGetMethod = null;
        String fieldName = Character.toUpperCase(aField.getName().charAt(0))
                + aField.getName().substring(1);
        String getMethodName = methodPerfix + fieldName;
        for (int j = 0; j < methods.length; j++) {
            if (methods[j].getName().equals(getMethodName)) return methods[j];
        }
        return objGetMethod;
    }

    /**
     * 第归取得有继承关系的class的所有method
     *
     * @param myClass 有可能有父类的class
     * @return 所有可用的method
     */
    public static Method[] getMethods(Class myClass) {

        Method[] methods = myClass.getDeclaredMethods();
        if (!myClass.getSuperclass().equals(Object.class)) {
            return addMethod(methods, getMethods(myClass.getSuperclass()));
        } else {
            return methods;
        }
    }

    /**
     * 第归取得有继承关系的class的所有field
     *
     * @param myClass 有可能有父类的class
     * @return 所有可用的field
     */
    public static Field[] getFields(Class myClass) {

        Field[] fields = myClass.getDeclaredFields();
        if (!myClass.getSuperclass().equals(Object.class)) {
            return addField(fields, getFields(myClass.getSuperclass()));
        } else {
            return fields;
        }
    }

    /**
     * 数组合并操作,返回合并后的新数组
     *
     * @param b1
     * @param b2
     * @return Method
     */
    private static Method[] addMethod(Method[] b1, Method[] b2) {

        Method[] b = new Method[b1.length + b2.length];
        System.arraycopy(b1, 0, b, 0, b1.length);
        System.arraycopy(b2, 0, b, b1.length, b2.length);
        return b;
    }

    private static Field[] addField(Field[] b1, Field[] b2) {

        Field[] b = new Field[b1.length + b2.length];
        System.arraycopy(b1, 0, b, 0, b1.length);
        System.arraycopy(b2, 0, b, b1.length, b2.length);
        return b;
    }
}

⌨️ 快捷键说明

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