还有另一种方法可以做到这一点。我认为它适用于所有框架。
由于我在Java Spring Boot中已经解决了,所以我先给出Java Spring Boot项目的解决方案。
您可以使用 autocomplete="off" 属性关闭自动完成功能。但在许多现代浏览器中,此属性不会产生任何影响。所以,在这种情况下,如果我们在输入字段下再使用一个东西,那么这个问题就可以解决了。
<input type="text" readonly id="username" name="username">
在 Spring Boot 中我们应该这样写:
<html:text property="username" styleId="username" readonly="readonly"></html:text>
现在,通过编写 readonly,我们禁用了保存提示。我们还必须使用 "text" 作为密码类型。所以,它会是这样的:
<input type="text" readonly id="password" name="password">
<html:text property="password" styleId="password" readonly="readonly"></html:text>
但这会使密码字段可见。我们需要在密码字段中显示 "********"。为此,我们将使用一个棘手的方法,即我们将使用一种使每个字符看起来像小点的字体。所以,我们需要改成css内容。
从here 下载“security-disc”字体文件/图像。在 Spring Boot 中,下载“security-disc”字体/图像文件,然后在 WEB-INF/fonts 下的 WebContent 中定义字体文件,在 WEB-INF/images 下定义字体图像>.
<style>
@font-face {
font-family: 'text-security-disc';
src: url('../WEB_INF/fonts/text-security-disc.eot');
src: url('../WEB_INF/fonts/text-security-disc.eot?#iefix') format('embedded-opentype'),
url('../WEB_INF/fonts/text-security-disc.woff') format('woff'),
url('../WEB_INF/fonts/text-security-disc.ttf') format('truetype'),
url('../WEB_INF/images/text-security-disc.svg#text-security') format('svg');
}
input.password {
font-family: 'text-security-disc';
width:15%;
margin-bottom:5px
}
</style>
如果找不到您的目录路径,您可以使用
URL('<%=request.getContextPath()%>/WEB-INF/fonts/text-security-disc.eot');
方法二:
我们可以用来删除密码的另一种方法,以及表单中的其他值。这些值以 cookie 的形式存储在浏览器中,因此如果 cookie 被删除,那么密码以及其他值也会被删除。所以我们只需要添加一个函数来删除cookies。
<script type="text/javascript">
function savePass() {
passVal = "password = "
+ escape(document.Frm.passWord.value)
+ ";";
document.cookie = passVal
+ "expires = Sun, 01-May-2021 14:00:00 GMT";
document.getElementById("show").innerHTML =
"Password saved, " + document.cookie;
}
function dltPass() {
document.cookie = passVal
+ "expires = Sun, 01-May-2005 14:00:00 GMT";
// Set the expiration date to
// removes the saved password
document.getElementById("show").innerHTML =
"Password deleted!!!";
// Removes the password from the browser
document.getElementById("pass").value = "";
// Removes the password from the input box
}
</script>
在这里,我们在 dltPass 函数中添加了较旧的到期日期。因此,cookie 将被视为过期并被删除。
最后,防止浏览器记住密码的另一种最简单的方法是使用 autocomplete="new-password"。这样,浏览器将在以任何形式填写密码字段时给出随机密码建议。所以实际的密码不会保存在浏览器中。