使用Proguard对FAT JAR包进行代码混淆

2024-04-25 18:58

本文主要是介绍使用Proguard对FAT JAR包进行代码混淆,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用Proguard对FAT JAR包进行代码混淆

  • 1. 解压jar到目标目录
  • 2. 配置混淆文件
  • 3. 通过命令行执行代码混淆
  • 4. 对混淆后的jar包重新打包
  • 5. 将重新打包的JAR再次打包成FAT Jar
  • 参考

1. 解压jar到目标目录

jar xvf original.jar

original.jar为FAT Jar。假设解压目录为 D:/proguard/original

  • toman-service-1.0.jar
  • classes

2. 配置混淆文件

建议下载ProGuard通过GUI进行配置文件设置,设置完毕后通过Save configuration…保存配置文件。配置文件保存到D:/proguard/config/proguard-for-jar

为了更好的使用ProGuard,或者一开始就知道代码需要进行混淆的情况下,最好将@Component,@Controller,@Service,@RestController,@Repository,@Resource等注解都加上名字。

在我们的系统里DAO,API,常量等相关JAR包时没有进行混淆的。各系统可以根据自己的实际情况决定对哪进行混淆。
在这里插入图片描述
配置文件示例:

# 带混淆JAR以及对应输出jar
-injars original\toman-service-1.0.jar
-outjars target\toman-service-pro-1.0.jar
-injars classes(**.class)
-outjars target\classes-pro.jar# jdk jars
-libraryjars 'C:\Program Files\Java\jre1.8.0_191\lib\rt.jar'
-libraryjars 'C:\Program Files\Java\jre1.8.0_191\lib\jce.jar'# third-part jar
-libraryjars lib\ajdv-0.1.jar
-libraryjars lib\annotations-3.0.0.jar
-libraryjars lib\ant-1.8.0.jar
-libraryjars lib\ant-launcher-1.8.0.jar
-libraryjars lib\aspectjweaver-1.8.13.jar
-libraryjars lib\btf-1.2.jar
-libraryjars lib\c3p0-0.9.5.2.jar
# 应用中无需混淆JAR
-libraryjars lib\toman-api-1.0.0-SNAPSHOT.jar-dontshrink
-dontoptimize
# 定义输出mapping文件
-printmapping toman-mapping
# 混淆后类名不区分大小写(windows下不区分大小写)
-dontusemixedcaseclassnames
# 不做混淆的包名
-keeppackagenames com.toman.product
# 以下属性不做混淆
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,Synthetic,EnclosingMethod
-dontpreverify
# 忽略警告信息
-ignorewarnings# 保留@Configuration注解的类的方法名
# keep classes and class members,  @Configuration, com.toman.product.**, <methods>
-keep @org.springframework.context.annotation.Configuration class com.toman.product.** {<methods>;
}# 保留所有@Component注解的类的@Resource注解的字段以及@Pointcut注解的方法
# keep class members only, @Component, com.toman.product.**, <fields>
-keepclassmembers @org.springframework.stereotype.Component class com.toman.product.** {@javax.annotation.Resource<fields>;@org.aspectj.lang.annotation.Pointcut<methods>;
}# 保留@RestController注解的类的@Resource注解的字段
# keep class members only, @RestController, com.toman.product.**, <fields>
-keepclassmembers @org.springframework.web.bind.annotation.RestController class com.toman.product.** {@javax.annotation.Resource<fields>;
}# 保留@Service注解的类的@Resource注解的字段
# keep class members only, @Service, com.toman.product.**, <fields>
-keepclassmembers @org.springframework.stereotype.Service class com.toman.product.** {@javax.annotation.Resource<fields>;
}# 保留@Controller注解的类的@Resource注解的字段
# keep class members only, @Controller, com.toman.product.**, <fields>
-keepclassmembers @org.springframework.stereotype.Controller class com.toman.product.** {@javax.annotation.Resource<fields>;
}# 保留@ConfigurationProperties注解的类的字段名和方法名
# keep class members only, @ConfigurationProperties, com.toman.product.**, <fields><methods>
-keepclassmembers @org.springframework.boot.context.properties.ConfigurationProperties class com.toman.product.** {<fields>;<methods>;
}# 保留所有领域对象的字段名和方法名
-keepclassmembers class com.toman.product.core.domain.** {<fields>;<methods>;
}# 以下为ProGuard自带配置
# Keep - Applications. Keep all application classes, along with their 'main' methods.
-keepclasseswithmembers public class * {public static void main(java.lang.String[]);
}# 以下部分为节省空间已删除
# enumeration classes.
# Also keep - Database drivers. Keep all implementations of java.sql.Driver.
# Also keep - Swing UI L&F. Keep all extensions of javax.swing.plaf.ComponentUI,
# along with the special 'createUI' method.
# Keep - Native method names. Keep all native class/method names.
# Remove - System method calls. Remove all invocations of System
# methods without side effects whose return values are not used.
# Remove - Math method calls. Remove all invocations of Math
# methods without side effects whose return values are not used.
# Remove - Number method calls. Remove all invocations of Number
# methods without side effects whose return values are not used.
# Remove - String method calls. Remove all invocations of String
# methods without side effects whose return values are not used.
# Remove - StringBuffer method calls. Remove all invocations of StringBuffer
# methods without side effects whose return values are not used.
# Remove - StringBuilder method calls. Remove all invocations of StringBuilder
# methods without side effects whose return values are not used.

3. 通过命令行执行代码混淆

%PROGUARD_HOME%\bin\proguard.bat -include D:/proguard/config/proguard-for-ja -basedirectory D:/proguard

混淆后的代码存储到D:/proguard/target。但是混淆后生成的jar包并不能直接使用,需要重新进行打包

  • toman-service-pro-1.0.jar
  • classes-pro.jar

4. 对混淆后的jar包重新打包

对混淆后的JAR包解压

jar xvf toman-service-pro-1.0.jar
jar xvf classes-pro.jar

将解压后的class文件重新打包成JAR

jar -cvfM0 toman-service-1.0.jar ../toman-service-pro-1.0
jar -cvfM0 classes.jar ../classes-pro

5. 将重新打包的JAR再次打包成FAT Jar

jar -cvfM0 target.jar .

参考

使用proguard进行javaweb代码混淆

这篇关于使用Proguard对FAT JAR包进行代码混淆的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/935523

相关文章

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

MySQL中比较运算符的具体使用

《MySQL中比较运算符的具体使用》本文介绍了SQL中常用的符号类型和非符号类型运算符,符号类型运算符包括等于(=)、安全等于(=)、不等于(/!=)、大小比较(,=,,=)等,感兴趣的可以了解一下... 目录符号类型运算符1. 等于运算符=2. 安全等于运算符<=>3. 不等于运算符<>或!=4. 小于运

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

MySQL进行数据库审计的详细步骤和示例代码

《MySQL进行数据库审计的详细步骤和示例代码》数据库审计通过触发器、内置功能及第三方工具记录和监控数据库活动,确保安全、完整与合规,Java代码实现自动化日志记录,整合分析系统提升监控效率,本文给大... 目录一、数据库审计的基本概念二、使用触发器进行数据库审计1. 创建审计表2. 创建触发器三、Java

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断