淺談Springboot2.0防止XSS攻擊的幾種方式
在平時(shí)做項(xiàng)目代碼開(kāi)發(fā)的時(shí)候,很容易忽視XSS攻擊的防護(hù),網(wǎng)上有很多自定義全局?jǐn)r截器來(lái)實(shí)現(xiàn)XSS過(guò)濾,其實(shí)不需要這么麻煩,SpringBoot留有不少鉤子(擴(kuò)展點(diǎn)),據(jù)此我們可以巧妙地實(shí)現(xiàn)全局的XSS過(guò)濾
防止XSS攻擊,一般有兩種做法:
轉(zhuǎn)義
使用工具類HtmlUtils實(shí)現(xiàn)
過(guò)濾
將敏感標(biāo)簽去除
jsoup實(shí)現(xiàn)了非常強(qiáng)大的clean敏感標(biāo)簽的功能
轉(zhuǎn)義 做法的三種實(shí)現(xiàn):
轉(zhuǎn)義方法一:注冊(cè)自定義轉(zhuǎn)換器
自定義轉(zhuǎn)換器,集成PropertyEditorSupport類實(shí)現(xiàn),轉(zhuǎn)換器還可以實(shí)現(xiàn)數(shù)據(jù)格式轉(zhuǎn)換,例如:date的轉(zhuǎn)換;
@Component
public class DateEditor extends PropertyEditorSupport {
? ? Pattern pattern = Pattern.compile("[^0-9]");
? ? @Override
? ? public void setAsText(String text) throws IllegalArgumentException {
? ? ? ? if (StrUtil.isBlank(text)) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? text = text.trim();
? ? ? ? Matcher matcher = pattern.matcher(text);
? ? ? ? text = matcher.replaceAll("");
? ? ? ? int length = text.length();
? ? ? ? Date date;
? ? ? ? switch (length) {
? ? ? ? ? ? case 14:
? ? ? ? ? ? ? ? date = DateTime.parse(text, DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 12:
? ? ? ? ? ? ? ? date = DateTime.parse(text, DateTimeFormat.forPattern("yyyyMMddHHmm")).toDate();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 10:
? ? ? ? ? ? ? ? date = DateTime.parse(text, DateTimeFormat.forPattern("yyyyMMddHH")).toDate();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 8:
? ? ? ? ? ? ? ? date = DateTime.parse(text, DateTimeFormat.forPattern("yyyyMMdd")).toDate();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 6:
? ? ? ? ? ? ? ? date = DateTime.parse(text, DateTimeFormat.forPattern("yyyyMM")).toDate();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? date = DateTime.parse(text, DateTimeFormat.forPattern("yyyy")).toDate();
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? setValue(date);
? ? }
}@Component
public class StringEscapeEditor extends PropertyEditorSupport {
? ? public StringEscapeEditor() {
? ? ? ? super();
? ? }
? ? @Override
? ? public String getAsText() {
? ? ? ? Object value = getValue();
? ? ? ? return value != null ? value.toString() : "";
? ? }
? ? @Override
? ? public void setAsText(String text) {
? ? ? ? if (text == null) {
? ? ? ? ? ? setValue(null);
? ? ? ? } else {
? ? ? ? ? ? String value = text;
? ? ? ? ? ? value = value.trim();
? ? ? ? ? ? setValue(value);
? ? ? ? }
? ? }
}@Slf4j
@Component
public class CommentWebBindingInitializer extends ConfigurableWebBindingInitializer {
? ? private final StringEscapeEditor stringEscapeEditor;
? ? private final DateEditor dateEditor;
? ? @Autowired
? ? public CommentWebBindingInitializer(StringEscapeEditor stringEscapeEditor, DateEditor dateEditor) {
? ? ? ? this.stringEscapeEditor = stringEscapeEditor;
? ? ? ? this.dateEditor = dateEditor;
? ? }
? ? @Override
? ? public void initBinder(WebDataBinder binder) {
? ? ? ? log.info("init bind editor");
? ? ? ? super.initBinder(binder);
? ? ? ? // 注冊(cè)自定義的類型轉(zhuǎn)換器
? ? ? ? binder.registerCustomEditor(Date.class, dateEditor);
? ? ? ? binder.registerCustomEditor(String.class, stringEscapeEditor);
? ? }
}轉(zhuǎn)義方法二:BaseController
需要XSS防護(hù)的Controller的需要繼承該BaseController
public class BaseController {
?
? ? @Autowired
? ? private StringEscapeEditor stringEscapeEditor;
?
? ? @InitBinder
? ? public void initBinder(ServletRequestDataBinder binder) {
? ? ? ? binder.registerCustomEditor(String.class, stringEscapeEditor);
? ? }
}轉(zhuǎn)義方法三:Converter
@Component
public class StringEscapeEditor implements Converter<String, String> {
@Override
public String convert(String s) {
return StringUtils.isEmpty(s) ? s : HtmlUtils.htmlEscape(s);
}
}
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private LoginInterceptor loginInterceptor;
@Autowired
private StringEscapeEditor stringEscapeEditor;
/**
* 在參數(shù)綁定時(shí),自定義String->String的轉(zhuǎn)換器,
* 在轉(zhuǎn)換邏輯中對(duì)參數(shù)值進(jìn)行轉(zhuǎn)義,從而達(dá)到防XSS的效果
*
* @param registry
*/
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(StringEscapeEditor);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor)
.addPathPatterns("/**")
// 路徑不包括contextPath部分
.excludePathPatterns("/user/login", "/user/logout", "/index/test1");
}
/**
* 前后端分離需要解決跨域問(wèn)題
*
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
.allowCredentials(true).maxAge(3600);
}
}到此這篇關(guān)于淺談Springboot2.0防止XSS攻擊的幾種方式的文章就介紹到這了,更多相關(guān)Springboot防止XSS攻擊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)圖書管理系統(tǒng)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
java實(shí)現(xiàn)word文件轉(zhuǎn)html文件
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)word文件轉(zhuǎn)html文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
妙用Java8中的Function接口消滅if...else
在開(kāi)發(fā)過(guò)程中經(jīng)常會(huì)使用if...else...進(jìn)行判斷拋出異常、分支處理等操作。這些if...else...充斥在代碼中嚴(yán)重影響了代碼代碼的美觀,本文就妙用Java8中的Function接口消滅if...else,感興趣的可以了解一下2022-01-01
Spring事件監(jiān)聽(tīng)器ApplicationListener源碼詳解
這篇文章主要介紹了Spring事件監(jiān)聽(tīng)器ApplicationListener源碼詳解,ApplicationEvent以及Listener是Spring為我們提供的一個(gè)事件監(jiān)聽(tīng)、訂閱的實(shí)現(xiàn),內(nèi)部實(shí)現(xiàn)原理是觀察者設(shè)計(jì)模式,需要的朋友可以參考下2023-05-05
如何用idea編寫并運(yùn)行第一個(gè)spark scala處理程序
詳細(xì)介紹了如何使用IntelliJ IDEA創(chuàng)建Scala項(xiàng)目,包括配置JDK和Scala SDK,添加Maven支持,編輯pom.xml,并創(chuàng)建及運(yùn)行Scala程序,這為Scala初學(xué)者提供了一個(gè)基礎(chǔ)的項(xiàng)目搭建和運(yùn)行指南2024-09-09
JAVA初級(jí)項(xiàng)目——實(shí)現(xiàn)圖書管理系統(tǒng)
這篇文章主要介紹了JAVA如何實(shí)現(xiàn)圖書管理系統(tǒng),文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06

