SpringBoot集成License

本文详细介绍了如何使用Java进行授权许可管理,包括获取目标机器硬件信息生成device.json,使用该信息生成公私钥和license文件,以及在Spring Boot应用中进行授权使用、证书注册、接口拦截等步骤,确保授权流程的安全和有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

### 在 Spring Boot 中引入和配置 License 支持 为了在 Spring Boot 项目中实现 license 功能的支持,通常需要考虑几个方面:一是选择合适的库来处理许可证逻辑;二是确保应用程序能够验证并应用这些许可规则。下面介绍一种常见的方式——通过使用开源的 Java 库如 `License4J` 来完成这一目标。 #### 添加 Maven 或 Gradle 依赖项 对于基于 Maven 的构建工具,在项目的 `pom.xml` 文件里加入如下依赖: ```xml <dependency> <groupId>com.synaptics</groupId> <artifactId>license4j-runtime</artifactId> <version>7.0.1</version> </dependency> ``` 如果是采用 Gradle,则应在 build.gradle 文件中添加相应的依赖声明: ```groovy implementation 'com.synaptics:license4j-runtime:7.0.1' ``` 这一步骤使得开发者可以利用该库所提供的 API 进行许可证管理操作[^1]。 #### 创建自定义 Starter 类用于初始化 License Manager 创建一个新的类继承自 `org.springframework.boot.autoconfigure.AutoConfigurationImportSelector` 并重写其中的方法以加载特定于业务需求的配置属性和服务组件。此过程允许更加灵活地控制启动期间的行为以及与其他框架组件之间的交互方式。 ```java import org.springframework.context.annotation.Configuration; import com.rimerosolutions.spring.LicenseManager; @Configuration(proxyBeanMethods = false) public class MyCustomLicenseAutoConfig { private final LicenseManager licenseManager; public MyCustomLicenseAutoConfig(LicenseManager manager){ this.licenseManager=manager; } @PostConstruct void init(){ try { // 初始化 License Manager 实例... System.out.println("Initializing the license system..."); } catch (Exception e) { throw new RuntimeException(e); } } } ``` 上述代码片段展示了如何编写一个简单的自动配置器,它会在应用程序上下文中注册必要的 bean 同时执行某些预设的任务,比如读取外部文件或者环境变量中的密钥信息以便激活产品特性等功能[^2]。 #### 配置 application.properties/yml 文件 最后不要忘记更新 `application.properties` 或者 YAML 版本的应用程序设置文档,指定与授权有关的各项参数值,例如有效期、客户ID等字段的具体含义取决于所选用的具体解决方案提供商的要求。 ```properties app.license.keyPath=/path/to/your/license/keyfile.dat app.product.id=my-product-id-string-here app.customer.name=YourCustomerNameHere ``` 以上就是关于怎样在一个典型的 Spring Boot 工程内部集成本地化软件保护机制的大致流程概述[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值