springboot项目授权许可

springboot项目授权许可
使用smart-license1.0.3,给pringboot请求加载拦截器,用于商业许可授权,软件试用。smart-license使用指南

一、使用smart-license生成授权文件source.txt
下载smart-license-1.0.3.tar.gz文件,使用1.0.3就行。在linux下运行脚本,会生成source.txt(用于解密)和license.txt

[root@master2 bin]# ./license.sh 1h hello
sign for string:hello
License data :aGVsbG8=
Expire Date:2023-08-24 10:19:56
[root@master2 bin]# ls
keypair.bat  keypair.sh  license.bat  license_revert.bat  license_revert.sh  license.sh  license.txt  source.txt

二、在springboot中配置

将source.txt文件放在resource目录下,也可以自定义存放位置。

一、添加pom依赖

    <!--smart-license 1.0.3授权-->
        <dependency>
            <groupId>org.smartboot.license</groupId>
            <artifactId>license-client</artifactId>
            <version>1.0.3</version>
        </dependency>

二、授权文件校验程序

读取resource下中的license.txt文件进行校验,会校验文件中的过期时间,以及生成license时md5加密的值,如果不能通过验证,则会抛出异常。

/**
 * 许可授权拦截器
 */
public class GetLicense {
    public static void checkLicense() {
        ClassPathResource classPathResource = new ClassPathResource("license.txt");
        File file = null;
        try {
            file = classPathResource.getFile();
            String absolutePath = ResourceUtils.getFile("classpath:license.txt").getAbsolutePath();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        License license = new License();
        LicenseEntity licenseEntity = null;
        try {
            licenseEntity = license.loadLicense(file);
            String s1 = Md5Util.md5Encode("project", "utf-8");
            String md5 = licenseEntity.getMd5();
            if(!s1.equals(md5)){
                // 校验md5值是否相等
                throw new RuntimeException("软件授权文件无效");
            }
        } catch (LicenseException e){
            throw new RuntimeException("软件授权已过期,请联系软件提供商");
        }
    }
}

三、实现授权拦截器

实现拦截器HandlerInterceptor,在请求调用之前进行校验。

/**
 * 授权拦截器
 */
@Configuration
public class JarAuthInterceptor implements HandlerInterceptor {

    /**
     * 在请求处理之前进行调用(Controller方法调用之前)
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String requestURI = request.getRequestURI();
        //获取动态数据源名称
        GetLicense.checkLicense();
        return true;
    }
}

四、实现WebMvcConfigurer

WebMvcConfigurer可以用来添加自定义拦截器,拦截请求为所有路径

@Configuration
public class SignAuthConfiguration implements WebMvcConfigurer {
    @Autowired
    public JarAuthInterceptor jarAuthInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 许可授权拦截器
        registry.addInterceptor(jarAuthInterceptor).addPathPatterns("/**");
    }
}

三、拦截测试

加密编码不通过


image.png

过期不通过


image.png

原文链接:https://blog.csdn.net/qq_43146871/article/details/132467399

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容