Open GenweiWu opened 4 years ago
避免使用Random
private static String generateVerificationCode()
{
SecureRandom secureRandom = null; // 安全随机类
try
{
secureRandom = new SecureRandom();
}
catch (Exception e)
{
logger.error("secureRandom generate failed", e);
return null;
}
// 验证码数字取值范围
String codeList = "1234567890";
// 定义一个验证码字符串变量
StringBuilder sRand = new StringBuilder();
for (int i = 0; i < 6; i++)
{
// 随即生成一个0-9之间的整数
int code = secureRandom.nextInt(codeList.length() - 1);
String rand = codeList.substring(code, code + 1);
sRand.append(rand); // 将生成的随机数拼成一个六位数验证码
}
return sRand.toString();
}
public static String escapeHtml(final String value)
{
if (ValidateUtil.isEmptyString(value))
{
LOG.warn("try to escape an empty String{}", value);
return value;
}
String newValue = value;
newValue = newValue.replaceAll("&", "&");
newValue = newValue.replaceAll("<", "<");
newValue = newValue.replaceAll(">", ">");
newValue = newValue.replaceAll("\"", """);
newValue = newValue.replaceAll("\'", "'");
newValue = newValue.replaceAll("\\(", "(");
newValue = newValue.replaceAll("\\)", ")");
return newValue;
}
public static String unescapeHtml(final String value)
{
if (ValidateUtil.isEmptyString(value))
{
LOG.warn("try to escape an empty String{}", value);
return value;
}
String newValue = value;
newValue = newValue.replaceAll("&", "&");
newValue = newValue.replaceAll("<", "<");
newValue = newValue.replaceAll(">", ">");
newValue = newValue.replaceAll(""", "\"");
newValue = newValue.replaceAll("'", "\'");
newValue = newValue.replaceAll("(", "\\(");
newValue = newValue.replaceAll(")", "\\)");
return newValue;
}
String safe = ESAPI.encoder().encodeForHTML( request.getParameter( "input" ) );
<!-- https://mvnrepository.com/artifact/org.owasp.esapi/esapi -->
<dependency>
<groupId>org.owasp.esapi</groupId>
<artifactId>esapi</artifactId>
<version>2.1.0.1</version>
</dependency>
Dynamic Code Evaluation: Unsafe Deserialization反序列化
>理解
>解决:使用白名单进行校验