npm install access-token-api
//server(nodejs) 端
var accessTokenApi = require('access-token-api');
var TokenApi = new accessTokenApi({
webTokenVarName:'encrypt_api_tokenStr',//前端可通过webTokenVarName变量去到token值,默认encrypt_api_tokenStr
});
//web前端取token值
window[webTokenVarName] //请求接口时带上这个值就能进行token校验了
主要接口
生成token api `issue`
验证token api `verify`
token 有效次数减一 api `decline`
server 端 将token 注入到 web 前端页面 api `webInject` 自定义后端注入token到web前端页面的方式,可以初始化模块时自定义webInject参数(函数)
var TokenApi = new accessTokenApi({
webInject:function(html,token,callback){
var htmlEndIndex = html.indexOf('</html>');
var tokenScript = '<script>window.' + this.config.webTokenVarName + '=' + token + '</script>';
var prevHtml = html.substring(0,htmlEndIndex);
var nextHtml = html.substr(htmlEndIndex);
prevHtml += tokenScript;
prevHtml += nextHtml;
callback(null,prevHtml);
}
});