velocity 的 escape实现
生活随笔
收集整理的這篇文章主要介紹了
velocity 的 escape实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
EscapeHtmlReference的escape方法調(diào)用以下方法實(shí)現(xiàn): StringEscapeUtils.escapeHtml(param); 再調(diào)用 org.apache.commons.lang.Entities.HTML40.escape(writer, string);
代碼如下: public void escape(Writer writer, String str) throws IOException {
int len = str.length();
for(int i = 0; i < len; ++i) {
char c = str.charAt(i);
String entityName = this.entityName(c);
if(entityName == null) {
if(c > 127) {
writer.write("&#");
writer.write(Integer.toString(c, 10));
writer.write(59); //就是個(gè)分號(hào)
} else {
writer.write(c);
}
} else {
writer.write(38);
writer.write(entityName);
writer.write(59);
}
}
}
我們也可以自己調(diào)用 StringEscapeUtils.escapeHtml(param);
比如: String param = request.getParameter("p");
String x = StringEscapeUtils.escapeHtml(param);
System.out.println(x);
輸入 <script>alert(/xxx/)</script> 輸出 <script>alert(/xxx/)</script>
velocity 也可以自定義 EventHandler 處理xss,配置EscapeHtmlReference 替換成自己的EventHandler <bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/pages/"/>
<property name="velocityProperties">
<props>
<prop key="input.encoding">utf-8</prop>
<prop key="output.encoding">utf-8</prop>
<prop key="eventhandler.referenceinsertion.class">org.apache.velocity.app.event.implement.EscapeHtmlReference</prop>
<prop key="eventhandler.escape.html.match">/^(?!\$\!?unesc_).*/</prop>
</props>
</property>
</bean>
代碼如下: public void escape(Writer writer, String str) throws IOException {
int len = str.length();
for(int i = 0; i < len; ++i) {
char c = str.charAt(i);
String entityName = this.entityName(c);
if(entityName == null) {
if(c > 127) {
writer.write("&#");
writer.write(Integer.toString(c, 10));
writer.write(59); //就是個(gè)分號(hào)
} else {
writer.write(c);
}
} else {
writer.write(38);
writer.write(entityName);
writer.write(59);
}
}
}
我們也可以自己調(diào)用 StringEscapeUtils.escapeHtml(param);
比如: String param = request.getParameter("p");
String x = StringEscapeUtils.escapeHtml(param);
System.out.println(x);
輸入 <script>alert(/xxx/)</script> 輸出 <script>alert(/xxx/)</script>
velocity 也可以自定義 EventHandler 處理xss,配置EscapeHtmlReference 替換成自己的EventHandler <bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/pages/"/>
<property name="velocityProperties">
<props>
<prop key="input.encoding">utf-8</prop>
<prop key="output.encoding">utf-8</prop>
<prop key="eventhandler.referenceinsertion.class">org.apache.velocity.app.event.implement.EscapeHtmlReference</prop>
<prop key="eventhandler.escape.html.match">/^(?!\$\!?unesc_).*/</prop>
</props>
</property>
</bean>
轉(zhuǎn)載于:https://www.cnblogs.com/SEC-fsq/p/7249579.html
總結(jié)
以上是生活随笔為你收集整理的velocity 的 escape实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python自动化开发课堂笔记【Day1
- 下一篇: 对JVM的理解