Apabila melakukan pembangunan kod projek, adalah mudah untuk mengabaikan perlindungan serangan XSS Terdapat banyak pemintas global tersuai di Internet untuk melaksanakan penapisan XSS menyusahkan. SpringBoot mempunyai banyak Hook (titik sambungan), yang membolehkan kami melaksanakan penapisan XSS global
Escape<.> Gunakan kelas alat HtmlUtils untuk melaksanakan
penapisalih keluar teg sensitif
jsoup melaksanakan fungsi yang sangat berkuasa untuk membersihkan teg sensitif
@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); // 注册自定义的类型转换器 binder.registerCustomEditor(Date.class, dateEditor); binder.registerCustomEditor(String.class, stringEscapeEditor); } }
public class BaseController { @Autowired private StringEscapeEditor stringEscapeEditor; @InitBinder public void initBinder(ServletRequestDataBinder binder) { binder.registerCustomEditor(String.class, stringEscapeEditor); } }
@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; /** * 在参数绑定时,自定义String->String的转换器, * 在转换逻辑中对参数值进行转义,从而达到防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"); } /** * 前后端分离需要解决跨域问题 * * @param registry */ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH") .allowCredentials(true).maxAge(3600); } }
Atas ialah kandungan terperinci Apakah cara untuk menghalang serangan XSS dalam Springboot2.0. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!