日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

xss img onerror java_java后台防止XSS的脚本攻击

發(fā)布時(shí)間:2025/3/11 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 xss img onerror java_java后台防止XSS的脚本攻击 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

import java.util.regex.Pattern;

//具體過濾關(guān)鍵字符

public class XSSUtil {

private static Pattern[] patterns = new Pattern[]{

// Script fragments

Pattern.compile("", Pattern.CASE_INSENSITIVE),

// src='...'

Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL),

Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL),

// lonely script tags

Pattern.compile("", Pattern.CASE_INSENSITIVE),

Pattern.compile("

// eval(...)

Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL),

// expression(...)

Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL),

// javascript:...

Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE),

// vbscript:...

Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE),

// onload(...)=...

Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL),

//現(xiàn)場(chǎng)安全測(cè)試增加校驗(yàn)

Pattern.compile("alert(.*?)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL),

Pattern.compile("<", Pattern.MULTILINE | Pattern.DOTALL),

Pattern.compile(">", Pattern.MULTILINE | Pattern.DOTALL)

};

public static String stripXSS(String value){

if (value != null) {

// TODO ESAPI library

// NOTE: It's highly recommended to use the ESAPI library and uncomment the following line to

// avoid encoded attacks.

// value = ESAPI.encoder().canonicalize(value);

// Avoid null characters

value = value.replaceAll("\0", "");

// Remove all sections that match a pattern

for (Pattern scriptPattern : patterns){

value = scriptPattern.matcher(value).replaceAll("");

}

}

return value;

}

public static void main(String[] args) {

System.out.println("11"+ XSSUtil.stripXSS(""));

// System.out.println(XSSUtil.stripXSS(""));

}

}

import com.ideatech.common.util.XSSUtil;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.ControllerAdvice;

import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;

//每一個(gè)請(qǐng)求進(jìn)入控制層之前會(huì)先進(jìn)行字符過濾

@ControllerAdvice

@Component

@Slf4j

public class GlobalBindingInitializer {

@InitBinder

protected void initBinder(WebDataBinder binder) {

// String類型轉(zhuǎn)換,將所有傳遞進(jìn)來的String進(jìn)行HTML編碼,防止XSS攻擊

binder.registerCustomEditor(String.class, new PropertyEditorSupport() {

@Override

public void setAsText(String text) {

if(text != null){

String cleanText = XSSUtil.stripXSS(text);

if(!cleanText.equals(text)){

log.info("xss clean, before[{}], after[{}]",text,cleanText);

text = cleanText;

}

}

setValue(text);

}

@Override

public String getAsText() {

Object value = getValue();

return value != null ? value.toString() : "";

}

});

}

}

總結(jié)

以上是生活随笔為你收集整理的xss img onerror java_java后台防止XSS的脚本攻击的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。