kibana去掉丑陋的basic验证框,用自定义验证代替。
最近在改写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验证框,用自定义验证代替。的更多相关文章
- SpringBoot-表单验证-统一异常处理-自定义验证信息源
1. 简介 我们都知道前台的验证只是为了满足界面的友好性.客户体验性等等.但是如果仅靠前端进行数据合法性校验,是远远不够的.因为非法用户可能会直接从客户端获取到请求地址进行非法请求,所以后台的校验是必 ...
- easyui 表单验证validatetype——支持自定义验证
easyui 的validatebox()提供了自定义验证的方法,为此我把一些常用的数据验证汇总了一下,代码如下: 代码 Code highlighting produced by Actipro C ...
- struts2学习(12)struts2验证框架2.自定义验证
一.例子需求: 对敏感词进行验证: 将struts包中的validators.xml文件拷贝一份到src目录下,在最后面添加自己的验证器: com.cy.validators.SensitiveWor ...
- angularJS中的表单验证(包括自定义验证)
表单验证是angularJS一项重要的功能,能保证我们的web应用不会被恶意或错误的输入破坏.Angular表单验证提供了很多表单验证指令,并且能将html5表单验证功能同他自己的验证指令结合起来使用 ...
- Yii CActiveForm 客户端验证(enableClientValidation)和自定义验证
使用Yii的CActiveForm默认使用服务器端模型(model)的rules规则验证数据. 但这会导致无谓的请求提交,比较好的方式是为了用户体验在客户端也验证,而为了安全性,在服务器端和数据库也做 ...
- easyui -validatebox 验证框加载
问题: easyui验证狂框有时会验证输入字符的位数,或者验证有效字符组合 解决: 使用easyui的验证框,继承验证框,指定输入框为验证框即可 $(function(){ $.extend($.fn ...
- Extjs自定义验证介绍
表单验证实例(空验证,密码确认验证,email验证) 我们可以用单独的js写表单验证,但是extjs已经为我们想到了(自己单独写反而不方便). 在验证之前,我不得不提两个小知识点: //大家在很多的e ...
- ASP.NET开发中主要的字符验证方法-JS验证、正则表达式、验证控件、后台验证
ASP.NET开发中主要的字符验证方法-JS验证.正则表达式.验证控件.后台验证 2012年03月19日 星期一 下午 8:53 在ASP.NET开发中主要的验证方法收藏 <1>使用JS验 ...
- 使用自定义验证组件库扩展 Windows 窗体
使用自定义验证组件库扩展 Windows 窗体 1(共 1)对本文的评价是有帮助 - 评价此主题 发布日期 : 8/24/20 ...
随机推荐
- iOS 虚拟机测试出现的相关问题
一.报红 1.问题描述:自己在工程目录下新建文件夹 包其他文件夹下的.h 和.m文件拖到该文件夹 再删除工程中的报红文件复制进去新文件夹 运行报错:No such file or directory: ...
- ubuntu创建、删除文件及文件夹,强制清空回收站方法
mkdir 目录名 => 创建一个目录 rmdir 空目录名 => 删除一个空目录 rm 文件名 文件名 => 删除一个文件或多个文件 rm –rf 非 ...
- 使用aop记录数据库操作的执行时间
在项目中,我们往往需要记录数据库操作的时间,根据操作时间的不同,分别记录不同等级的日志. 首先我们可以写一个类实现MethodInterceptor接口: import org.aopalliance ...
- 面对一个“丢失了与用户“签订”的协议的修改”时进行的思考。
对于上图中的gauge,将value与label之间的比例值调整了,调整为1:1.2.这意味着,在新系统中打开老报表,老报表中的这个gauge的value可能会比以前大,二者可能是用户厌恶的效果. 严 ...
- CS异步下载
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Houdini 13在Ubuntu系统下流畅运行、不崩溃
至尊影视特效软件Houdini FX,当前最新版是13.0.547,经过试用在Ubuntu系统下可以完美运行,目前为止还没出现过崩溃的情况,之前在windows下使用Houdini 13简直就是噩梦, ...
- Part 8 Coalesce function in sql server
- FMS服务器在centos下安装
首先当然得先下载安装包了 http://pan.baidu.com/s/1jGL1Nvw #要先安装一下这个包,否则会提收提示错误,缺少libcap yum install compat-libcap ...
- vSphere存储
write by xiaoyang 配置iSCSI外部存储 1. 选择配置——硬件——存储 2. 在存储适配器里选择添加软件iSCSI适配器 3. 确认添加 4. ...
- 集合框架学习之排序Comparable&Comoarator
1.内置引用数据类型比较(常用) 1.1 Comparable 1.整数.小数Integer Float Double 直接比较基本数据类型的大小 2.字符:比较的Unicode码只差 3.字符串: ...