Springboot3巧妙运用拦截器阻断xss攻击
Springboot3巧妙运用拦截器阻断xss攻击
- 什么是xss
- 跨站脚本攻击
- 类型
- 简单示例
- 解决方法
- 拦截器
- 代码
- 使用demo
什么是xss
人们经常将跨站脚本攻击(Cross Site Scripting)缩写为CSS,但这会与层叠样式表(Cascading Style Sheets,CSS)的缩写混淆。因此,有人将跨站脚本攻击缩写为XSS。
跨站脚本攻击
是最普遍的Web应用安全漏洞。这类漏洞能够使得攻击者嵌入恶意脚本代码到正常用户会访问到的页面中,当正常用户访问该页面时,则可导致嵌入的恶意脚本代码的执行,从而达到恶意攻击用户的目的。
类型
- 持久型跨站 : 危害最大,跨站脚本存储在服务器的数据持久层中—数据库;
- 非持久型跨站 : 攻击者伪造反射型跨站脚本连接,直接访问该连接返回跨站代码;
- DOM跨站 :通常是客户端处理文档对象模型出现的逻辑安全问题
简单示例
我自己做了一个存在xss漏洞的简单web网站
我需要的正常访问结果
意料之外的异常结果
发现我们可以在连接后面拼接js等脚本,这很有可能就给攻击者留了后门
解决方法
拦截器
每次请求我们先看看内容存不存在非法内容
代码
核心代码
package com.zxs.xss;import com.zxs.xss.filter.XssFilter;
import jakarta.servlet.DispatcherType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableConfigurationProperties({XssProperties.class})
public class XssAutoConfiguration {@AutowiredXssProperties xssProperties;@Bean@ConditionalOnProperty(prefix = XssProperties.XSS, name = "enable", havingValue = "true")public FilterRegistrationBean xssFilterRegistration() {FilterRegistrationBean registration = new FilterRegistrationBean();registration.setDispatcherTypes(DispatcherType.REQUEST);registration.setFilter(new XssFilter());registration.addUrlPatterns(xssProperties.getUrlPatterns());registration.setName(xssProperties.getName());registration.setOrder(xssProperties.getOrder());System.out.println("拦截器启动 :"+xssProperties.getName()+" 状态");return registration;}
}
package com.zxs.xss.filter;import com.zxs.xss.servlet.XssHttpServletRequestWrapper;
import com.zxs.xss.util.HtmlFilterKit;
import jakarta.servlet.*;import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;public class XssFilter implements Filter {@Overridepublic void init(FilterConfig config) throws ServletException {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {XssHttpServletRequestWrapper xssRequest = new XssHttpServletRequestWrapper((HttpServletRequest) request,new HtmlFilterKit());System.out.println("拦截到:"+((HttpServletRequest) request).getMethod());chain.doFilter(xssRequest, response);}@Overridepublic void destroy() {}
}
package com.zxs.xss.servlet;import com.zxs.xss.util.HtmlFilterKit;
import jakarta.servlet.ReadListener;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.LinkedHashMap;
import java.util.Map;public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {public static final String APPLICATION_JSON_VALUE = "application/json";public static final String CONTENT_TYPE = "Content-Type";/*** 没被包装过的HttpServletRequest(特殊场景,需要自己过滤)*/HttpServletRequest orgRequest;/*** html过滤*/private final HtmlFilterKit htmlFilter;public XssHttpServletRequestWrapper(HttpServletRequest request,HtmlFilterKit htmlFilter) {super(request);this.htmlFilter = htmlFilter;orgRequest = request;}@Overridepublic ServletInputStream getInputStream() throws IOException {//非json类型,直接返回if (!APPLICATION_JSON_VALUE.equalsIgnoreCase(super.getHeader(CONTENT_TYPE))) {return super.getInputStream();}//为空,直接返回String json = StreamUtils.copyToString(super.getInputStream(), Charset.forName("UTF-8"));if (StringUtils.isEmpty(json)) {return super.getInputStream();}//xss过滤json = xssEncode(json);final ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes("UTF-8"));return new ServletInputStream() {@Overridepublic boolean isFinished() {return true;}@Overridepublic boolean isReady() {return true;}@Overridepublic void setReadListener(ReadListener readListener) {}@Overridepublic int read() throws IOException {return bis.read();}};}@Overridepublic String getParameter(String name) {String value = super.getParameter(xssEncode(name));if (!StringUtils.isEmpty(value)) {value = xssEncode(value);}return value;}@Overridepublic String[] getParameterValues(String name) {String[] parameters = super.getParameterValues(name);if (parameters == null || parameters.length == 0) {return null;}for (int i = 0; i < parameters.length; i++) {parameters[i] = xssEncode(parameters[i]);}return parameters;}@Overridepublic Map<String, String[]> getParameterMap() {Map<String, String[]> map = new LinkedHashMap<>();Map<String, String[]> parameters = super.getParameterMap();for (String key : parameters.keySet()) {String[] values = parameters.get(key);for (int i = 0; i < values.length; i++) {values[i] = xssEncode(values[i]);}map.put(key, values);}return map;}@Overridepublic String getHeader(String name) {String value = super.getHeader(xssEncode(name));if (!StringUtils.isEmpty(value)) {value = xssEncode(value);}return value;}private String xssEncode(String input) {return htmlFilter.filter(input);}/*** 获取最原始的request*/public HttpServletRequest getOrgRequest() {return orgRequest;}/*** 获取最原始的request*/public static HttpServletRequest getOrgRequest(HttpServletRequest request) {if (request instanceof XssHttpServletRequestWrapper) {return ((XssHttpServletRequestWrapper) request).getOrgRequest();}return request;}
}
package com.zxs.xss.util;public interface CharacterFilter {String filter(String input);
}
package com.zxs.xss.util;import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public final class HtmlFilterKit implements CharacterFilter {/*** regex flag union representing /si modifiers in php**/private static final int REGEX_FLAGS_SI = Pattern.CASE_INSENSITIVE | Pattern.DOTALL;private static final Pattern P_COMMENTS = Pattern.compile("<!--(.*?)-->", Pattern.DOTALL);private static final Pattern P_COMMENT = Pattern.compile("^!--(.*)--$", REGEX_FLAGS_SI);private static final Pattern P_TAGS = Pattern.compile("<(.*?)>", Pattern.DOTALL);private static final Pattern P_END_TAG = Pattern.compile("^/([a-z0-9]+)", REGEX_FLAGS_SI);private static final Pattern P_START_TAG = Pattern.compile("^([a-z0-9]+)(.*?)(/?)$", REGEX_FLAGS_SI);private static final Pattern P_QUOTED_ATTRIBUTES = Pattern.compile("([a-z0-9]+)=([\"'])(.*?)\\2", REGEX_FLAGS_SI);private static final Pattern P_UNQUOTED_ATTRIBUTES = Pattern.compile("([a-z0-9]+)(=)([^\"\\s']+)", REGEX_FLAGS_SI);private static final Pattern P_PROTOCOL = Pattern.compile("^([^:]+):", REGEX_FLAGS_SI);private static final Pattern P_ENTITY = Pattern.compile("&#(\\d+);?");private static final Pattern P_ENTITY_UNICODE = Pattern.compile("&#x([0-9a-f]+);?");private static final Pattern P_ENCODE = Pattern.compile("%([0-9a-f]{2});?");private static final Pattern P_VALID_ENTITIES = Pattern.compile("&([^&;]*)(?=(;|&|$))");private static final Pattern P_VALID_QUOTES = Pattern.compile("(>|^)([^<]+?)(<|$)", Pattern.DOTALL);private static final Pattern P_END_ARROW = Pattern.compile("^>");private static final Pattern P_BODY_TO_END = Pattern.compile("<([^>]*?)(?=<|$)");private static final Pattern P_XML_CONTENT = Pattern.compile("(^|>)([^<]*?)(?=>)");private static final Pattern P_STRAY_LEFT_ARROW = Pattern.compile("<([^>]*?)(?=<|$)");private static final Pattern P_STRAY_RIGHT_ARROW = Pattern.compile("(^|>)([^<]*?)(?=>)");private static final Pattern P_AMP = Pattern.compile("&");private static final Pattern P_QUOTE = Pattern.compile("<");private static final Pattern P_LEFT_ARROW = Pattern.compile("<");private static final Pattern P_RIGHT_ARROW = Pattern.compile(">");private static final Pattern P_BOTH_ARROWS = Pattern.compile("<>");/*** @xxx could grow large... maybe use sesat's ReferenceMap*/private static final ConcurrentMap<String, Pattern> P_REMOVE_PAIR_BLANKS = new ConcurrentHashMap<String, Pattern>();private static final ConcurrentMap<String, Pattern> P_REMOVE_SELF_BLANKS = new ConcurrentHashMap<String, Pattern>();/*** set of allowed html elements, along with allowed attributes for each element**/private final Map<String, List<String>> vAllowed;/*** counts of open tags for each (allowable) html element**/private final Map<String, Integer> vTagCounts = new HashMap<String, Integer>();/*** html elements which must always be self-closing (e.g. "<img />")**/private final String[] vSelfClosingTags;/*** html elements which must always have separate opening and closing tags (e.g. "<b></b>")**/private final String[] vNeedClosingTags;/*** set of disallowed html elements**/private final String[] vDisallowed;/*** attributes which should be checked for valid protocols**/private final String[] vProtocolAtts;/*** allowed protocols**/private final String[] vAllowedProtocols;private final String[] vRemoveBlanks;/*** entities allowed within html markup**/private final String[] vAllowedEntities;/*** flag determining whether comments are allowed in input String.*/private final boolean stripComment;private final boolean encodeQuotes;private boolean vDebug = false;private final boolean alwaysMakeTags;/*** Default constructor.*/public HtmlFilterKit() {vAllowed = new HashMap<>();final ArrayList<String> a_atts = new ArrayList<String>();a_atts.add("href");a_atts.add("target");vAllowed.put("a", a_atts);final ArrayList<String> img_atts = new ArrayList<String>();img_atts.add("src");img_atts.add("width");img_atts.add("height");img_atts.add("alt");vAllowed.put("img", img_atts);final ArrayList<String> no_atts = new ArrayList<String>();vAllowed.put("b", no_atts);vAllowed.put("strong", no_atts);vAllowed.put("i", no_atts);vAllowed.put("em", no_atts);vSelfClosingTags = new String[]{"img"};vNeedClosingTags = new String[]{"a", "b", "strong", "i", "em"};vDisallowed = new String[]{};vAllowedProtocols = new String[]{"http", "mailto", "https"}; // no ftp.vProtocolAtts = new String[]{"src", "href"};vRemoveBlanks = new String[]{"a", "b", "strong", "i", "em"};vAllowedEntities = new String[]{"amp", "gt", "lt", "quot"};stripComment = true;encodeQuotes = true;alwaysMakeTags = true;}/*** Set debug flag to true. Otherwise use default settings. See the default constructor.** @param debug turn debug on with a true argument*/public HtmlFilterKit(final boolean debug) {this();vDebug = debug;}/*** Map-parameter configurable constructor.** @param conf map containing configuration. keys match field names.*/public HtmlFilterKit(final Map<String, Object> conf) {assert conf.containsKey("vAllowed") : "configuration requires vAllowed";assert conf.containsKey("vSelfClosingTags") : "configuration requires vSelfClosingTags";assert conf.containsKey("vNeedClosingTags") : "configuration requires vNeedClosingTags";assert conf.containsKey("vDisallowed") : "configuration requires vDisallowed";assert conf.containsKey("vAllowedProtocols") : "configuration requires vAllowedProtocols";assert conf.containsKey("vProtocolAtts") : "configuration requires vProtocolAtts";assert conf.containsKey("vRemoveBlanks") : "configuration requires vRemoveBlanks";assert conf.containsKey("vAllowedEntities") : "configuration requires vAllowedEntities";vAllowed = Collections.unmodifiableMap((HashMap<String, List<String>>) conf.get("vAllowed"));vSelfClosingTags = (String[]) conf.get("vSelfClosingTags");vNeedClosingTags = (String[]) conf.get("vNeedClosingTags");vDisallowed = (String[]) conf.get("vDisallowed");vAllowedProtocols = (String[]) conf.get("vAllowedProtocols");vProtocolAtts = (String[]) conf.get("vProtocolAtts");vRemoveBlanks = (String[]) conf.get("vRemoveBlanks");vAllowedEntities = (String[]) conf.get("vAllowedEntities");stripComment = conf.containsKey("stripComment") ? (Boolean) conf.get("stripComment") : true;encodeQuotes = conf.containsKey("encodeQuotes") ? (Boolean) conf.get("encodeQuotes") : true;alwaysMakeTags = conf.containsKey("alwaysMakeTags") ? (Boolean) conf.get("alwaysMakeTags") : true;}private void reset() {vTagCounts.clear();}private void debug(final String msg) {if (vDebug) {Logger.getAnonymousLogger().info(msg);}}//---------------------------------------------------------------// my versions of some PHP library functionspublic static String chr(final int decimal) {return String.valueOf((char) decimal);}public static String htmlSpecialChars(final String s) {String result = s;result = regexReplace(P_AMP, "&", result);result = regexReplace(P_QUOTE, """, result);result = regexReplace(P_LEFT_ARROW, "<", result);result = regexReplace(P_RIGHT_ARROW, ">", result);return result;}@Overridepublic String filter(final String input) {reset();String s = input;debug("************************************************");debug(" INPUT: " + input);s = escapeComments(s);debug(" escapeComments: " + s);s = balanceHTML(s);debug(" balanceHTML: " + s);s = checkTags(s);debug(" checkTags: " + s);s = processRemoveBlanks(s);debug("processRemoveBlanks: " + s);s = validateEntities(s);debug(" validateEntites: " + s);debug("************************************************\n\n");return s;}public boolean isAlwaysMakeTags() {return alwaysMakeTags;}public boolean isStripComments() {return stripComment;}private String escapeComments(final String s) {final Matcher m = P_COMMENTS.matcher(s);final StringBuffer buf = new StringBuffer();if (m.find()) {final String match = m.group(1); //(.*?)m.appendReplacement(buf, Matcher.quoteReplacement("<!--" + htmlSpecialChars(match) + "-->"));}m.appendTail(buf);return buf.toString();}private String balanceHTML(String s) {if (alwaysMakeTags) {//// try and form html//s = regexReplace(P_END_ARROW, "", s);s = regexReplace(P_BODY_TO_END, "<$1>", s);s = regexReplace(P_XML_CONTENT, "$1<$2", s);} else {//// escape stray brackets//s = regexReplace(P_STRAY_LEFT_ARROW, "<$1", s);s = regexReplace(P_STRAY_RIGHT_ARROW, "$1$2><", s);//// the last regexp causes '<>' entities to appear// (we need to do a lookahead assertion so that the last bracket can// be used in the next pass of the regexp)//s = regexReplace(P_BOTH_ARROWS, "", s);}return s;}private String checkTags(String s) {Matcher m = P_TAGS.matcher(s);final StringBuffer buf = new StringBuffer();while (m.find()) {String replaceStr = m.group(1);replaceStr = processTag(replaceStr);m.appendReplacement(buf, Matcher.quoteReplacement(replaceStr));}m.appendTail(buf);s = buf.toString();// these get tallied in processTag// (remember to reset before subsequent calls to filter method)for (String key : vTagCounts.keySet()) {for (int ii = 0; ii < vTagCounts.get(key); ii++) {s += "</" + key + ">";}}return s;}private String processRemoveBlanks(final String s) {String result = s;for (String tag : vRemoveBlanks) {if (!P_REMOVE_PAIR_BLANKS.containsKey(tag)) {P_REMOVE_PAIR_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?></" + tag + ">"));}result = regexReplace(P_REMOVE_PAIR_BLANKS.get(tag), "", result);if (!P_REMOVE_SELF_BLANKS.containsKey(tag)) {P_REMOVE_SELF_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?/>"));}result = regexReplace(P_REMOVE_SELF_BLANKS.get(tag), "", result);}return result;}private static String regexReplace(final Pattern regex_pattern, final String replacement, final String s) {Matcher m = regex_pattern.matcher(s);return m.replaceAll(replacement);}private String processTag(final String s) {// ending tagsMatcher m = P_END_TAG.matcher(s);if (m.find()) {final String name = m.group(1).toLowerCase();if (allowed(name)) {if (!inArray(name, vSelfClosingTags)) {if (vTagCounts.containsKey(name)) {vTagCounts.put(name, vTagCounts.get(name) - 1);return "</" + name + ">";}}}}// starting tagsm = P_START_TAG.matcher(s);if (m.find()) {final String name = m.group(1).toLowerCase();final String body = m.group(2);String ending = m.group(3);//debug( "in a starting tag, name='" + name + "'; body='" + body + "'; ending='" + ending + "'" );if (allowed(name)) {String params = "";final Matcher m2 = P_QUOTED_ATTRIBUTES.matcher(body);final Matcher m3 = P_UNQUOTED_ATTRIBUTES.matcher(body);final List<String> paramNames = new ArrayList<String>();final List<String> paramValues = new ArrayList<String>();while (m2.find()) {paramNames.add(m2.group(1)); //([a-z0-9]+)paramValues.add(m2.group(3)); //(.*?)}while (m3.find()) {paramNames.add(m3.group(1)); //([a-z0-9]+)paramValues.add(m3.group(3)); //([^\"\\s']+)}String paramName, paramValue;for (int ii = 0; ii < paramNames.size(); ii++) {paramName = paramNames.get(ii).toLowerCase();paramValue = paramValues.get(ii);// debug( "paramName='" + paramName + "'" );
// debug( "paramValue='" + paramValue + "'" );
// debug( "allowed? " + vAllowed.get( name ).contains( paramName ) );if (allowedAttribute(name, paramName)) {if (inArray(paramName, vProtocolAtts)) {paramValue = processParamProtocol(paramValue);}params += " " + paramName + "=\"" + paramValue + "\"";}}if (inArray(name, vSelfClosingTags)) {ending = " /";}if (inArray(name, vNeedClosingTags)) {ending = "";}if (ending == null || ending.length() < 1) {if (vTagCounts.containsKey(name)) {vTagCounts.put(name, vTagCounts.get(name) + 1);} else {vTagCounts.put(name, 1);}} else {ending = " /";}return "<" + name + params + ending + ">";} else {return "";}}// commentsm = P_COMMENT.matcher(s);if (!stripComment && m.find()) {return "<" + m.group() + ">";}return "";}private String processParamProtocol(String s) {s = decodeEntities(s);final Matcher m = P_PROTOCOL.matcher(s);if (m.find()) {final String protocol = m.group(1);if (!inArray(protocol, vAllowedProtocols)) {// bad protocol, turn into local anchor link insteads = "#" + s.substring(protocol.length() + 1, s.length());if (s.startsWith("#//")) {s = "#" + s.substring(3, s.length());}}}return s;}private String decodeEntities(String s) {StringBuffer buf = new StringBuffer();Matcher m = P_ENTITY.matcher(s);while (m.find()) {final String match = m.group(1);final int decimal = Integer.decode(match).intValue();m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));}m.appendTail(buf);s = buf.toString();buf = new StringBuffer();m = P_ENTITY_UNICODE.matcher(s);while (m.find()) {final String match = m.group(1);final int decimal = Integer.valueOf(match, 16).intValue();m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));}m.appendTail(buf);s = buf.toString();buf = new StringBuffer();m = P_ENCODE.matcher(s);while (m.find()) {final String match = m.group(1);final int decimal = Integer.valueOf(match, 16).intValue();m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));}m.appendTail(buf);s = buf.toString();s = validateEntities(s);return s;}private String validateEntities(final String s) {StringBuffer buf = new StringBuffer();// validate entities throughout the stringMatcher m = P_VALID_ENTITIES.matcher(s);while (m.find()) {final String one = m.group(1); //([^&;]*)final String two = m.group(2); //(?=(;|&|$))m.appendReplacement(buf, Matcher.quoteReplacement(checkEntity(one, two)));}m.appendTail(buf);return encodeQuotes(buf.toString());}private String encodeQuotes(final String s) {if (encodeQuotes) {StringBuffer buf = new StringBuffer();Matcher m = P_VALID_QUOTES.matcher(s);while (m.find()) {final String one = m.group(1); //(>|^)final String two = m.group(2); //([^<]+?)final String three = m.group(3); //(<|$)m.appendReplacement(buf, Matcher.quoteReplacement(one + regexReplace(P_QUOTE, """, two) + three));}m.appendTail(buf);return buf.toString();} else {return s;}}private String checkEntity(final String preamble, final String term) {return ";".equals(term) && isValidEntity(preamble)? '&' + preamble: "&" + preamble;}private boolean isValidEntity(final String entity) {return inArray(entity, vAllowedEntities);}private static boolean inArray(final String s, final String[] array) {for (String item : array) {if (item != null && item.equals(s)) {return true;}}return false;}private boolean allowed(final String name) {return (vAllowed.isEmpty() || vAllowed.containsKey(name)) && !inArray(name, vDisallowed);}private boolean allowedAttribute(final String name, final String paramName) {return allowed(name) && (vAllowed.isEmpty() || vAllowed.get(name).contains(paramName));}
}
package com.zxs.xss;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = XssProperties.XSS)
public class XssProperties {public static final String XSS = "xss";/*** xss 是否生效*/boolean enable = false;/*** xss过滤器的名字*/String name = "xssFilter";/*** xss过滤器需要匹配的路径*/String[] urlPatterns = {"/*"};/*** 过滤器的优先级,值越小优先级越高*/int order = 0;public String getName() {return name;}public void setName(String name) {this.name = name;}public String[] getUrlPatterns() {return urlPatterns;}public void setUrlPatterns(String[] urlPatterns) {this.urlPatterns = urlPatterns;}public int getOrder() {return order;}public void setOrder(int order) {this.order = order;}public boolean isEnable() {return enable;}public void setEnable(boolean enable) {this.enable = enable;}
}
package com.zxs.xss.exception;public class XSSException extends RuntimeException {private String msg;private int code = 500;public XSSException(String msg) {super(msg);this.msg = msg;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}
}
com.zxs.xss.XssAutoConfiguration
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zxs</groupId><artifactId>zxs-xss-starter</artifactId><version>0.0.1</version><packaging>jar</packaging><name>zxs-xss-starter</name><description>为SpringbootWeb应用打造的Xss防护工具</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.0</version><relativePath/></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>21</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>jakarta.servlet</groupId><artifactId>jakarta.servlet-api</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><optional>true</optional><scope>test</scope></dependency></dependencies>
</project>
使用demo
将上面的插件包打包引入到我们的demo包里面
<dependency><groupId>com.bizihang</groupId><artifactId>zxs-xss-starter</artifactId><version>0.0.1</version></dependency>
# 开启xss防护
xss.enable=true
# 设置xss防护的url拦截路径
xss.url-patterns=/zxs/noxss-json
# 设置xss防护过滤器的优先级,值越小优先级越高
xss.order=0
package com.example.demo.controller;import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.tags.Param;@RequestMapping("/zxs")
@RestController
public class XSSTestController {@GetMapping("/xss-string")public Object xssFormTest(String value) {System.out.println(value);return value;}@PostMapping("/noxss-json")public Object xssJsonTest(@RequestBody Param param ) {return param;}
}
启动之后就可以了
通过例子可以看到我这边实现了一个存在xss的api与一个被拦截器保护的api
可以看到被保护的api是会过滤掉我们代码里面定义的非法字符
没被保护的api还是正常的出现xss
然后我们把
xss.url-patterns=/* 全保护起来,再次进入之前存在xss的api
发现已经被拦截了
相关文章:
Springboot3巧妙运用拦截器阻断xss攻击
Springboot3巧妙运用拦截器阻断xss攻击 什么是xss跨站脚本攻击类型简单示例解决方法拦截器代码使用demo 什么是xss 人们经常将跨站脚本攻击(Cross Site Scripting)缩写为CSS,但这会与层叠样式表(Cascading Style Sheetsÿ…...
leetcode 2551. 将珠子放入背包中
题目如下 数据范围 示例 题目的意思是将一个长度为n的数组weight(简称w)分成k个小数组,同时计算这些小数组的边界和。 设i (0 < i < n - 1) 首先我们假设已经找到最大分数序列即划分的小数组最优 令 j1,j2,j3.....jk为这些小数组的左端点(不包括第一个小数组)。 则有…...
【Spring】SpringBoot整合ShardingSphere并实现多线程分批插入10000条数据(进行分库分表操作)。
??个人主页:哈__ 期待您的关注 目录 一、ShardingSphere简介 ?1.Sharding-JDBC 2.Sharding-Proxy? 3.Sharding-Sidecar(TBD)? 二、为什么用到ShardingSphere? 三、数据分片 四、SpringBoot整合ShardingSphere 1.创建我们的数据…...
Python中的ast.literal_eval:安全地解析字符串为Python对象
Python中的ast.literal_eval:安全地解析字符串为Python对象 什么是ast.literal_eval?为什么说它是“安全”的? 如何使用ast.literal_eval?示例1:将字符串转换为列表示例2:将字符串转换为字典示例3ÿ…...
前端用json-server来Mock后端返回的数据处理
<html><body><div class"login-container"><h2>登录</h2><div class"login-form"><div class"form-group"><input type"text" id"username" placeholder"请输入用户名&q…...
【linux】文件与目录命令 - mv
文章目录 1. 基本用法2. 常用参数3. 用法举例4. 注意事项 mv 命令用于移动或重命名文件和目录,是 Linux 系统中管理文件的重要工具之一。它既能移动文件到指定路径,也能重命名文件或目录。 1. 基本用法 语法: mv [选项] 源文件 目标文件 mv…...
OSPF - LSA对照表
LSA的三要素,如何唯一表示一条LSA Type:表示是几类的LSA Link-id:这个比较特殊,不同的LSA的Link-ID不同 Advertising router:谁产生的LSA 常用的就是1、2、3、4、5、7型LSA 点击蓝字跳转LSA详细介绍(持续更新中…...
Mongodb基础sqL
------------------------------------------数据库------------------------------ (2).查看所有数据库 show dbs (3).选择数据库,如果不存在则隐式创建这个数据库 use 数据库名 ------------------------------------------集合------------------------------ …...
uniapp开发u-icon图标不显示问题
uniapp开发图标用u-icon不显示,换成uv-icon就可以了 插件市场从这里下载:uv-ui 破釜沉舟之兼容vue32、app、h5、小程序等多端,灵活导入,利剑出击 - DCloud 插件市场 组件库看这个:介绍 | 我的资料管理-uv-ui 是全面兼…...
宁德时代2025年Verify入职测评语言理解及数字推理真题SHL题库汇总、考情分析
宁德时代社招Verify入职测评对薪酬有着重要影响,其规定正确率达到80%才能顺利通过测评。这体现了公司对人才专业素养与能力的严格要求,旨在筛选出真正符合岗位需求的优秀人才。测评内容涵盖了专业知识、技能运用、逻辑思维等多方面,只有综合能…...
Spring Data Elasticsearch简介
一、Spring Data Elasticsearch简介 1 SpringData ElasticSearch简介 Elasticsearch是一个实时的分布式搜索和分析引擎。它底层封装了Lucene框架,可以提供分布式多用户的全文搜索服务。 Spring Data ElasticSearch是SpringData技术对ElasticSearch原生API封装之后的产物,它通…...
即插即用,无缝集成各种模型,港科大蚂蚁等发布Edicho:图像编辑一致性最新成果!
文章链接:https://arxiv.org/pdf/2412.21079 项目链接:https://ezioby.github.io/edicho/ 亮点直击 显式对应性引导一致性编辑:通过将显式图像对应性融入扩散模型的去噪过程,改进自注意力机制与分类器自由引导(CFG&…...
鸿蒙开发(29)弹性布局 (Flex)
概述 弹性布局(Flex)提供更加有效的方式对容器中的子元素进行排列、对齐和分配剩余空间。常用于页面头部导航栏的均匀分布、页面框架的搭建、多行数据的排列等。 容器默认存在主轴与交叉轴,子元素默认沿主轴排列,子元素在主轴方…...
华为 Sensor 省电策略调研
华为EMUI 9.0.0.187(C00E57R1P15) 无该功能 华为EMUI 9.1.0.321(C00E320R1P1) 之后有sensor管控 一、华为 Sensor 省电策略 1. Sensor 类别只配置非唤醒类Sensor 2. 手机静止情况,应用不可见时达到1分钟࿰…...
Kotlin语言的网络编程
Kotlin语言的网络编程 Kotlin作为一种现代的编程语言,其简洁、安全和高效的特性使得在开发各种应用时得到广泛认可。尤其是在网络编程方面,Kotlin凭借其与Java的高度兼容性以及丰富的库支持,使得网络操作变得更加简单易用。本文将详细探讨Ko…...
redis:安装部署、升级以及失败回退
安装部署 一、准备工作 1. 检查系统要求 确保你的服务器满足 Redis 的基本要求: 操作系统:支持的 Linux 发行版(如 Ubuntu, CentOS)内存:至少 4GB(根据实际应用需求调整)CPU:单核或多核 CPU磁盘空间:足够的磁盘空间用于数据存储和日志记录2. 更新系统软件包 在开始…...
3. ML机器学习
1.人工智能与机器学习的关系 机器学习是人工智能的一个重要分支,是人工智能的一个子集。它无需显式编程,而是通过数据和算法使机器能够自动学习和改进,从而实现智能行为。机器学习依赖于算法来识别数据中的模式,并通过这些模式做出…...
在高德地图上加载3DTilesLayer图层模型/天地瓦片
1. 引入必要的库 Three.js:一个用于创建和显示3D图形的JavaScript库。vuemap/three-layer:一个Vue插件,它允许你在高德地图中添加Three.js图层。vuemap/layer-3dtiles:一个用于处理3D Tiles格式数据的Vue插件,可以用来…...
用户使用LLM模型都在干什么?
Anthropic 对用户与 Claude 3.5 Sonnet 的大量匿名对话展开分析,主要发现及相关情况如下: 使用用途分布 软件开发主导:在各类使用场景中,软件开发占比最高,其中编码占 Claude 对话的 15% - 25%,网页和移动应…...
2 抽象工厂(Abstract Factory)模式
抽象工厂模式 1.1 分类 (对象)创建型 1.2 提出问题 家具店里有沙发、椅子、茶几等产品。产品有不同风格,如现代、北欧、工业。希望确保客户收到的产品风格统一,并可以方便地添加新产品和风格。 1.3 解决方案 提供一个创建一…...
数据结构-串
串的实现 在C语言中所使用的字符串就是串的数据类型的一种。 串的存储结构 定长顺序存储表示 类似于线性表的顺序存储结构,用一组连续的存储单元存储串值的字符序列。 #define MAXLEN 255 //预定义最大串长为255 typedef struct SString {char ch[MAXLEN]; …...
C#,图论与图算法,有向图(Direct Graph)广度优先遍历(BFS,Breadth First Search)算法与源程序
1 图的广度优先遍历 图的广度优先遍历(或搜索)类似于树的广度优先遍历(参见本文的方法2)。这里唯一需要注意的是,与树不同,图可能包含循环,因此我们可能再次来到同一个节点。为了避免多次处理节…...
使用ElasticSearch查询
从一个query body开始 {"query": {"bool": {"disable_coord": true,"must": [{"match": {"enabled": "1"}},{"range": {"effectTime": {"lt": "2017-06-13 13:33:…...
PyCharm+RobotFramework框架实现UDS自动化测试——(一)python-can 库的安装与环境配置
从0开始学习CANoe使用 从0开始学习车载测试 相信时间的力量 星光不负赶路者,时光不负有心人。 文章目录 1. 概述2.安装 python-can 库—基于pycharm在对应的工程下3. 在任意盘中安装环境4. 导入 can 模块语法5. 配置 CAN 接口6.CANoe设备连接语法 1. 概述 本专栏主…...
C# 值类型和引用类型详解
简介 在 C# 中,值类型和引用类型是两个基础的数据类型类别,它们的主要区别在于 存储位置 和 赋值方式。 值类型 值类型存储的是数据本身,分配在 栈 (Stack) 中。当一个值类型变量被赋值给另一个变量时,会复制值。 值类型的特点…...
计算机网络 —— 网络编程(TCP)
计算机网络 —— 网络编程(TCP) TCP和UDP的区别TCP (Transmission Control Protocol)UDP (User Datagram Protocol) 前期准备listen (服务端)函数原型返回值使用示例注意事项 accpect (服务端)函数原型返回…...
[Unity Shader] Shader基础光照3:环境光与自发光
在Unity中,光照是场景渲染的关键组成部分。正确使用环境光和自发光能够大大提高场景的真实感和视觉效果。本篇文章将详细介绍Unity中的环境光和自发光的基本概念,以及如何在编辑器和Shader中进行操作和实现。 1. 环境光(Ambient Light) 1.1 环境光的定义 环境光是场景中…...
云原生安全风险分析
一、什么是云原生安全 云原生安全包含两层含义: 面向云原生环境的安全具有云原生特征的安全 0x1:面向云原生环境的安全 面向云原生环境的安全的目标是防护云原生环境中基础设施、编排系统和微服务等系统的安全。 这类安全机制不一定具备云原生的特性…...
Redis 安装与配置指南
Redis 安装与配置指南 目录 安装说明 Linux 安装 Redis 3.0 压缩包上传服务器编译和安装修改配置启动 Redis关闭 Redis 卸载 RedisRedis 集群配置 Master 主库配置启动 Master 节点的 Redis 和 Sentinel客户登录验证Slave 从库配置查看集群数据验证 安装说明 Linux 安装 R…...
C语言Day13(c程序设计小红书+pta)
目录 (一)用函数调用实现,把最小的数字放在最前面,把最大的放在最后边 (二)使数字向后移m位 (三)用户自定义数据类型: (四)候选人计票数 &am…...
C++二十三种设计模式之迭代器模式
C二十三种设计模式之迭代器模式 一、组成二、特点三、目的四、缺点五、示例代码 一、组成 抽象聚合类:存储集合元素,声明管理集合元素接口。 具体聚合类:实现管理集合元素接口。 抽象迭代器类:声明访问和遍历聚合类元素的接口。 …...
【AI游戏】使用强化学习玩 Flappy Bird:从零实现 Q-Learning 算法(附完整资源)
1. 引言 Flappy Bird 是一款经典的休闲游戏,玩家需要控制小鸟穿过管道,避免碰撞。虽然游戏规则简单,但实现一个 AI 来自动玩 Flappy Bird 却是一个有趣的挑战。本文将介绍如何使用 Q-Learning 强化学习算法来训练一个 AI,使其能够…...
VSCode 中的 launch.json 配置使用
VSCode 中的 launch.json 配置使用 在 VSCode 中,launch.json 文件用于配置调试设置,特别是用来定义如何启动和调试你的应用。它允许你配置不同的调试模式、运行参数和调试选项。 基本结构 launch.json 文件位于 .vscode 文件夹内,可以通过…...
深度学习算法:开启智能时代的钥匙
引言 深度学习作为机器学习的一个分支,近年来在图像识别、自然语言处理、语音识别等多个领域取得了革命性的进展。它的核心在于构建多层的神经网络,通过模仿人脑处理信息的方式,让机器能够从数据中学习复杂的模式。 深度学习算法的基本原理…...
Clojure语言的并发编程
Clojure语言的并发编程 引言 在现代软件开发中,并发编程成为了处理多个任务、提高应用效率和响应速度的重要手段。尤其是在多核处理器逐渐成为主流的今天,如何高效利用这些计算资源是每个开发者面临的挑战。Clojure作为一种函数式编程语言,…...
MySQL学习记录1【DQL和DCL】
SQL学习记录 该笔记从DQL处开始记录 DQL之前值得注意的点 字段 BETWEEN min AND max 可以查询区间[min, max]的数值如果同一个字段需要满足多个OR条件,可以采取 字段 IN(数值1, 数值2, 数值3....)LIKE语句 字段 LIKE ___%%% 表示模糊匹配,_匹配一个字段…...
EasyExcel的应用
一、简单使用 引入依赖: 这里我们可以使用最新的4.0.2版本,也可以选择之前的稳定版本,3.1.x以后的版本API大致相同,新的版本也会向前兼容(3.1.x之前的版本,部分API可能在高版本被废弃)&…...
JS控制对应数据隐藏
首先需要获得到所有的input框,并声明一个空对象来存放,遍历所有的复选框,将他们中选中的放入对象,并设置键值为true,然后执行checkFalseValues(result)函数 function hideItem() {let checkboxes $(.setting_box inp…...
【剑指Offer刷题系列】数据流中的中位数
目录 问题描述示例示例 1: 思路解析方法一:使用两个堆(最大堆和最小堆)核心思路详细步骤示例分析优势适用场景 代码实现Python 实现(方法一:使用两个堆) 测试代码复杂度分析方法一:使…...
RabbitMQ高级篇之MQ可靠性 数据持久化
文章目录 消息丢失的原因分析内存存储的缺陷如何确保 RabbitMQ 的消息可靠性?数据持久化的三个方面持久化对性能的影响持久化实验验证性能对比Spring AMQP 默认持久化总结 消息丢失的原因分析 RabbitMQ 默认使用内存存储消息,但这种方式带来了两个主要问…...
C 语言奇幻之旅 - 第16篇:C 语言项目实战
目录 引言1. 项目规划1.1 需求分析与设计1.1.1 项目目标1.1.2 功能需求1.1.3 技术实现方案 2. 代码实现2.1 模块化编程2.1.1 学生信息模块2.1.2 成绩管理模块 2.2 调试与测试2.2.1 调试2.2.2 测试2.2.4 测试结果 3. 项目总结3.1 代码优化与重构3.1.1 代码优化3.1.2 代码重构 3.…...
[笔记] 使用 Jenkins 实现 CI/CD :从 GitLab 拉取 Java 项目并部署至 Windows Server
随着软件开发节奏的加快,持续集成(CI)和持续部署(CD)已经成为确保软件质量和加速产品发布的不可或缺的部分。Jenkins作为一款广泛使用的开源自动化服务器,为开发者提供了一个强大的平台来实施这些实践。然而…...
Git最便捷的迁移方式
#当公司要求git需要迁移时,你是不是感觉到束手无策。今天带来给大家最快,最便捷的迁移方式 这个命令是用于重命名git仓库中的远程仓库名。在这个命令中,我们将远程仓库的名字从"origin"改为"old-origin"。 git remote …...
【颜色分类--荷兰国旗问题】
问题 给定一个包含红色、白色和蓝色、共 n 个元素的数组 nums , 原地 对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。必须在不使用库内置的 sort 函数的情况下…...
xrdp连接闪退情况之一
错误核查 首先使用命令vim ~/.xsession-errors,当里面的报错信息为WARNING **: Could not make bus activated clients aware of XDG_CURRENT_DESKTOPGNOME environment variable:Failed to execute child process “dbus-launch” (No such file or directory)&am…...
KubeVirt 进阶:设置超卖比、CPU/MEM 升降配、在线磁盘扩容
前两篇文章,我们分别介绍 Kubevirt 的安装、基本使用 以及 将 oVirt 虚拟机迁移到 KubeVirt,我们留了两个ToDo,一个是本地磁盘的动态分配,一个是固定 IP 的需求,本期我们先解决第一个,本地磁盘的动态分配。…...
(回溯法)leetcode39组合总和
第一个2开头,下面的子节点的集合元素均为2,5,3 但是在5开头,下面的子节点集合元素均为5,3 带着这个图的思路确定i和index的传递值 backtracking(i, nums,8,sum);用的是i而不是i1 // ConsoleApplication3.cpp : 此文件包含 "main" 函数。程序…...
【数据结构】二叉搜索树
目录 1. 二叉搜索树的概念 2. 二叉搜索树的性能分析 3.二叉搜索树的实现 3. 1.二叉搜索树的插入 3.2. 二叉搜索树的查找 3.3. 二叉搜索树的删除 3.4. 二叉搜索树的实现代码 4. 二叉搜索树key和key/value两种使用场景 4.1 key搜索场景: 4.2 key/value搜索场…...
高可用虚拟IP-keepalived
个人觉得华为云这个文档十分详细:使用虚拟IP和Keepalived搭建高可用Web集群_弹性云服务器 ECS_华为云 应用场景:虚拟IP技术。虚拟IP,就是一个未分配给真实主机的IP,也就是说对外提供数据库服务器的主机除了有一个真实IP外还有一个…...
CSS语言的多线程编程
CSS语言的多线程编程 引言 在现代Web开发中,CSS(层叠样式表)被广泛用于给网页添加样式。然而,CSS本身是一种声明性语言,在设计上并没有直接支持多线程编程的功能。实际上,CSS的解析和应用是由浏览器的渲染…...