SpringBoot集成License

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<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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值