来源:自学PHP网 时间:2015-04-17 12:00 作者: 阅读:次
[导读] 记录下:CheckMojo.java01package com.neeao.security.ibatis_sql_injection_check;02import java.io.File;03import java.io.IOException;04import java.util.ArrayList;0506import org.apache.commons.io.......
记录下:
CheckMojo.java
01
package com.neeao.security.ibatis_sql_injection_check;
02
import java.io.File;
03
import java.io.IOException;
04
import java.util.ArrayList;
05
06
import org.apache.commons.io.FileUtils;
07
import org.apache.maven.plugin.AbstractMojo;
08
import org.apache.maven.plugin.MojoExecutionException;
09
10
/**
11
* @author Neeao
12
* @goal check
13
* @phase prepare-package
14
*/
15
public class CheckMojo extends AbstractMojo {
16
/**
17
* Web资源文件目录
18
*
19
* @parameter expression="${basedir}/src/main/resources"
20
*/
21
private File resourcesDirectory;
22
23
public void execute() throws MojoExecutionException {
24
getLog().info("start sql injection check...");
25
26
File resourcesDir = resourcesDirectory;
27
if (resourcesDir.exists()) {
28
getLog().info("Find ibatis xml file...");
29
findFiles(resourcesDir);
30
31
}
32
}
33
/**
34
* 查找文件
35
* @param dir
36
*/
37
private void findFiles(File dir) {
38
File[] files = dir.listFiles();
39
for (File f : files) {
40
if (f.isFile()&&f.getName().toLowerCase().endsWith(".xml")) {
41
getLog().info("find xml file:" + f.getAbsolutePath());
42
checkFile(f.getAbsolutePath());
43
} else if (f.isDirectory()) {
44
findFiles(f);
45
}
46
}
47
}
48
/**
49
* 检查文件
50
* @param filename
51
*/
52
private void checkFile(String filename) {
53
ArrayList<String> content = new ArrayList<String>();
54
try {
55
content = (ArrayList<String>) FileUtils.readLines(new File(filename));
56
int i=1;
57
for (String line : content) {
58
if (line.contains("$")) {
59
getLog().error(filename+",line:"+i+","+line);
60
}
61
i++;
62
}
63
} catch (IOException e) {
64
e.printStackTrace();
65
}
66
}
67
68
public File getResourcesDirectory() {
69
return resourcesDirectory;
70
}
71
72
public void setResourcesDirectory(File resourcesDirectory) {
73
this.resourcesDirectory = resourcesDirectory;
74
}
75
}
1
pom.xml文件:
01
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
02
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
03
<modelVersion>4.0.0</modelVersion>
04
<groupId>com.neeao.security</groupId>
05
<artifactId>sql--injection-check</artifactId>
06
<packaging>maven-plugin</packaging>
07
<version>1.0</version>
08
<name>sql-injection-check Maven Mojo</name>
09
<url>http://maven.apache.org</url>
10
<dependencies>
11
<dependency>
12
<groupId>org.apache.maven</groupId>
13
<artifactId>maven-plugin-api</artifactId>
14
<version>2.0</version>
15
</dependency>
16
<dependency>
17
<groupId>commons-io</groupId>
18
<artifactId>commons-io</artifactId>
19
<version>2.4</version>
20
</dependency>
21
</dependencies>
22
</project>
test方法:
mvn clear
mvn packape
mvn install
D:\workspace\ibatis-sql-injection-check>mvn com.neeao.security:sql--injection-ch
eck:1.0:check
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building sql-injection-check Maven Mojo 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- sql--injection-check:1.0:check (default-cli) @ sql--injection-check -
-- www.2cto.com
[INFO] start sql injection check...
[INFO] Find ibatis xml file...
[INFO] find xml file:D:\workspace\ibatis-sql-injection-check\src\main\resources\
NewFile.xml
[ERROR] D:\workspace\ibatis-sql-injection-check\src\main\resources\NewFile.xml,l
ine:3, name like '%$name$%'
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.196s
[INFO] Finished at: Tue Nov 27 23:59:25 CST 2012
[INFO] Final Memory: 2M/15M
[INFO] ------------------------------------------------------------------------
|
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com