美文网首页
springboot项目授权许可

springboot项目授权许可

作者: 不减肥到一百三不改名字 | 来源:发表于2023-10-17 13:50 被阅读0次

    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

    相关文章

      网友评论

          本文标题:springboot项目授权许可

          本文链接:https://www.haomeiwen.com/subject/ajtqidtx.html