License教程
1、获取目标机器硬件信息
- 将license-device-exec.jar放到目标机器上,执行:
java -jar license-device-exec.jar
,生成device.json
文件
2、根据目标机器硬件信息生成license文件
- 根据
device.json
文件,使用工具com.yubest.gen.MainApp.main
,生成公私钥及license授权文件
public class MainApp {
public static void main(String[] args) {
try {
String subject = "mySubject";
String pubPwd = "a123456";
String priPwd = "a123457";
KeyUtil.createKey(subject, pubPwd, priPwd);
File device = new File("device.json");
String json = Files.readAllLines(device.toPath(), Charset.forName("UTF-8")).get(0);
LicenseCreatorParam param = new LicenseCreatorParam()
.setSubject(subject)
.setPubPass(pubPwd)
.setPriPass(priPwd)
.setDeviceInfo(json);
new MyLicenseCreator()
.setCreatorParam(param)
.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 私钥:
private.key
- 公钥:
public.key
- 授权文件:
license.lic
3、授权使用流程
- 3.1、将公钥:
public.key
和授权文件:license.lic
放到待授权机器上 - 3.2、添加配置
application.properties
license.subject=mySubject
license.storePass=a123456
license.licPath=/xxx/license.lic
license.pubKeyPath=/xxx/public.key
- 3.3、将LicenseManager注入Spring容器中
@Data
@EqualsAndHashCode(callSuper = true)
@Component
@ConfigurationProperties(prefix = "license")
public class LicenseConfig extends LicenseVerifierParam {
@Bean
public LicenseManager licenseManager() {
LicenseParam param = toLicenseParam(LicenseConfig.class);
return new MyLicenseManager(param);
}
}
@Component
@Slf4j
public class LicenseRegister implements ApplicationRunner {
@Autowired
private LicenseConfig config;
@Autowired
private LicenseManager manager;
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("------开始注册证书------");
new MyLicenseVerifier().install(config.getLicPath(), manager);
log.info("------结束注册证书------");
}
}
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Autowired
private LicenseInterceptor licenseInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(licenseInterceptor)
.addPathPatterns("/**");
}
}
@Component
@Slf4j
public class LicenseInterceptor implements HandlerInterceptor {
@Autowired
private LicenseManager manager;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
log.info("------开始校验证书------");
try {
new MyLicenseVerifier().verify(manager);
return true;
} catch (Exception e) {
log.error("证书校验失败", e);
}
response.setCharacterEncoding("UTF-8");
Map<String, Object> data = new HashMap<>();
data.put("code", 1000);
data.put("msg", "当前服务器不在授权范围内");
response.getWriter().write(new Gson().toJson(data));
return false;
}
}
码云:gitee.com/hweiyu/spring-boot-license