sudone.com 服务器系统架构分析日志


HTTP Basic Authentication验证方式的注销(logout)方法

[2010-04-13 11:10:14]
Basic Authentication方式的注销是个难题,在网上寻找找到了一段js代码,我稍加修改了下,可以很方便地注销掉这种登录。


function clearAuthenticationCache(page) {
  if (!page) page = '/';
  try {
    var agt=navigator.userAgent.toLowerCase();
    if (agt.indexOf("msie") != -1) {
      // IE clear HTTP Authentication
      document.execCommand("ClearAuthenticationCache");
    }
    else if (agt.indexOf("firefox") != -1) {
      // Let's create an xmlhttp object
      var xmlhttp = createXMLObject();
      // Let's prepare invalid credentials
      xmlhttp.open("GET", page, true, "logout", "logout");
      // Let's send the request to the server
      xmlhttp.send("");
      // Let's abort the request
      xmlhttp.abort();
    }
    else {
      window.close();
    }
  } catch(e) {
    // There was an error
    return;
  }
}
    
function createXMLObject() {
  try {
    if (window.XMLHttpRequest) {
      xmlhttp = new XMLHttpRequest();
    }
    // code for IE
    else if (window.ActiveXObject) {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  } catch (e) {
    xmlhttp=false
  }
  return xmlhttp;
}

其中IE浏览器是通过调用内部命令ClearAuthenticationCache实现的;Firefox是通过Ajax发送一个错误的账号密码(logout:logout)实现,所以系统中就不要有叫logout密码也为logout的用户;其它浏览器就直接关闭页面,但如Opera事实上是没有注销的。

站名:sudone.com; 站长:Ayou; 服务器:ubuntu+nginx+squid+php
老细网