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

来源:互联网 发布:奶油知多少 编辑:程序博客网 时间:2024/05/18 21:50

先阅读一下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进行混淆。通过上面的步骤可以有效地对我们编写的代码进行混淆,目的达到。

 

相关链接:开发日志

0 0