使用字符流转义Java库

使用字符流转义Java库,java,security,xss,Java,Security,Xss,是否有针对Java的HTML/JavaScript转义库具有字符流接口?是。为HTML、CSS、JavaScript、JavaScript正则表达式内容、JSON和XML提供转义符,用于将转义文本附加到可追加的。JavaWriters实现appendeable,就像StringBuilder和StringBuffer一样 它还允许以一种模式转义JavaScript,该模式允许它嵌入HTML元素或XML CDATA部分,而无需通过编码尖括号进一步转义 /** * Given a plain te

是否有针对Java的HTML/JavaScript转义库具有字符流接口?

是。为HTML、CSS、JavaScript、JavaScript正则表达式内容、JSON和XML提供转义符,用于将转义文本附加到可追加的。Java
Writer
s实现
appendeable
,就像
StringBuilder
StringBuffer
一样

它还允许以一种模式转义JavaScript,该模式允许它嵌入HTML
元素或XML CDATA部分,而无需通过编码尖括号进一步转义

/**
 * Given a plain text string writes an unquoted javascript string literal.
 *
 * @param s the plain text string to escape.
 * @param asciiOnly Makes sure that only ASCII characters are written to out.
 *     This is a good idea if you don't have control over the charset that
 *     the javascript will be served with.
 * @param embeddable True to make sure that nothing is written to out that
 *     could interfere with embedding inside a script tag or CDATA section, or
 *     other tag that typically contains markup.
 *     This does not make it safe to embed in an HTML attribute without
 *     further escaping.
 * @param out written to.
 */
public static void escapeJsString(
    CharSequence s, boolean asciiOnly, boolean embeddable, Appendable out)
    throws IOException

还有一个同名的方便方法,它使用
StringBuilder
,不需要您处理
IOException

您想转义什么?婚姻?@Michael-你有图书馆吗您想在HTML/JS中转义什么示例?您可以试试。@Michael StringEscapeUtils不是防止XSS攻击的好选择。如果考虑XSS保护,它的<代码> HeaveEcScript脚本>代码>方法从根本上被破坏。正是出于这个目的,OWASP ESAPI优于Apache Commons Lang。您可以将ESAPI中的JavaScriptCodec与escapeEcmaScript进行比较,您会注意到所采用方法的不同。除了Base64编码之外,我认为ESAPI也不支持。看起来有一个用于输出的流接口,但没有用于输入的流接口。我得出的结论是,没有两个库,所以你的答案是最接近的。