?? securitymgr.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 + -