当前位置: 移动技术网 > IT编程>开发语言>Java > Java项目安全处理方法

Java项目安全处理方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

一、url中参数显示问题,解决方案:

1、普通get请求修改为post请求

2、参数(js加密,java解密)

二、mybatis模糊查询中,sql拼接问题,解决方案方案:

1、使用安全的符号和方法,xml中拼接示例:

<if test="statelist != null">
  state in 
  <foreach close=")" collection="statelist" index="index" item="sta" open="(" separator=",">
    #{statelist[${index}]}
  </foreach>
</if>
<if test="title != null and title != ''">
    and title like concat('%',#{title},'%') 
</if>

2、java中转义特殊字符,java中字符处理示例:

param = param.replace("%", "\\%");
param = param.replace("_", "\\_");
param = param.replace(",", "\\,");
param = param.replace("'", "\\'");
param = param.replace("/", "//");
param = param.replace("\\", \\\\);

三、文件上传安全问题

解决方案:判断文件名、请求contenttype和文件头内容。

文件头内容判断:

常见文件类型识别

常用文件的头信息: 
jpeg (jpg),文件头:ffd8ffe1 
png (png),文件头:89504e47 
gif (gif),文件头:47494638
tiff (tif),文件头:49492a00 
windows bitmap (bmp),文件头:424d 
cad (dwg),文件头:41433130
adobe photoshop (psd),文件头:38425053
rich text format (rtf),文件头:7b5c727466 
xml (xml),文件头:3c3f786d6c 
html (html),文件头:68746d6c3e 
email [thorough only] (eml),文件头:44656c69766572792d646174653a 
outlook express (dbx),文件头:cfad12fec5fd746f 
outlook (pst),文件头:2142444e 
ms word/excel (xls.or.doc),文件头:d0cf11e0 
ms access (mdb),文件头:5374616e64617264204a 
wordperfect (wpd),文件头:ff575043 
postscript (eps.or.ps),文件头:252150532d41646f6265 
adobe acrobat (pdf),文件头:255044462d312e 
quicken (qdf),文件头:ac9ebd8f 
windows password (pwl),文件头:e3828596 
zip archive (zip),文件头:504b0304 
rar archive (rar),文件头:52617221
wave (wav),文件头:57415645
avi (avi),文件头:41564920
real audio (ram),文件头:2e7261fd 
real media (rm),文件头:2e524d46 
mpeg (mpg),文件头:000001ba 
mpeg (mpg),文件头:000001b3 
quicktime (mov),文件头:6d6f6f76 
windows media (asf),文件头:3026b2758e66cf11 
midi (mid),文件头:4d546864

java附件上传时后台验证上传文件的合法性

public static map<string, string=""> mfiletypes = new hashmap<string, string="">();
static {
    // imagesffd8ffe1
    mfiletypes.put("ffd8ffe1", ".jpg");
    mfiletypes.put("ffd8ffe0", ".jpg");
    mfiletypes.put("89504e47", ".png");
    mfiletypes.put("47494638", ".gif");
    mfiletypes.put("49492a00", ".tif");
    mfiletypes.put("424d", ".bmp");
    // 办公文档类
    mfiletypes.put("d0cf11e0", ".doc"); // ppt、doc、xls
    mfiletypes.put("504b0304", ".docx"); // pptx、docx、xlsx
    /** 注意由于文本文档录入内容过多,则读取文件头时较为多变-start **/
    mfiletypes.put("0d0a0d0a", ".txt"); // txt
    mfiletypes.put("0d0a2d2d", ".txt"); // txt
    mfiletypes.put("0d0ab4b4", ".txt"); // txt
    mfiletypes.put("b4b4bda8", ".txt"); // 文件头部为汉字
    mfiletypes.put("73646673", ".txt"); // txt,文件头部为英文字母
    mfiletypes.put("32323232", ".txt"); // txt,文件头部内容为数字
    mfiletypes.put("0d0a09b4", ".txt"); // txt,文件头部内容为数字
    mfiletypes.put("3132330d", ".txt"); // txt,文件头部内容为数字
    /** 注意由于文本文档录入内容过多,则读取文件头时较为多变-end **/
    mfiletypes.put("25504446", ".pdf");
    mfiletypes.put("255044462d312e", ".pdf");
    // 压缩包
    mfiletypes.put("52617221", ".rar");
    mfiletypes.put("1f8b08", ".gz");
}
/**
    * 判断上传的文件是否合法
    *
    * @param file
    *            文件
    * @param contenttype
    *            是否指定类型
    * @param typestr
    *            文件类型后缀名(.jpg,.png,.gif,.jpeg)
    * @return
    */
public boolean checkfileillegal(multipartfile file, string filename, string typestr) {
    if (!file.isempty()) {
        if (stringutils.isnotblank(file.getcontenttype())) {
            string type = null;
            try {
                type = getfiletype(file.getinputstream());
            } catch (ioexception e) {
            logger.error("checkfileillegal->getfiletype->error:" + e.getmessage());
            return false;
        }
        if (null != type && -1 != typestr.indexof(type)) {
            int index = filename.lastindexof(".");
            if (stringutils.isnotblank(filename) && -1 != index) {
                string filetype = filename.substring(index).tolowercase();
                if (-1 != typestr.indexof(filetype)) {
                    return true;
                    }
                }
            }
        }
    }
    return false;
}
/**
 * 根据文件的输入流获取文件头信息
 * @return 文件头信息
 */
public static string getfiletype(inputstream is) {
    byte[] b = new byte[4];
    if (is != null) {
        try {
            is.read(b, 0, b.length);
        } catch (ioexception e) {
            e.printstacktrace();
        }
    }
    return mfiletypes.get(getfileheader(b));
}

总结

以上所说就是本文关于java项目安全处理方法的全部内容,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网