栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

SpringBoot集成License

Java 更新时间:发布时间: 百科书网 趣学号
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);

            //生成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容器中
@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、安装证书
@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、添加接口拦截配置
@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;
    }
}
码云:gitee.com/hweiyu/spring-boot-license
转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/986792.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号