freemarker默认escape html 防范xss
freemarker 有html escape 方法,但是框架没有地方可以配置默认escape 1.<#escape>指令 2.<xxx?html>内建函数 方法一、 网上比较多的是通过TemplateLoader,给加载的template文件2头套 <#escape> <#escape x as x?html> your template code </#escape> 参考: http://techdiary.peterbecker.de/2009/02/defending-against-xss-attacks-in.html 但是现在我们应用的对freemarker做了扩展,一个页面分3个部分,一个layout、一个view、多个control。 多次render才到最终结果。要控制比较麻烦配置,也不友好。 方法二 改源码的$变量、默认全部转义、对固定的扩展的layout、一个view、多个control,配置正则原义输出。 变量是string类型的时候,用了xxx?string作为原义输出的内建函数。 缺点:比较暴力,修改了DollarViable源码,后续freemarker有升级要跟随修改 /** * The original code * env.getOut().write(escapedExpression.getStringValue(env)); */