在政府医疗机构工作的乐趣之一是必须处理所有围绕PHI(受保护的健康信息)的偏执。不要误解我的意思,我支持尽一切可能保护人们的个人信息(健康状况、财务状况、上网习惯等),但有时人们会有点太神经质了。

举个例子:我们的一位州客户最近发现浏览器提供了保存密码的方便功能。我们都知道它已经存在了一段时间,完全是可选的,由最终用户决定是否使用它是一个明智的决定。然而,目前有一点骚动,我们被要求找到一种方法来禁用我们网站的功能。

问:网站有没有办法告诉浏览器不要提供记住密码的功能?我从事网络开发已经很长时间了,但我不知道我以前遇到过这种情况。

任何帮助都是感激的。


当前回答

Since most of the autocomplete suggestions, including the accepted answer, don't work in today's web browsers (i.e. web browser password managers ignore autocomplete), a more novel solution is to swap between password and text types and make the background color match the text color when the field is a plain text field, which continues to hide the password while being a real password field when the user (or a program like KeePass) is entering a password. Browsers don't ask to save passwords that are stored in plain text fields.

The advantage of this approach is that it allows for progressive enhancement and therefore doesn't require Javascript for a field to function as a normal password field (you could also start with a plain text field instead and apply the same approach but that's not really HIPAA PHI/PII-compliant). Nor does this approach depend on hidden forms/fields which might not necessarily be sent to the server (because they are hidden) and some of those tricks also don't work either in several modern browsers.

jQuery插件:

https://github.com/cubiclesoft/php-flexforms-modules/blob/master/password-manager/jquery.stoppasswordmanager.js

相关源代码来自上述链接:

(function($) {
$.fn.StopPasswordManager = function() {
    return this.each(function() {
        var $this = $(this);

        $this.addClass('no-print');
        $this.attr('data-background-color', $this.css('background-color'));
        $this.css('background-color', $this.css('color'));
        $this.attr('type', 'text');
        $this.attr('autocomplete', 'off');

        $this.focus(function() {
            $this.attr('type', 'password');
            $this.css('background-color', $this.attr('data-background-color'));
        });

        $this.blur(function() {
            $this.css('background-color', $this.css('color'));
            $this.attr('type', 'text');
            $this[0].selectionStart = $this[0].selectionEnd;
        });

        $this.on('keydown', function(e) {
            if (e.keyCode == 13)
            {
                $this.css('background-color', $this.css('color'));
                $this.attr('type', 'text');
                $this[0].selectionStart = $this[0].selectionEnd;
            }
        });
    });
}
}(jQuery));

演示:

https://barebonescms.com/demos/admin_pack/admin.php

点击菜单中的“添加条目”,然后滚动到页面底部的“模块:停止密码管理器”。

免责声明:虽然这种方法适用于视力正常的人,但屏幕阅读器软件可能存在问题。例如,屏幕阅读器可能会大声读出用户的密码,因为它看到的是纯文本字段。使用上述插件还可能产生其他不可预见的后果。改变内置的web浏览器功能应该通过测试各种各样的条件和边缘情况来进行。

其他回答

我在这个问题上苦苦挣扎了一段时间,并有了一个独特的解决方法。特权用户不能让保存的密码为他们工作,但普通用户需要它。这意味着特权用户必须登录两次,第二次强制不保存密码。

有了这个要求,标准的autocomplete="off"方法并不适用于所有浏览器,因为密码可能是从第一次登录时保存的。一位同事找到了一种解决方案,在关注新密码字段时替换密码字段,然后关注新密码字段(然后连接相同的事件处理程序)。这是有效的(除了它在IE6中造成了一个无限循环)。也许有办法,但这让我头疼。

最后,我尝试将用户名和密码放在表单之外。令我惊讶的是,这竟然起作用了!它可以在IE6上运行,也可以在Linux上运行当前版本的Firefox和Chrome。我还没有进一步测试它,但我怀疑它在大多数(如果不是所有)浏览器中都能运行(但如果有一种浏览器不关心是否没有表单,我也不会感到惊讶)。

下面是一些示例代码,以及一些jQuery来让它工作:

<input type="text" id="username" name="username"/>
<input type="password" id="password" name="password"/>

<form id="theForm" action="/your/login" method="post">
  <input type="hidden" id="hiddenUsername" name="username"/>
  <input type="hidden" id="hiddenPassword" name="password"/>
  <input type="submit" value="Login"/>
</form>

<script type="text/javascript" language="JavaScript">
  $("#theForm").submit(function() {
    $("#hiddenUsername").val($("#username").val());
    $("#hiddenPassword").val($("#password").val());
  });
  $("#username,#password").keypress(function(e) {
    if (e.which == 13) {
      $("#theForm").submit();
    }
  });
</script>

您可以通过在每个显示中随机为密码字段使用的名称来防止浏览器匹配表单。然后浏览器看到与url相同的密码,但不能确定这是相同的密码。也许它在控制其他东西。

更新:注意,这应该是使用自动补全或其他策略的补充,而不是替代它们,因为其他人指出的原因。

还要注意,这只会阻止浏览器自动完成密码。它不会阻止它以浏览器选择使用的任意安全级别存储密码。

我被给了一个类似的任务禁用自动填写登录名和密码的浏览器,经过大量的试验和错误,我发现下面的解决方案是最优的。只需在原始控件之前添加以下控件。

<input type="text" style="display:none">
<input type="text" name="OriginalLoginTextBox">

<input type="password" style="display:none">
<input type="text" name="OriginalPasswordTextBox">

这在IE11和Chrome 44.0.2403.107上运行正常

使用这种方法我没有遇到任何问题:

使用autocomplete="off",添加一个隐藏密码字段,然后再添加一个非隐藏密码字段。如果浏览器不尊重autocomplete="off"则会尝试自动完成隐藏的内容

最干净的方法是使用autocomplete="off"标签属性but 当你用Tab切换字段时,Firefox不能正确地遵守它。

唯一可以阻止这种情况的方法是添加一个虚假的隐藏密码字段,它会欺骗浏览器在那里填充密码。

<input type="text" id="username" name="username"/>
<input type="password" id="prevent_autofill" autocomplete="off" style="display:none" tabindex="-1" />
<input type="password" id="password" autocomplete="off" name="password"/>

这是一种丑陋的黑客行为,因为你改变了浏览器的行为,这应该被认为是糟糕的做法。只有在你真的需要的时候才使用它。

注意:这将有效地停止密码自动填充,因为FF将“保存”#prevent_autofill的值(它是空的),并将尝试填充那里保存的任何密码,因为它总是使用在DOM中分别输入“username”之后找到的第一个type=“password”输入。