Ben*_*ngs 253 html java escaping
有没有逃脱推荐的方法<,>,"和&字符在普通的Java代码输出HTML时?(除了手动执行以下操作外,即).
String source = "The less than sign (<) and ampersand (&) must be escaped before using them in HTML";
String escaped = source.replace("<", "<").replace("&", "&"); // ...
Run Code Online (Sandbox Code Playgroud)
dfa*_*dfa 257
来自Apache Commons Lang的StringEscapeUtils:
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;
// ...
String source = "The less than sign (<) and ampersand (&) must be escaped before using them in HTML";
String escaped = escapeHtml(source);
Run Code Online (Sandbox Code Playgroud)
对于版本3:
import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;
// ...
String escaped = escapeHtml4(source);
Run Code Online (Sandbox Code Playgroud)
Bru*_*ard 54
很好的简短方法:
public static String escapeHTML(String s) {
StringBuilder out = new StringBuilder(Math.max(16, s.length()));
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&') {
out.append("&#");
out.append((int) c);
out.append(';');
} else {
out.append(c);
}
}
return out.toString();
}
Run Code Online (Sandbox Code Playgroud)
基于/sf/answers/618661641/(放大器在那里丢失).根据http://www.w3.org/TR/html4/sgml/entities.html,if子句中检查的四个字符是128以下的唯一字符.
Mar*_*rov 45
有一个较新版本的Apache Commons Lang库,它使用不同的包名(org.apache.commons.lang3).该StringEscapeUtils转义不同类型的文件,现在有不同的静态方法(http://commons.apache.org/proper/commons-lang/javadocs/api-3.0/index.html).所以要转义HTML 4.0版字符串:
import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;
String output = escapeHtml4("The less than sign (<) and ampersand (&) must be escaped before using them in HTML");
Run Code Online (Sandbox Code Playgroud)
okr*_*asz 40
对于那些使用Google Guava的人:
import com.google.common.html.HtmlEscapers;
[...]
String source = "The less than sign (<) and ampersand (&) must be escaped before using them in HTML";
String escaped = HtmlEscapers.htmlEscaper().escape(source);
Run Code Online (Sandbox Code Playgroud)
Ori*_*olJ 39
在Android(API 16或更高版本)上,您可以:
Html.escapeHtml(textToScape);
Run Code Online (Sandbox Code Playgroud)
或者用于较低的API:
TextUtils.htmlEncode(textToScape);
Run Code Online (Sandbox Code Playgroud)
Jef*_*ams 38
小心这个.HTML文档中有许多不同的"上下文":在元素内部,引用的属性值,不带引号的属性值,URL属性,javascript,CSS等...您需要为每个使用不同的编码方法这些是为了防止跨站点脚本(XSS).有关这些上下文的详细信息,请查看OWASP XSS Prevention备忘单 - https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet.您可以在OWASP ESAPI库中找到每个上下文的转义方法 - https://github.com/ESAPI/esapi-java-legacy.
小智 14
出于某些目的,HtmlUtils:
import org.springframework.web.util.HtmlUtils;
[...]
HtmlUtils.htmlEscapeDecimal("&"); //gives &
HtmlUtils.htmlEscape("&"); //gives &
Run Code Online (Sandbox Code Playgroud)
Ada*_*ent 10
虽然@dfa回答org.apache.commons.lang.StringEscapeUtils.escapeHtml很好并且我在过去使用它,但它不应该用于转义HTML(或XML)属性,否则空格将被规范化(意味着所有相邻的空白字符变成单个空格).
我知道这是因为我对我的库(JATL)提出了错误,因为没有保留空格的属性.因此,我有一个(复制n'粘贴)类(我从JDOM中窃取了一些),它区分了属性和元素内容的转义.
虽然这在过去可能没有那么重要(适当的属性转移),但是由于使用HTML5的data-属性使用,它越来越受到关注.
小智 5
org.apache.commons.lang3.StringEscapeUtils现在已弃用。您现在必须使用org.apache.commons.text.StringEscapeUtils
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons.text.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
324638 次 |
| 最近记录: |