我们身处于拿来即用的时代,我们身处于物联网高爆发的时代,我们身处在知识产权弱于保护的时代,我们身处在幸幸苦苦码的代码被人轻而易取获取的时代,我们身处狼狈且不甘于平凡的时代。所以保护自己的成果显得尤为重要,建立自己核心代码license和使用期限的重要性甚嚣尘上。
第一步:
将自己的核心代码使用一个Java工程编写。此时应该牢记写测试用例
第二步:
在sc目录下添加license.xml。这里面放自己的密钥和使用时间,一级其他信息。使用DOM4J.jar在日期过期后,修改xml的内容。
package com.huixin.util;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Dom4jXMLHelper {
public static void handlerXML(){
Document document = getDocument();
readAllElementsFromXMLDocument(document);
}
/**
* 该方法实现了对xml文档的读取功能
*
* @param document
* @return
*/
public static void readAllElementsFromXMLDocument(Document document) {
Element root = document.getRootElement();
List list = root.elements();
for (int i = 0; i < list.size(); i++) {
Element firstNodes = (Element) list.get(i);
String firstNode = firstNodes.getName();
if(firstNode.equalsIgnoreCase("Signature")){
firstNodes.setText("license已经过期了,请联系管理员...");
}else if(firstNode.equalsIgnoreCase("Data")){
List ll = firstNodes.elements();
for (int j = 0; j < ll.size(); j++) {
Element element = (Element) ll.get(j);
if(element.getName().equalsIgnoreCase("SubscriptionExpiry")){
element.setText("20180831");
}
if(element.getName().equalsIgnoreCase("LicenseExpiry")){
element.setText("20180831");
}
if(element.getName().equalsIgnoreCase("SerialNumber")){
element.setText(UUID.randomUUID().toString());
}
}
}
/*List ll = book.elements();
System.out.println(id);
for (int j = 0; j < ll.size(); j++) {
Element element = (Element) ll.get(j);
if ("title".equals(element.getName())) {
String title = element.getText();
System.out.println(title);
}
if ("price".equals(element.getName())) {
String price = element.getText();
double p = Double.parseDouble(price);
System.out.println(price);
}
}*/
}
writeToNewXMLDocument(document);
}
/**
* 通过document对象将内存中的dom树保存到新的xml文档。
*
* @param document
* @throws Exception
*/
public static void writeToNewXMLDocument(Document document) {
try{
XMLWriter writer = new XMLWriter(new FileWriter("src/license.xml"));
writer.write(document);
writer.close();
}catch(IOException ex){
ex.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 该方法用于得到document对象。
*
* @return
* @throws Exception
*/
public static Document getDocument() {
SAXReader sr = new SAXReader();
Document document = null;
try {
document = sr.read("src\\license.xml");
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception ex){
ex.printStackTrace();
}
return document;
}
}
第三步:
在核心代码关键位置,每次读取xml进行判断,是否过期,并加以提示
/**
* 获取项目license
* @return
*/
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
License aposeLic = new License();
aposeLic.setLicense(is);
Calendar cale=Calendar.getInstance();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String time="2019-01-01";
Date date2019=sdf.parse(time);
if(cale.getTime().before(date2019)){
System.out.println("License将于2019年1月1日过期,请联系管理员付费...");
result = true;
}else{
Dom4jXMLHelper.handlerXML();
}
}catch(NullPointerException ne){
System.out.println("License出现问题了,请联系管理员破解...");
ne.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
在核心位置进行getLicense()调用
第四步:
使用jvmti 防止反编译,参考文档: https://blog.youkuaiyun.com/u011311291/article/details/78131243/
第五步:
使用java ide进行打包成jar