上一篇使用progurad混淆spring cloud代碼,實際看下來發現混淆代碼效果不好,價值不大。於是改換為:allatori,在參考網上代碼后,終於實驗成功,過程如下:
在代碼的根目錄下新建文件夾lib,下載allatori,地址,下載完成后,將allatori拷貝到lib文件夾(目前已經到8.0版了)
然后在文件夾中建立配置文件,allatori.xml:
<config> <input> <!-- in表示輸出的原始jar包,out表示輸出的混淆后的jar包,后者名稱可自定義,也可以是war --> <jar in="api-1.0-SNAPSHOT.jar" out="api-encrypted.jar"/> </input> <keep-names> <class access="protected+"> <field access="protected+"/> <method access="protected+"/> </class> <!-- 視圖層的方法參數名稱不做混淆,否則傳參會對應不上,不怕麻煩的也可以加@RequestParam指定入參名稱 --> <class template="class *Controller"> <method template="private+ *(**)" parameters="keep"/> </class> </keep-names> <property name="log-file" value="log.xml"/> <!-- 忽略的包或類,這些文件將不被混淆 --> <ignore-classes> <class template="class *springframework*" /> <class template="class *shardingjdbc*" /> <class template="class *jni*" /> <class template="class *alibaba*"/> <class template="class *persistence*"/> <!-- 排除業務類 --> <class template="class cn.*******.App"/> <class template="class cn.*******.entity.*" /> <class template="class cn.*******.mapper.*" /> <class template="class cn.*******.web.*" /> </ignore-classes> <!-- 到期時間(到期后無法啟動jar) 格式:yyyy/mm/dd--> <!--<expiry date="2021/04/03" string="SERVICE EXPIRED!"/>--> <!-- 隨機命名混淆字符,默認用當前時間,每次打包混淆的類名、變量名都不一樣,如果做了配置那么兩次打包內容就一樣--> <!--<property name="random-seed" value="abcdef ghnljk svi"/>--> </config>
然后,在pom文件中引入2個插件:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-and-filter-allatori-config</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target</outputDirectory> <resources> <resource> <directory>${basedir}/lib</directory> <includes> <include>allatori.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> <!-- Allatori 插件 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <id>run-allatori</id> <phase>package</phase> <goals> <goal>exec</goal> </goals> </execution> </executions> <configuration> <executable>java</executable> <arguments> <argument>-Xms128m</argument> <argument>-Xmx512m</argument> <argument>-jar</argument> <argument>${basedir}/lib/allatori.jar</argument> <argument>${basedir}/target/allatori.xml</argument> </arguments> </configuration> </plugin>
另外,最好攜帶上maven打包插件,這樣打包時會攜帶上所需要的jar
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
然后打包,打包后再點擊package,則會將打包后的jar加密成可執行的加密包。
參考:allatori代碼混淆實現(springboot、springmvc)