【Developer Log】ProGuard扰码web项目(WAR)

本文详细介绍了如何解决ProGuard在WAR项目中混淆*.class文件时遇到的警告问题,通过将类文件打包成jar并放置在WEB-INF/lib目录下实现有效混淆。包括打成jar包、混淆jar、封装war等关键步骤,并提供了实操指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先阅读一下http://proguard.sourceforge.net/manual/troubleshooting.html#unexpectedclass,内容如下:

Warning: class file ... unexpectedly contains class ...
The given class file contains a definition for the given class, but the directory name of the file doesn't correspond to the package name of the class. ProGuard will accept the class definition, but the current implementation will not write out the processed version. Please make sure your input classes are packaged correctly. Notably, class files that are in the WEB-INF/classes directory in a war should be packaged in a jar and put in the WEB-INF/lib directory. If you don't mind these classes not being written to the output, you can specify the
-ignorewarnings option, or even the-dontwarn option.

war项目中java编译后放在WEB-INF/classes中,如果我们对其proguard,就不断地报Warning: class file ... unexpectedly contains class ...,然后混淆失败。上面这段话说得很明白,目前proguard的版本不支持对WEB-INF/classes里面的*.class进行混淆,而WEB-INF/classes中的正是我们要进行保护,避免反编译的内容。我上网查了一下,大致都在说如果进行proguard配置来对war进行混淆。I am amazing,各路大神是怎么做到的,真的能混淆需要保护的内容吗?

这段文字给给出了一个很重要的建议,即将WEB-INF/classes其打包成jar,放置在WEB-INF/lib中。由此,我们可以对我们编写的代码进行混淆。

step 1:打成jar

选择项目,可以直接在src/main/java中点击右键,export为jar包,例如a.jar。这个jar包实际上就是我们希望混淆的内容,那么为何不直接对其进行混淆。

step2:混淆jar

如果采用annotation而非在web.xml中定义servelet,我们就无需保留servelet的名字(已经相关的package名字),对Fliter也是同理。下面是proguard的配置。

在Input/Output中引入所有涉及的jar包,包括javax的servlet。

step3:封装war

我们将经过混淆后的a-proguard.jar放在WEB-INF/lib/中,同时删除classes/,如果用maven,还需要删除test-classes/。在Eclipse的项目中F5进行更新,然后右键export为war,这样即可。

最后,通过解压缩工具,例如7z打开war包进行检查。

proguard的目的是防止反编译,不在乎强求是否直接将war还是jar进行混淆。通过上面的步骤可以有效地对我们编写的代码进行混淆,目的达到。

 

相关链接:开发日志

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值