本来想用cgilua的authentication模块的,但研究了一下,发现比较麻烦。我就只想实现类似进入家用路由器配置页面的权限认证,最后发现下面方式可用:
http://blog.jianghu.taobao.com/u/NDcxMzMxODA=/blog/blog_detail.htm?aid=32121417
(同时,顺便也帮他找了个错误,让他修改了)
转帖如下:
在kepler里面支持www-auth
change list:
wsapi/xavante.lua :
set_cgivars 函数中添加
AUTHORIZATION = req.headers ["authorization"],
cgilua.lua:
添加:
function httpstatus(no,str)
SAPI.Response.httpstatus(no,str)
end
sapi.lua:
倒数7行位置(write=...后面, 与wirte函数平级)添加:
httpstatus = function(no,str)
res.status = no
end
但test.lp文件在我这儿运行有没反应,最后debug后发现,修改如下即可:
<%
require"mime"
authorized = false;
k = cgilua.servervariable('AUTHORIZATION')
if k and #k > 0 then
str = mime.unb64(string.sub(k,7,-1))
user = string.sub(str,1,string.find(str,':',1,true)-1)
pwd = string.sub(str,string.find(str,':',1,true)+1,-1)
if user==pwd then authorized = true end
end
cgilua.print(user)
cgilua.print(pwd)
if not authorized then
cgilua.header('WWW-Authenticate','Basic realm="login"')
cgilua.httpstatus(401)
cgilua.print'wrong username or password' --- 这句话是添加的,需要在return前无条件显示信息才正确
return
end
cgilua.print'Hello1!'
%>
另:
在html文件中包含另一个html文件(类似jsp的include file),lua代码为
<% cgilua.lp.include ("test1.lp") %>