JAVA获取类的泛型信息

Java 使用 license

使用 truelicense

  1. 应用程序可以创建以及验证绑定给用户、系统等实体的 license。
  2. 防止用户随意拷贝软件和 license。
  3. licenses 可以是永久性的或者临时性的(在某个特定时期内有效)
  4. licenses 的验证由 JAVA Security API 提供的数字签名机制来实现。
  5. license 安装模块需要用特殊机制对其进行保护,以防被反编译轻易破解。

管理端生成密钥

1
2
3
4
5
6
7
# 1、生成密钥对
keytool -genkey -alias dsa_private_key -keyalg DSA -keysize 1024 -dname "CN=maxzhao, OU=maxzhao, O=maxzhao_org, L=nanjing, ST=jiangsu, C=China" \
-validity 36500 -keystore dsa_private_key_store.jks -storepass maxzhao_pwd -keypass maxzhao_pwd
# 2、导出公钥
keytool -export -alias dsa_private_key -file dsa_certfile.cer -keystore dsa_private_key_store.jks -storepass maxzhao_pwd
# 3、证书文件导入到公钥库
keytool -import -alias dsa_public_cert -file dsa_certfile.cer -keystore dsa_public_certs_store.jks -storepass maxzhao_pwd

服务段生成证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.license;


import de.schlichtherle.license.*;

import javax.security.auth.x500.X500Principal;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Properties;
import java.util.prefs.Preferences;

/**
* 生成证书
*/
public class CreateLicense {
public static void main(String[] args) {
CreateLicense cLicense = new CreateLicense();
cLicense.setParam("/param.properties");
//生成证书
boolean b = cLicense.create();
}

private static String PRIVATE_ALIAS = "";
private static String KEY_PWD = "";
private static String STORE_PWD = "";
private static String SUB_JECT = "";
private static String LIC_PATH = "";
private static String PRI_PATH = "";
private static String ISSUED_TIME = "";
private static String NOT_BEFORE = "";
private static String NOT_AFTER = "";
private static String CONSUMER_TYPE = "";
private static int CONSUMER_AMOUNT = 0;
private static String INFO = "";
// X500 Principal
private final static X500Principal DEFAULT_HOLDER_AND_ISSUER = new X500Principal("CN=maxzhao, OU=maxzhao, O=maxzhao_org, L=nanjing, ST=jiangsu, C=China");

public void setParam(String propertiesPath) { // 获取参数
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream(propertiesPath);
try {
prop.load(in);
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
PRIVATE_ALIAS = prop.getProperty("privateAlias");
KEY_PWD = prop.getProperty("keyPwd");
STORE_PWD = prop.getProperty("storePwd");
SUB_JECT = prop.getProperty("subJect");
KEY_PWD = prop.getProperty("keyPwd");
LIC_PATH = prop.getProperty("licPath");
PRI_PATH = prop.getProperty("priPath"); //license content
ISSUED_TIME = prop.getProperty("issuedTime");
NOT_BEFORE = prop.getProperty("notBefore");
NOT_AFTER = prop.getProperty("notAfter");
CONSUMER_TYPE = prop.getProperty("consumerType");
CONSUMER_AMOUNT = Integer.valueOf(prop.getProperty("consumerAmount"));
INFO = prop.getProperty("info");
}

/**
* 证书发布者端执行
*/
public boolean create() {
try {
LicenseManager licenseManager = new LicenseManager(initLicenseParams());
licenseManager.store((createLicenseContent()), new File(LIC_PATH));
} catch (Exception e) {
e.printStackTrace();
System.out.println("客户端证书生成失败!");
return false;
}
System.out.println("服务器端生成证书成功!");
return true;
}

/**
* 返回生成证书时需要的参数
*/
private static LicenseParam initLicenseParams() {
Preferences preference = Preferences.userNodeForPackage(LicenseCreator.class);
// 设置对证书内容加密的对称密码
CipherParam cipherParam = new DefaultCipherParam(STORE_PWD);
KeyStoreParam privateStoreParam = new DefaultKeyStoreParam(LicenseCreator.class, PRI_PATH, PRIVATE_ALIAS, STORE_PWD, KEY_PWD);
return new DefaultLicenseParam(SUB_JECT, preference, privateStoreParam, cipherParam);
}

/**
* 从外部表单拿到证书的内容
*/
public final static LicenseContent createLicenseContent() {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
LicenseContent content = null;
content = new LicenseContent();
content.setSubject(SUB_JECT);
content.setHolder(DEFAULT_HOLDER_AND_ISSUER);
content.setIssuer(DEFAULT_HOLDER_AND_ISSUER);
try {
content.setIssued(format.parse(ISSUED_TIME));
content.setNotBefore(format.parse(NOT_BEFORE));
content.setNotAfter(format.parse(NOT_AFTER));
} catch (ParseException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
content.setConsumerType(CONSUMER_TYPE);
content.setConsumerAmount(CONSUMER_AMOUNT);
content.setInfo(INFO); // 扩展
content.setExtra(new Object());
return content;
}
}

客户端使用证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.license;

import de.schlichtherle.license.*;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.prefs.Preferences;

/**
* 校验证书
*/
public class VerifyLicense {

public static void main(String[] args) {
VerifyLicense license = new VerifyLicense();
license.setParam("/public-param.properties");
license.verify();
}

private static String PUBLIC_ALIAS = "";
private static String STORE_PWD = "";
private static String SUB_JECT = "";
private static String LIC_PATH = "";
private static String PUB_PATH = "";

public void setParam(String propertiesPath) {
// 获取参数
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream(propertiesPath);
try {
prop.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PUBLIC_ALIAS = prop.getProperty("publicAlias");
STORE_PWD = prop.getProperty("storePwd");
SUB_JECT = prop.getProperty("subJect");
LIC_PATH = prop.getProperty("licPath");
PUB_PATH = prop.getProperty("pubPath");
}

/**
* 证书使用者端执行
*/
public boolean verify() {
LicenseManager licenseManager = new LicenseManager(initLicenseParams());
// 安装证书
try {
licenseManager.install(new File(LIC_PATH));
System.out.println("客户端安装证书成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("客户端证书安装失败!");
return false;
}
// 验证证书
try {
licenseManager.verify();
System.out.println("客户端验证证书成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("客户端证书验证失效!");
return false;
}
return true;
}

/**
* 返回验证证书需要的参数
*/
private static LicenseParam initLicenseParams() {
Preferences preference = Preferences.userNodeForPackage(VerifyLicense.class);
CipherParam cipherParam = new DefaultCipherParam(STORE_PWD);
KeyStoreParam privateStoreParam = new DefaultKeyStoreParam(VerifyLicense.class, PUB_PATH, PUBLIC_ALIAS, STORE_PWD, null);
return new DefaultLicenseParam(SUB_JECT, preference, privateStoreParam, cipherParam);
}
}

maven 依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

<dependencies>
<dependency>
<groupId>de.schlichtherle.truelicense</groupId>
<artifactId>truelicense-core</artifactId>
<version>1.33</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15to18</artifactId>
<version>1.77</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.68</version>
</dependency>
</dependencies>

param.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# alias
privateAlias=dsa_private_key
keyPwd=maxzhao_pwd
storePwd=maxzhao_pwd
subJect=bigdata
# 绝对路径
licPath=/temp/bigdata-out-18.lic
priPath=/dsa_private_key_store.jks
##########license content###########
issuedTime=2014-04-01
notBefore=2014-04-01
notAfter=2024-07-18
consumerType=user
consumerAmount=1
# other info
info=this is a license

public-param.properties

1
2
3
4
5
6
7
8
9
# alias
publicAlias=dsa_public_cert
storePwd=maxzhao_pwd
# 主题
subJect=bigdata
# 绝对路径
licPath=/temp/bigdata-out-18.lic
pubPath=/dsa_public_certs_store.jks

本文地址: https://github.com/maxzhao-it/blog/post/56235478467467/