当前位置:网站首页>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 .
边栏推荐
- Under win10 64 bit, matlab fails to configure notebook
- Meeting OA project (III) -- my meeting (meeting seating and submission for approval)
- Cause: could't make a guess for solution
- 【Halcon视觉】图像滤波
- 汉诺塔II|汉诺塔4柱
- INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决方式
- Replay the snake game with C language (II) end
- Common errors when starting projects in uniapp ---appid
- C language course design Tetris (Part 2)
- 网易云UI模仿--&gt;侧边栏
猜你喜欢
Data communication foundation - layer 2 switching principle
Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
万字详解“用知识图谱驱动企业业绩增长”
Okaleido ecological core equity Oka, all in fusion mining mode
数通基础-Telnet远程管理设备
Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install
Learning about opencv (3)
Cause: couldn‘t make a guess for 解决方法
【Halcon视觉】仿射变换
Beginner of flask framework-04-flask blueprint and code separation
随机推荐
Learning about opencv (4)
Study on the basis of opencv
Review of database -- 3. SQL language
About automatic operation on Web pages
Getting started with SQL - combined tables
Use of Android grendao database
简单化构造函数的继承方法(二)- ES6中的class继承
数通基础-二层交换原理
Android greendao数据库的使用
Basic usage of protobuf
Learning about tensor (III)
数通基础-网络基础知识
The charm of SQL optimization! From 30248s to 0.001s
Under win10 64 bit, matlab fails to configure notebook
Strange Towers of Hanoi|汉诺塔4柱问题
面试第二家公司的面试题及答案(二)
Session based recommendations with recurrent neural networks
Vectortilelayer replacement style
Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
Learning about opencv (1)