WWW-Authenticate 注销方法

来源:互联网 发布:联通网络解锁助手 编辑:程序博客网 时间:2024/05/01 18:40

目前该方法已经支持IE,Firefox,Chrome

最主要是Chrome的实现方式很恶心,用了sajax,在服务端发送一个WWW-Authenticate

该文章主要参考了http://doc1.fehot.com/2/K/35akhAU.html,然后加入了Chrome的支持

 

<%
require "sajax"

function sys_logout()
    cgilua.servervariable('AUTHORIZATION')
    cgilua.header('WWW-Authenticate','Basic realm=""');
    cgilua.httpstatus(401);
    return
end

sajax.init()
sajax.export ("sys_logout", sys_logout)
if sajax.handle_client_request () then return end
%>

<label for="info">logout</label>
<script type="text/javascript">
    <%
    sajax.show_javascript();
    %>

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 {
      document.execCommand("ClearAuthenticationCache");
      other_browse_logout();
    }
  } 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;
}
function do_sys_apply(msg)
{
    alert('logout')
}

function other_browse_logout()
{
    x_sys_logout(do_sys_apply)
}
clearAuthenticationCache()
parent.window.location = parent.window.location;
</script>
<script type="text/javascript">
    <!--
     document.execCommand("ClearAuthenticationCache");
    parent.window.location = parent.window.location;
    //-->
</script>

 

说明如下:

1:蓝色字体部分: 对于非ie和firefox的都采用sajax方法进行注销

2:绿色字体部分:  如果没有该语句,sajax注销方式不生效,试过只更新页面上的label标签,无效果

3:红色字体部分: 如果采用sajax方法后,发现IE功能不正常,则再添加此代码后正常

4:本服务端语言为lua

5:clearAuthenticationCache传入的page页面必须要有权限验证,否则在firefox退出不成功

 

文中的下面语句,我只是让其logout后如果在输入密码,则显示相应的页面而已,大家可以根据自己需要修改或者删除该句话。

parent.window.location = parent.window.location;