今日分享
每天分享技术实战干货,技术在于积累和收藏,点赞关注小锋,每天实战技术分享。
关注小锋,每天分享实战技术。
springboot整合TrueLicense-验证License证书
1、新增License校验类参数
/**
* @ProjectName LicenseVerifyParam
* @author Administrator
* @version 1.0.0
* @Description License校验类需要的参数
* @createTime 2022/4/30 0030 18:29
*/
@Data
public class LicenseVerifyParam {
/**
* 证书subject
*/
private String subject;
/**
* 公钥别称
*/
private String publicAlias;
/**
* 访问公钥库的密码
*/
private String storePass;
/**
* 证书生成路径
*/
private String licensePath;
/**
* 密钥库存储路径
*/
private String publicKeysStorePath;
public LicenseVerifyParam() {
}
public LicenseVerifyParam(String subject, String publicAlias, String storePass, String licensePath, String publicKeysStorePath) {
this.subject = subject;
this.publicAlias = publicAlias;
this.storePass = storePass;
this.licensePath = licensePath;
this.publicKeysStorePath = publicKeysStorePath;
}
}
2、新增LicenseVerify校验类
/**
* @ProjectName LicenseVerify
* @author Administrator
* @version 1.0.0
* @Description License校验类
* @createTime 2022/4/30 0030 18:28
*/
public class LicenseVerify {
private static Logger logger = LogManager.getLogger(LicenseVerify.class);
/**
* @title install
* @description 安装License证书
* @author Administrator
* @updateTime 2022/4/30 0030 18:29
*/
public synchronized LicenseContent install(LicenseVerifyParam param){
LicenseContent result = null;
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//1. 安装证书
try{
LicenseManager licenseManager = LicenseManagerHolder.getInstance(initLicenseParam(param));
licenseManager.uninstall();
result = licenseManager.install(new File(param.getLicensePath()));
logger.info(MessageFormat.format("证书安装成功,证书有效期:{0} - {1}",format.format(result.getNotBefore()),format.format(result.getNotAfter())));
}catch (Exception e){
logger.error("证书安装失败!",e);
}
return result;
}
/**
* @title verify
* @description 校验License证书
* @author Administrator
* @updateTime 2022/4/30 0030 18:29
*/
public boolean verify(){
LicenseManager licenseManager = LicenseManagerHolder.getInstance(null);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//2. 校验证书
try {
LicenseContent licenseContent = licenseManager.verify();
// System.out.println(licenseContent.getSubject());
logger.info(MessageFormat.format("证书校验通过,证书有效期:{0} - {1}",format.format(licenseContent.getNotBefore()),format.format(licenseContent.getNotAfter())));
return true;
}catch (Exception e){
logger.error("证书校验失败!",e);
return false;
}
}
/**
* @title initLicenseParam
* @description 初始化证书生成参数
* @author Administrator
* @updateTime 2022/4/30 0030 18:29
*/
private LicenseParam initLicenseParam(LicenseVerifyParam param){
Preferences preferences = Preferences.userNodeForPackage(LicenseVerify.class);
CipherParam cipherParam = new DefaultCipherParam(param.getStorePass());
KeyStoreParam publicStoreParam = new CustomKeyStoreParam(LicenseVerify.class
,param.getPublicKeysStorePath()
,param.getPublicAlias()
,param.getStorePass()
,null);
return new DefaultLicenseParam(param.getSubject()
,preferences
,publicStoreParam
,cipherParam);
}
}
3、新增LicenseCheckListener监听器
用于在项目启动的时候安装License证书
/**
* @ProjectName LicenseCheckListener
* @author Administrator
* @version 1.0.0
* @Description 在项目启动时安装证书
* @createTime 2022/4/30 0030 18:28
*/
@Component
public class LicenseCheckListener implements ApplicationListener<ContextRefreshedEvent> {
private static Logger logger = LogManager.getLogger(LicenseCheckListener.class);
/**
* 证书subject
*/
@Value("${license.subject}")
private String subject;
/**
* 公钥别称
*/
@Value("${license.publicAlias}")
private String publicAlias;
/**
* 访问公钥库的密码
*/
@Value("${license.storePass}")
private String storePass;
/**
* 证书生成路径
*/
@Value("${license.licensePath}")
private String licensePath;
/**
* 密钥库存储路径
*/
@Value("${license.publicKeysStorePath}")
private String publicKeysStorePath;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
//root application context 没有parent
ApplicationContext context = event.getApplicationContext().getParent();
if(context == null){
if(StringUtils.isNotBlank(licensePath)){
logger.info("++++++++ 开始安装证书 ++++++++");
LicenseVerifyParam param = new LicenseVerifyParam();
param.setSubject(subject);
param.setPublicAlias(publicAlias);
param.setStorePass(storePass);
param.setLicensePath(licensePath);
param.setPublicKeysStorePath(publicKeysStorePath);
LicenseVerify licenseVerify = new LicenseVerify();
//安装证书
licenseVerify.install(param);
logger.info("++++++++ 证书安装结束 ++++++++");
}
}
}
}
上面代码使用参数信息如下所示:
#License相关配置
license.subject=license_demo
license.publicAlias=publicCert
license.storePass=public_password1234
license.licensePath=D:/license/license.lic
license.publicKeysStorePath=D:/license/publicCerts.keystore
4、新增拦截器-请求验证证书
/** * @ProjectName LicenseCheckInterceptor * @author Administrator * @version 1.0.0 * @Description LicenseCheckInterceptor * @createTime 2022/4/30 0030 18:27 */public class LicenseCheckInterceptor implements HandlerInterceptor { private static Logger logger = LogManager.getLogger(LicenseCheckInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { LicenseVerify licenseVerify = new LicenseVerify(); //校验证书是否有效 boolean verifyResult = licenseVerify.verify(); if(verifyResult){ return true; }else{ response.setCharacterEncoding("utf-8"); Map<String,String> result = new HashMap<>(1); result.put("result","您的证书无效,请核查服务器是否取得授权或重新申请证书!"); response.getWriter().write(JSON.toJSONString(result)); return false; } }}
5、新建WebMvcConfig注册拦截器
/** * @ProjectName WebMvcConfig * @author Administrator * @version 1.0.0 * @Description 注册拦截器 * @createTime 2022/4/30 0030 21:11 */@Configurationpublic class WebMvcConfig implements WebMvcConfigurer { /** * 添加拦截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LicenseCheckInterceptor()).addPathPatterns("/check"); }}
6、启动项目测试
本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 sumchina520@foxmail.com 举报,一经查实,本站将立刻删除。
如若转载,请注明出处:https://www.sumedu.com/faq/130975.html
如若转载,请注明出处:https://www.sumedu.com/faq/130975.html