51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Spring Boot集成xjar快速入门Demo

1.什么是xjar?

Spring Boot JAR 安全加密运行工具, 同时支持的原生JAR.

基于对JAR包内资源的加密以及拓展ClassLoader来构建的一套程序加密启动, 动态解密运行的方案, 避免源码泄露以及反编译.

功能特性

  • 无代码侵入, 只需要把编译好的JAR包通过工具加密即可.

  • 完全内存解密, 降低源码以及字节码泄露或反编译的风险.

  • 支持所有JDK内置加解密算法.

  • 可选择需要加解密的字节码或其他资源文件.

  • 支持Maven插件, 加密更加便捷.

  • 动态生成Go启动器, 保护密码不泄露.

2.环境准备

mac更新brew,并安装go * *

git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallowgit -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow

更新 *

brew update

安装go *

brew install go

检查go版本 *

go version

3.代码工程

实验目标

使用xjar 加密spring boot jar包

第一种方法采用maven插件

<build>    <plugins>
        <plugin>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-maven-plugin</artifactId>            <executions>                <execution>                    <goals>                        <goal>repackage</goal>                    </goals>                </execution>            </executions>        </plugin>        <plugin>            <groupId>com.github.core-lib</groupId>            <artifactId>xjar-maven-plugin</artifactId>            <version>4.0.2</version>            <executions>                <execution>                    <goals>                        <goal>build</goal>                    </goals>                    <!--yoou can change install-->                    <phase>package</phase>                    <configuration>                        <password>xxxx</password>                        <!-- need enc resources -->                        <includes>                                     <include>com/et/**</include>                            <include>mapper/*Mapper.xml</include>                            <include>config/**</include>                        </includes>                        <!-- no need enc resources -->                        <excludes>                            <exclude>static/**</exclude>                            <exclude>META-INF/**</exclude>                        </excludes>                        <!--target jar dir -->                        <targetDir>${project.build.directory}\xJarDir\</targetDir>                        <!-- target jar name -->                        <targetJar>zsplat.jar</targetJar>                    </configuration>                </execution>            </executions>        </plugin>    </plugins></build>

对于 Spring Boot 项目或模块, 该插件要后于 spring-boot-maven-plugin 插件执行, 有两种方式:

  • 将插件放置于 spring-boot-maven-plugin 的后面, 因为其插件的默认 phase 也是 package

  • 将插件的 phase 设置为 install(默认值为:package), 打包命令采用 mvn clean install

也可以通过Maven命令执行 * *

mvn xjar:build -Dxjar.password=io.xjarmvn xjar:build -Dxjar.password=io.xjar -Dxjar.targetDir=/directory/to/save/target.xjar

但通常情况下是让XJar插件绑定到指定的phase中自动执行, 这样就能在项目构建的时候自动构建出加密的包. * *

mvn clean package -Dxjar.password=io.xjarmvn clean install -Dxjar.password=io.xjar -Dxjar.targetDir=/directory/to/save/target.xjar

强烈建议

强烈建议不要在 pom.xml 的 xjar-maven-plugin 配置中写上密码,这样会导致打包出来的 xjar 包中的 pom.xml 文件保留着密码,极其容易暴露密码!强烈推荐通过 mvn 命令来指定加密密钥!

第二种方式代码加密

引入jar

<project> <!-- 设置 jitpack.io 仓库 --> <repositories> <repository> <id>jitpack.io</id> <url>https://jitpack.io</url> </repository> </repositories> <!-- 添加 XJar 依赖 --> <dependencies> <dependency> <groupId>com.github.core-lib</groupId> <artifactId>xjar</artifactId> <version>4.0.2</version> <!-- <scope>test</scope> --> </dependency> </dependencies></project>
  • 必须添加 https://jitpack.io Maven仓库.

  • 如果使用 JUnit 测试类来运行加密可以将 XJar 依赖的 scope 设置为 test.

2. 加密源码

package com.et.xjar;
import io.xjar.XCryptos;
public class JarEncryptio {    public static void main(String[] args) throws Exception {        // Spring-Boot Jar包加密        XCryptos.encryption()                .from("/Users/liuhaihua/IdeaProjects/springboot-demo/xjar/target/xjar-1.0-SNAPSHOT.jar")                .use("passwad")                .exclude("/static/**/*")                .exclude("/templates/**/*")                .exclude("/META-INF/resources/**/*")                .to("/Users/liuhaihua/IdeaProjects/springboot-demo/xjar/target/xJarDir/xjar-encryption.jar");        System.out.println("success");    }}

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

  • https://github.com/Harries/springboot-demo(xjar)

4.测试

加密

1.maven执行 *

mvn clean package

2.运行Java代码执行main方法加密

编译不同平台的启动器

将 xjar.go 在不同的平台进行编译即可得到不同平台的启动器可执行文件, 其中Windows下文件名为 xjar.exe 而Linux下为 xjar *

go build xjar.go

运行

运行环境只需要有java环境即可 * *

./xjar java --add-opens java.base/jdk.internal.loader=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.net=ALL-UNNAMED -jar xjar-encryption.jar

5.引用

  • https://github.com/core-lib/xjar

  • http://www.liuhaihua.cn/archives/710942.html

赞(4)
未经允许不得转载:工具盒子 » Spring Boot集成xjar快速入门Demo