本文中介绍的项目使用了SSM框架。
项目包结构:
该项目是典型的Maven WEB项目
主要混淆对象 对类的名称、属性、方法名都进行混淆
次要混淆对象 对类的名称不混淆,类的属性、方法名选择性混淆
非混淆对象 不进行混淆,混淆后可能出现异常
Maven 配置(pom.xml)
使用Maven集成的ProGuard插件,混淆配置不用单独建立文件
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>
- 4.0.0
- </modelVersion>
- <groupId>
- com.test
- </groupId>
- <artifactId>
- prog
- </artifactId>
- <packaging>
- war
- </packaging>
- <version>
- hx
- </version>
- <name>
- prog Maven Webapp
- </name>
- <url>
- http://maven.apache.org
- </url>
- <properties>
- <project.build.sourceEncoding>
- UTF-8
- </project.build.sourceEncoding>
- <mybatis.generator.configurationFile>
- src/test/resources/mybatisGenerator.xml
- </mybatis.generator.configurationFile>
- </properties>
- <dependencies>
- <dependency>
- <groupId>
- javax.persistence
- </groupId>
- <artifactId>
- persistence-api
- </artifactId>
- <version>
- 1.0
- </version>
- </dependency>
- <dependency>
- <groupId>
- com.googlecode.sli4j
- </groupId>
- <artifactId>
- sli4j-slf4j-log4j
- </artifactId>
- <version>
- 2.0
- </version>
- </dependency>
- <dependency>
- <groupId>
- junit
- </groupId>
- <artifactId>
- junit
- </artifactId>
- <version>
- 4.11
- </version>
- <scope>
- test
- </scope>
- </dependency>
- <dependency>
- <groupId>
- net.sf.ehcache
- </groupId>
- <artifactId>
- ehcache-core
- </artifactId>
- <version>
- 2.6.5
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.codehaus.jackson
- </groupId>
- <artifactId>
- jackson-mapper-asl
- </artifactId>
- <version>
- 1.9.13
- </version>
- </dependency>
- <dependency>
- <groupId>
- net.sf.json-lib
- </groupId>
- <artifactId>
- json-lib
- </artifactId>
- <version>
- 2.4
- </version>
- <classifier>
- jdk15
- </classifier>
- </dependency>
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-core
- </artifactId>
- <version>
- 3.2.5.RELEASE
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-context-support
- </artifactId>
- <version>
- 3.2.4.RELEASE
- </version>
- <type>
- jar
- </type>
- <scope>
- compile
- </scope>
- </dependency>
- <dependency>
- <groupId>
- org.mybatis
- </groupId>
- <artifactId>
- mybatis-spring
- </artifactId>
- <version>
- 1.2.1
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.mybatis
- </groupId>
- <artifactId>
- mybatis-ehcache
- </artifactId>
- <version>
- 1.0.0
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-jdbc
- </artifactId>
- <version>
- 3.2.5.RELEASE
- </version>
- </dependency>
- <!-- 阿里巴巴数据源包 -->
- <dependency>
- <groupId>
- com.alibaba
- </groupId>
- <artifactId>
- druid
- </artifactId>
- <version>
- 1.0.2
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.apache.poi
- </groupId>
- <artifactId>
- poi
- </artifactId>
- <version>
- 3.8
- </version>
- </dependency>
- <dependency>
- <groupId>
- </groupId>
- <artifactId>
- google-connections
- </artifactId>
- <version>
- 1.0
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-webmvc
- </artifactId>
- <version>
- 3.2.5.RELEASE
- </version>
- </dependency>
- <dependency>
- <groupId>
- commons-httpclient
- </groupId>
- <artifactId>
- commons-httpclient
- </artifactId>
- <version>
- 3.1
- </version>
- </dependency>
- <!-- cxf.jar -->
- <dependency>
- <groupId>
- org.apache.cxf
- </groupId>
- <artifactId>
- cxf-rt-frontend-jaxws
- </artifactId>
- <version>
- 2.7.6
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.apache
- </groupId>
- <artifactId>
- neethi
- </artifactId>
- <version>
- 3.0.2
- </version>
- </dependency>
- <dependency>
- <groupId>
- wsdl4j
- </groupId>
- <artifactId>
- wsdl4j
- </artifactId>
- <version>
- 1.6.3
- </version>
- <type>
- jar
- </type>
- <scope>
- compile
- </scope>
- </dependency>
- <dependency>
- <groupId>
- org.apache.ws
- </groupId>
- <artifactId>
- commons.schema
- </artifactId>
- <version>
- 2.0.3
- </version>
- </dependency>
- <dependency>
- <groupId>
- com.ctc
- </groupId>
- <artifactId>
- wstx
- </artifactId>
- <version>
- 4.2.0
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.codehaus
- </groupId>
- <artifactId>
- stax2
- </artifactId>
- <version>
- 3.1.1
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-beans
- </artifactId>
- <version>
- 3.2.5.RELEASE
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-test
- </artifactId>
- <version>
- 3.2.5.RELEASE
- </version>
- <scope>
- test
- </scope>
- </dependency>
- <dependency>
- <groupId>
- org.aspectj
- </groupId>
- <artifactId>
- aspectjweaver
- </artifactId>
- <version>
- 1.7.4
- </version>
- </dependency>
- <!-- mysql驱动包 -->
- <dependency>
- <groupId>
- mysql
- </groupId>
- <artifactId>
- mysql-connector-java
- </artifactId>
- <version>
- 5.1.12
- </version>
- </dependency>
- <dependency>
- <groupId>
- net.sourceforge.jtds
- </groupId>
- <artifactId>
- jtds
- </artifactId>
- <version>
- 1.3.0
- </version>
- </dependency>
- <!-- <dependency>
- <groupId>com.oracle</groupId>
- <artifactId>ojdbc14</artifactId>
- <version>10.2.0.1.0</version>
- </dependency> -->
- <dependency>
- <groupId>
- org.mybatis
- </groupId>
- <artifactId>
- mybatis
- </artifactId>
- <version>
- 3.1.1
- </version>
- </dependency>
- <dependency>
- <groupId>
- javax
- </groupId>
- <artifactId>
- javaee-api
- </artifactId>
- <version>
- 6.0
- </version>
- <scope>
- provided
- </scope>
- <!-- <scope>compile</scope>-->
- </dependency>
- <dependency>
- <groupId>
- javax.mail
- </groupId>
- <artifactId>
- </artifactId>
- <version>
- 1.4.7
- </version>
- </dependency>
- <dependency>
- <groupId>
- javax.servlet
- </groupId>
- <artifactId>
- jstl
- </artifactId>
- <version>
- 1.2
- </version>
- </dependency>
- <dependency>
- <groupId>
- commons-fileupload
- </groupId>
- <artifactId>
- commons-fileupload
- </artifactId>
- <version>
- 1.3
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.apache.clerezza.ext
- </groupId>
- <artifactId>
- org.json.simple
- </artifactId>
- <version>
- 0.3-incubating
- </version>
- <scope>
- compile
- </scope>
- </dependency>
- <dependency>
- <groupId>
- com.alibaba
- </groupId>
- <artifactId>
- fastjson
- </artifactId>
- <version>
- 1.1.41
- </version>
- </dependency>
- <dependency>
- <groupId>
- dom4j
- </groupId>
- <artifactId>
- dom4j
- </artifactId>
- <version>
- 1.6
- </version>
- </dependency>
- <!--二维码-->
- <dependency>
- <groupId>
- com.google.zxing
- </groupId>
- <artifactId>
- core
- </artifactId>
- <version>
- 3.0.1
- </version>
- </dependency>
- <!--缩略图-->
- <dependency>
- <groupId>
- net.coobird
- </groupId>
- <artifactId>
- thumbnailator
- </artifactId>
- <version>
- 0.4.8
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-orm
- </artifactId>
- <version>
- 3.2.5.RELEASE
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.unitils
- </groupId>
- <artifactId>
- unitils-core
- </artifactId>
- <version>
- 3.1
- </version>
- <scope>
- test
- </scope>
- </dependency>
- <dependency>
- <groupId>
- org.unitils
- </groupId>
- <artifactId>
- unitils-database
- </artifactId>
- <version>
- 3.1
- </version>
- <scope>
- test
- </scope>
- </dependency>
- <dependency>
- <groupId>
- org.unitils
- </groupId>
- <artifactId>
- unitils-dbmaintainer
- </artifactId>
- <version>
- 3.1
- </version>
- <scope>
- test
- </scope>
- </dependency>
- <dependency>
- <groupId>
- org.unitils
- </groupId>
- <artifactId>
- unitils-dbunit
- </artifactId>
- <version>
- 3.1
- </version>
- <scope>
- test
- </scope>
- </dependency>
- <dependency>
- <groupId>
- org.unitils
- </groupId>
- <artifactId>
- unitils-spring
- </artifactId>
- <version>
- 3.1
- </version>
- <scope>
- test
- </scope>
- </dependency>
- <dependency>
- <groupId>
- org.unitils
- </groupId>
- <artifactId>
- unitils-orm
- </artifactId>
- <version>
- 3.1
- </version>
- <scope>
- test
- </scope>
- </dependency>
- <dependency>
- <groupId>
- org.bouncycastle
- </groupId>
- <artifactId>
- bcprov-jdk14
- </artifactId>
- <version>
- 1.45
- </version>
- </dependency>
- <dependency>
- <groupId>
- sun.misc
- </groupId>
- <artifactId>
- BASE64Decoder
- </artifactId>
- <version>
- 1.2
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.quartz-scheduler
- </groupId>
- <artifactId>
- quartz
- </artifactId>
- <version>
- 1.8.5
- </version>
- </dependency>
- </dependencies>
- <build>
- <!-- <pluginManagement> -->
- <plugins>
- <plugin>
- <groupId>
- org.apache.maven.plugins
- </groupId>
- <artifactId>
- maven-compiler-plugin
- </artifactId>
- <version>
- 2.3.2
- </version>
- <configuration>
- <source>
- 1.7
- </source>
- <target>
- 1.7
- </target>
- <encoding>
- UTF-8
- </encoding>
- </configuration>
- </plugin>
- <plugin>
- <groupId>
- org.mybatis.generator
- </groupId>
- <artifactId>
- mybatis-generator-maven-plugin
- </artifactId>
- <version>
- 1.3.1
- </version>
- <configuration>
- <overwrite>
- false
- </overwrite>
- </configuration>
- </plugin>
- <plugin>
- <groupId>
- org.apache.tomcat.maven
- </groupId>
- <artifactId>
- tomcat7-maven-plugin
- </artifactId>
- <version>
- 2.1
- </version>
- <configuration>
- <uriEncoding>
- utf-8
- </uriEncoding>
- <port>
- 8888
- </port>
- <path>
- /
- </path>
- <contextReloadable>
- false
- </contextReloadable>
- <useTestClasspath>
- false
- </useTestClasspath>
- </configuration>
- </plugin>
- <plugin>
- <groupId>
- org.codehaus.mojo
- </groupId>
- <artifactId>
- tomcat-maven-plugin
- </artifactId>
- <version>
- 1.1
- </version>
- <configuration>
- <url>
- http://192.168.200.121:8080/manager/text
- </url>
- <username>
- tomcat
- </username>
- <password>
- password
- </password>
- <path>
- /prog11
- </path>
- </configuration>
- </plugin>
- <!-- ProGuard混淆插件-->
- <plugin>
- <groupId>
- com.github.wvengen
- </groupId>
- <artifactId>
- proguard-maven-plugin
- </artifactId>
- <version>
- 2.0.11
- </version>
- <executions>
- <execution>
- <!-- 混淆时刻,这里是打包的时候混淆-->
- <phase>
- package
- </phase>
- <goals>
- <!-- 使用插件的什么功能,当然是混淆-->
- <goal>
- proguard
- </goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <!-- 是否将生成的PG文件安装部署-->
- <attach>
- true
- </attach>
- <!-- 是否混淆-->
- <obfuscate>
- true
- </obfuscate>
- <!-- 指定生成文件分类 -->
- <attachArtifactClassifier>
- pg
- </attachArtifactClassifier>
- <options>
- <!-- JDK目标版本1.7-->
- <option>
- -target 1.7
- </option>
- <!-- 不做收缩(删除注释、未被引用代码)-->
- <option>
- -dontshrink
- </option>
- <!-- 不做优化(变更代码实现逻辑)-->
- <option>
- -dontoptimize
- </option>
- <!-- 不路过非公用类文件及成员-->
- <option>
- -dontskipnonpubliclibraryclasses
- </option>
- <option>
- -dontskipnonpubliclibraryclassmembers
- </option>
- <!-- 优化时允许访问并修改有修饰符的类和类的成员 -->
- <option>
- -allowaccessmodification
- </option>
- <!-- 确定统一的混淆类的成员名称来增加混淆,防止冲突-->
- <option>
- -useuniqueclassmembernames
- </option>
- <!-- 不混淆所有包名,Spring配置中有大量固定写法的包名-->
- <option>
- -keeppackagenames
- </option>
- <!-- 不混淆所有特殊的类-->
- <option>
- -keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod
- </option>
- <!-- 不混淆所有的set/get方法,毕竟项目中使用的部分第三方框架(例如Shiro)会用到大量的set/get映射-->
- <option>
- -keepclassmembers public class * {void set*(***);*** get*();}
- </option>
- <!-- 不混淆job包下的所有类名,且类中的方法也不混淆-->
- <!--<option>-keep class com.chinatelecom.gz.wy.zhukun.shiro_spring.job.** { <methods>; }</option> -->
- <!-- 不混淆filter包下的所有类名,这里主要是对Shiro的路踢人过滤器混淆,对类的属性和方法进行了混淆-->
- <!--<option>-keep class com.chinatelecom.gz.wy.zhukun.shiro_spring.filter.** </option> -->
- <!-- 不混淆model包中的所有类以及类的属性及方法,实体包,混淆了会导致ORM框架及前端无法识别-->
- <!--<option>-keep class com.chinatelecom.gz.wy.zhukun.shiro_spring.model.** {*;}</option>-->
- <!-- com.test.prog.util, util包不混淆的类的属性及方法,实体包 -->
- <option>
- -keep class com.test.prog.util.finals.Const{ *; }
- </option>
- <!-- 不混淆凭证包下的所有类名,但对类中的属性、方法进行混淆,原因是Spring配置中用到了这个类名-->
- <option>
- -keep class com.test.prog.util.SecCode
- </option>
- <option>
- -keep class com.test.prog.util.exception.HihSoftHandlerException
- </option>
- <!-- 不混淆job包下的所有类名,且类中的方法也不混淆-->
- <option>
- -keep class com.test.prog.controller.** { <methods>; }
- </option>
- </options>
- <outjar>
- ${project.build.finalName}-pg.jar
- </outjar>
- <!-- 添加依赖,这里你可以按你的需要修改,这里测试只需要一个JRE的Runtime包就行了 -->
- <libs>
- <lib>
- ${java.home}/lib/rt.jar
- </lib>
- <lib>
- ${java.home}/lib/jce.jar
- </lib>
- <lib>
- ${java.home}/lib/security/local_policy.jar
- </lib>
- <lib>
- ${java.home}/lib/security/US_export_policy.jar
- </lib>
- <lib>
- ${java.home}/lib/ext/sunjce_provider.jar
- </lib>
- </libs>
- <!-- 加载文件的过滤器,就是你的工程目录了-->
- <inFilter>
- com/test/prog/**
- </inFilter>
- <!-- 对什么东西进行加载,这里仅有classes成功,毕竟你也不可能对配置文件及JSP混淆吧-->
- <injar>
- classes
- </injar>
- <!-- 输出目录-->
- <outputDirectory>
- ${project.build.directory}
- </outputDirectory>
- </configuration>
- </plugin>
- </plugins>
- <!-- </pluginManagement> -->
- <resources>
- <resource>
- <directory>
- src/main/resources
- </directory>
- <includes>
- <include>
- **/*.properties
- </include>
- <include>
- **/*.xml
- </include>
- </includes>
- </resource>
- <resource>
- <directory>
- src/main/java
- </directory>
- <excludes>
- <exclude>
- **/*.java
- </exclude>
- <exclude>
- **/.svn/*
- </exclude>
- </excludes>
- </resource>
- </resources>
- </build>
- </project>
- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
- 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
- 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
- 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
- 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
- 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
- 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
- 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
- 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
- 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
- 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
- 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
- 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
- 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
- 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
- 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
- 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
- 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
- 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
- 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
- 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
- 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
- 424 425 426 427
在项目location执行命令
mvn clean package -DskipTests -X
执行命令后,会在target下生成以下文件:
E:\tomcat7\webapps\项目名\WEB-INF\classes
运行服务,项目运行成功
查看目录下面文件(下面是包括包名的混淆)
通过反编译工具查看混淆代码
可以看出,混淆成功了!
下面是测试中遇到的部分问题及相关的解决方式:
解析:-target 1.7 jdk目标版本的问题
解析:-dontskipnonpubliclibraryclasses 不路过非公用类文件及成员
解析:-dontskipnonpubliclibraryclasses -dontskipnonpubliclibraryclassmembers 不路过非公用类文件及成员造成的bug
解析:-dontoptimize 变更代码逻辑引起的bug
还有一些其他的关于项目不能混淆的bug,这个需要根据项目而定。
百度搜索“就爱阅读”,专业资料,生活学习,尽在就爱阅读网92to.com,您的在线图书馆!
来源: http://www.92to.com/bangong/2017/12-01/31968310.html