最近在改写kibana,碰到了验证登录的问题。问题是这样子的,nginx设置了basic认证,然后客户端访问kibana的时候总是会弹出登录框,输入用户名和密码,现在要改写这个登陆框,用bootstrap来模拟一下登录验证。

最终效果:

解决方案:首先看到请求的返回status是401,也就是未授权,可以自己百度一下 `basic authentication`。我们需要在node端把401的错误码改成其他的,我这里改成了403。红色部分是我后来加上的,文件路径: `src/server/plugins/elasticsearch/lib/create_proxy.js`。这样就不会弹出难看的认证框了。

var createAgent = require('./create_agent');
var mapUri = require('./map_uri');
module.exports = function createProxy(server, method, route, opts) {
opts = opts || {};
var options = {
method: method,
path: route,
handler: {
proxy: {
mapUri: mapUri(server, opts.prefix),
passThrough: true,
agent: createAgent(server),
onResponse:function(err, res, request, reply, settings, ttl){
if(res.statusCode==401){
res.statusCode =403;
}
reply(res);
}
}
}
};
if (opts && opts.config) options.config = opts.config;
server.route(options);
};

解决了nodejs端的状态码问题,现在该处理前端的展示方面了。

看了kibana源码,这里引用了angularjs,那就好办了,我们可以利用$httpProvider这个服务来写一个拦截器。然后在request的时候,把本地存储的用户名和密码写到header中去,就模拟了basic验证。文件路径:src/kibana/plugins/kibana/index.js

// ensure that the kibana module requires ui.bootstrap
require('modules')
.get('kibana', ['ui.bootstrap','ngCookies'])
.config(function ($tooltipProvider) {
$tooltipProvider.setTriggers({ 'mouseenter': 'mouseleave click' });
})
.factory('HttpInterceptorFactory', ['$rootScope', '$cookieStore', '$q', function ($rootScope, $cookieStore, $q) {
return {
'request': function (httpConfig) {
var up = $cookieStore.get('usernameandpassword'); up && (httpConfig.headers.Authorization = "Basic " + up); return httpConfig;
}
};
}])
.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('HttpInterceptorFactory');
}])
.controller('LoginController',function($scope,$cookieStore,$modalInstance){
$scope.username="";
$scope.password="";
$scope.ok = function (user,psw) {
$cookieStore.put("usernameandpassword", btoa(user +":"+psw));
location.reload();
};
})
.directive('kibana', function (Private, $rootScope,$timeout, $cookieStore,$injector, Promise, config, kbnSetup,$modal) {
return {
template: require('text!plugins/kibana/kibana.html'),
controllerAs: 'kibana',
controller: function ($scope) {
var _ = require('lodash');
var self = $rootScope.kibana = this;
var notify = new Notifier({ location: 'Kibana' }); // this is the only way to handle uncaught route.resolve errors
$rootScope.$on('$routeChangeError', function (event, next, prev, err) {
if(err.origError.status==403){
// location.hash="#/passport/login";
self.doLogin();
return;
}
notify.fatal(err);
}); self.doLogin = function(){
var modalInstance = $modal.open({
template: require('text!plugins/kibana/login.html'),
controller: 'LoginController',
size: 'sm',
backdrop:'static'
});
modalInstance.result.then(function (selectedItem) { }, function () {
$log.info('Modal dismissed at: ' + new Date());
});
} // run init functions before loading the mixins, so that we can ensure that
// the environment is ready for them to get and use their dependencies
self.ready = Promise.all([ kbnSetup(), config.init() ])
.then(function () {
// load some "mixins"
var mixinLocals = { $scope: $scope, notify: notify };
$injector.invoke(require('plugins/kibana/_init'), self, mixinLocals);
$injector.invoke(require('plugins/kibana/_apps'), self, mixinLocals);
$injector.invoke(require('plugins/kibana/_timepicker'), self, mixinLocals); $scope.setupComplete = true;
});
}
};
});
<div class="modal-header">
<h3 class="modal-title">Login In</h3>
</div> <div class="modal-body">
<div class="form-group">
<label >Username</label>
<input style="border:1px solid;" type="text" ng-model="username" class="form-control" placeholder="Username">
</div>
<div class="form-group">
<label >Password</label>
<input style="border:1px solid;" type="password" ng-model="password" class="form-control" placeholder="Password">
</div>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok(username,password)">Login</button>
</div>

大功告成。我使用的kibana的版本是4.2.0,enjoy it。

kibana去掉丑陋的basic验证框,用自定义验证代替。的更多相关文章

  1. SpringBoot-表单验证-统一异常处理-自定义验证信息源

    1. 简介 我们都知道前台的验证只是为了满足界面的友好性.客户体验性等等.但是如果仅靠前端进行数据合法性校验,是远远不够的.因为非法用户可能会直接从客户端获取到请求地址进行非法请求,所以后台的校验是必 ...

  2. easyui 表单验证validatetype——支持自定义验证

    easyui 的validatebox()提供了自定义验证的方法,为此我把一些常用的数据验证汇总了一下,代码如下: 代码 Code highlighting produced by Actipro C ...

  3. struts2学习(12)struts2验证框架2.自定义验证

    一.例子需求: 对敏感词进行验证: 将struts包中的validators.xml文件拷贝一份到src目录下,在最后面添加自己的验证器: com.cy.validators.SensitiveWor ...

  4. angularJS中的表单验证(包括自定义验证)

    表单验证是angularJS一项重要的功能,能保证我们的web应用不会被恶意或错误的输入破坏.Angular表单验证提供了很多表单验证指令,并且能将html5表单验证功能同他自己的验证指令结合起来使用 ...

  5. Yii CActiveForm 客户端验证(enableClientValidation)和自定义验证

    使用Yii的CActiveForm默认使用服务器端模型(model)的rules规则验证数据. 但这会导致无谓的请求提交,比较好的方式是为了用户体验在客户端也验证,而为了安全性,在服务器端和数据库也做 ...

  6. easyui -validatebox 验证框加载

    问题: easyui验证狂框有时会验证输入字符的位数,或者验证有效字符组合 解决: 使用easyui的验证框,继承验证框,指定输入框为验证框即可 $(function(){ $.extend($.fn ...

  7. Extjs自定义验证介绍

    表单验证实例(空验证,密码确认验证,email验证) 我们可以用单独的js写表单验证,但是extjs已经为我们想到了(自己单独写反而不方便). 在验证之前,我不得不提两个小知识点: //大家在很多的e ...

  8. ASP.NET开发中主要的字符验证方法-JS验证、正则表达式、验证控件、后台验证

    ASP.NET开发中主要的字符验证方法-JS验证.正则表达式.验证控件.后台验证 2012年03月19日 星期一 下午 8:53 在ASP.NET开发中主要的验证方法收藏 <1>使用JS验 ...

  9. 使用自定义验证组件库扩展 Windows 窗体

    使用自定义验证组件库扩展 Windows 窗体             1(共 1)对本文的评价是有帮助 - 评价此主题                          发布日期 : 8/24/20 ...

随机推荐

  1. 《Cortex-M0权威指南》之Cortex-M0技术综述

    转载请注明来源:cuixiaolei的技术博客 Cortex-M0 处理器简介 1. Cortex-M0 处理器基于冯诺依曼架构(单总线接口),使用32位精简指令集(RISC),该指令集被称为Thum ...

  2. c语言中赋值语句的结果

    c语言中赋值语句的结果 最近在看<C语言接口与实现>,在第一章就发现一个以前很少用到的用法.在实现strcpy函数时,书中给出了两种实现. //version 1, 使用数组实现(新手惯用 ...

  3. 100天成就卓越领导力:新晋领导者的First100训练法

    <100天成就卓越领导力:新晋领导者的First100训练法> 基本信息 原书名:Your Frist 100 days: How to Make Maximum Impact in Yo ...

  4. Oracle 基础——索引

    一.索引 索引是一种快速访问数据的途径,可提高数据库性能.索引使数据库程序无须对整个表进行扫描,就可以在其中找到所需的数据,就像书的目录,可以快速查找所需的信息,无须阅读整本书. 二.索引存在的问题 ...

  5. Gulp 自动化的项目构建工具

    在很多场合都会听到前端工和 node 程师推荐 Grunt 来实现项目的自动化,自动化可以自动完成 javascript/coffee/sass/less 等文件的的测试.检查.合并.压缩.格式化.部 ...

  6. Laravel 清空配置缓存

    清空配置缓存 php artisan cache:clear php artisan config:clear

  7. Common工具类的验证码类的使用(未实现验证)

    验证码接收 using System; using System.Collections.Generic; using System.Linq; using System.Web; using CZB ...

  8. Linq 数据库操作(增删改查)

    Linq数据库增删改查 Linq是一种查询语言,集成包含在formwork中,包含在C#语言中,它的作用是降低查询的门槛,提高开发效率,是我们必须掌握的技术之一,下面是我自己对linq数据库操作的方法 ...

  9. win7安装office2007出错被中断-已经解决

    觉得雨林木风win7系统本身的office2007不好,但不能卸载,用360强力删除工具,把整个安装的文件夹全部删除,重新用之前能够在另外xp和win7系统成功安装的破解版office2007,安装开 ...

  10. Linux基本服务命令

    启动Httpd服务:service httpd start 停止Httpd服务:service httpd stop 重启Httpd服务:service httpd restart 关闭httpd所有 ...