当前位置:网站首页>Prevent XSS attacks
Prevent XSS attacks
2022-07-26 10:18:00 【CS beat you】
principle : By filtering request Requested paramer To deal with .
1, To write xssFilter class ,
public class XssFilter implements Filter {
FilterConfig filterConfig = null;
private List<String> urlExclusion = null;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
public void destroy() {
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String servletPath = httpServletRequest.getServletPath();
if (urlExclusion != null && urlExclusion.contains(servletPath)) {
chain.doFilter(request, response);
} else {
chain.doFilter(new XssHttpServletRequestWrapper((HttpServletRequest) request), response);
}
}
public List<String> getUrlExclusion() {
return urlExclusion;
}
public void setUrlExclusion(List<String> urlExclusion) {
this.urlExclusion = urlExclusion;
}
2, adopt XssHttpServletRequestWrapper Filter url
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
public XssHttpServletRequestWrapper(HttpServletRequest servletRequest) {
super(servletRequest);
}
public String[] getParameterValues(String parameter) {
String[] values = super.getParameterValues(parameter);
if (values == null) {
return null;
}
int count = values.length;
String[] encodedValues = new String[count];
for (int i = 0; i < count; i++) {
encodedValues[i] = cleanXSS(values[i]);
}
return encodedValues;
}
public String getParameter(String parameter) {
String value = super.getParameter(parameter);
if (value == null) {
return null;
}
return cleanXSS(value);
}
public String getHeader(String name) {
String value = super.getHeader(name);
if (value == null)
return null;
return cleanXSS(value);
}
private String cleanXSS(String value) {
//You'll need to remove the spaces from the html entities below
value = value.replaceAll("<", "& lt;").replaceAll(">", "& gt;");
value = value.replaceAll("\\(", "& #40;").replaceAll("\\)", "& #41;");
value = value.replaceAll("'", "& #39;");
value = value.replaceAll("eval\\((.*)\\)", "");
value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
value = value.replaceAll("script", "");
return value;
}
}3, Inject spring in
/**
* xssFilter register
*/
@Bean
public FilterRegistrationBean xssFilterRegistration() {
XssFilter xssFilter = new XssFilter();
// Can it be added here xss Filter interface
//xssFilter.setUrlExclusion(Arrays.asList("/merchants/*"));
FilterRegistrationBean registration = new FilterRegistrationBean(xssFilter);
registration.addUrlPatterns("/*"); // there /* It refers to blocking all paths
return registration;
}notes : With reference guns Implementation scheme of open source framework . thank guns author .
边栏推荐
- The practice of OpenCV -- bank card number recognition
- 面试突击68:为什么 TCP 需要 3 次握手?
- MySQL function
- 数通基础-STP原理
- Using undertow, Nacos offline logout delay after service stop
- Employee information management system based on Web
- Formwork (III)
- Redis realizes distributed lock and gets a watchdog
- Tower of Hanoi II | tower of Hanoi 4 columns
- The problem of incomplete or partial display of the last recyclerview is solved
猜你喜欢

Draw arrows with openlayer

Flask framework beginner-03-template

Leetcode 504. Hex number

equals与==的区别

【Halcon视觉】编程逻辑

Mysql5.7.25 master-slave replication (one-way)

数通基础-STP原理

INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决方式

How to write a million reading article

Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install
随机推荐
SQL Server 2008 server engine failed to start?
Tableviewcell highly adaptive
【Halcon视觉】形态学腐蚀
【C#语言】LINQ概述
Uniapp error 7 < Map >: marker ID should be a number
数通基础-TCPIP参考模型
Learning about opencv (3)
【Halcon视觉】阈值分割
Rocky basic exercise -shell script 2
【Halcon视觉】算子的结构
面试第一家公司的面试题及答案(一)
Force deduction DFS
Study notes of the fifth week of sophomore year
Android greendao数据库的使用
MySQL 5.7.25 source code installation record
Self encapsulated database dbutils universal template
MySQL function
Set view dynamic picture
Study notes of the first week of sophomore year
Solution of inputting whole line string after inputting integer