# spring-boot-license **Repository Path**: hweiyu/spring-boot-license ## Basic Information - **Project Name**: spring-boot-license - **Description**: SpringBoot集成License - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 18 - **Forks**: 4 - **Created**: 2022-06-20 - **Last Updated**: 2025-02-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # License教程 ## 1、获取目标机器硬件信息 * 将license-device-exec.jar放到目标机器上,执行:`java -jar license-device-exec.jar`,生成`device.json`文件 ## 2、根据目标机器硬件信息生成license文件 * 根据`device.json`文件,使用工具`com.yubest.gen.MainApp.main`,生成公私钥及license授权文件 ```java 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); //生成license 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容器中 ```java @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); } } ``` * 3.4、安装证书 ```java @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("------结束注册证书------"); } } ``` * 3.5、添加接口拦截配置 ```java @Configuration public class InterceptorConfig implements WebMvcConfigurer { @Autowired private LicenseInterceptor licenseInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { //添加要拦截的url 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 data = new HashMap<>(); data.put("code", 1000); data.put("msg", "当前服务器不在授权范围内"); response.getWriter().write(new Gson().toJson(data)); return false; } } ```