SpringBoot实现License认证(只校验有效期)
import de.schlichtherle.license.LicenseContent;
import de.schlichtherle.license.LicenseParam;
import de.schlichtherle.license.LicenseManager;
import de.schlichtherle.license.DefaultCipher;
import java.io.*;
import java.security.PrivateKey;
import java.text.SimpleDateFormat;
import java.util.Properties;
public class LicenseVerifier {
private static final String SERVER_PUBLIC_KEY = "your_public_key.txt";
private static final String LICENSE_FILE = "your_license.lic";
public static void main(String[] args) throws Exception {
// 初始化 LicenseManager
LicenseManager licenseManager = new LicenseManager(initLicenseParam());
// 验证许可证
licenseManager.verify();
System.out.println("License验证通过。");
}
private static LicenseParam initLicenseParam() throws Exception {
// 读取服务端公钥
InputStream in = new BufferedInputStream(new FileInputStream(SERVER_PUBLIC_KEY));
Properties props = new Properties();
props.load(in);
in.close();
// 加载服务端公钥
PublicKey publicKey = SecurityUtils.getPublicKey(props.getProperty("public.key"));
// 构建LicenseParam
LicenseParam licenseParam = new LicenseParam();
licenseParam.setPublicAlias("public");
licenseParam.setPublicKey(publicKey);
licenseParam.setLicensePath(LICENSE_FILE);
licenseParam.setLicenseCallback(new DefaultLicenseCallback());
return licenseParam;
}
private static class DefaultLicenseCallback implements LicenseCallback {
@Override
public boolean verify(LicenseContent content) throws Exception {
// 只验证有效期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date start = sdf.parse(content.getNotBefore());
Date end = sdf.parse(content.getNotAfter());
Date now = new Date();
return now.after(start) && now.before(end);
}
@Override
public void onValid(LicenseContent content, LicenseStore store) throws Exception {
// 当许可证有效时的回调
}
@Override
public void onExpired(LicenseContent content, LicenseStore store) throws Exception {
// 当许可证过期时的回调
throw new Exception("License已过期。");
}
}
}
这个代码示例展示了如何使用自定义的DefaultLicenseCallback
来实现只验证许可证的有效期的方法。在verify
方法中,我们只比较当前日
评论已关闭