背景
最近使用的APM系統(tǒng)skywalking在升級(jí)后壹甥,其將UI的用戶認(rèn)證功能去掉了(開發(fā)者基于安全考慮)非区。這個(gè)后臺(tái)雖然也沒多重要,但多少還是需要一個(gè)用戶認(rèn)證的功能盹廷。結(jié)合公司統(tǒng)一登陸的需求,就想著用nginx+ldap的方式進(jìn)行鑒權(quán)久橙,于是就找到了這個(gè)方案:
https://github.com/kvspb/nginx-auth-ldap
系統(tǒng)環(huán)境:centos7
所需軟件:nginx 1.16俄占、openldap-server、
插件:nginx-auth-ldap(需openssl>1.0.2淆衷,所以用centos7系統(tǒng))
一缸榄、OpenLDAP
前期調(diào)研,就自己搭了一個(gè)ldap祝拯,簡(jiǎn)單記錄下安裝過(guò)程:
1甚带、安裝軟件包
yum install?openldap?openldap-clients?openldap-servers -y
2、配置文件目錄介紹
/etc/openldap/slapd.conf ? ?#ldap的主配置文件佳头,可從自帶的模版文件/usr/share/openldap-servers/slapd.conf.obsolete復(fù)制即可
/var/lib/ldap/DB_CONFIG ? ? #ldap的數(shù)據(jù)庫(kù)配置文件鹰贵,也提供了模版文件/usr/share/openldap-servers/DB_CONFIG.example
/etc/openldap/slapd.d/ ? ? ? ?#該目錄下保存了從slapd.conf生成的一些ldap格式的配置文件
3、復(fù)制配置文件
cp ?/usr/share/openldap-servers/slapd.conf.obsolete?/etc/openldap/slapd.conf?
cp?/usr/share/openldap-servers/DB_CONFIG.example?/var/lib/ldap/DB_CONFIG
4康嘉、生成管理員賬號(hào)密碼
$slappasswd -s Your_PassWord
{SSHA}T4LCsZI3PR+rKVmPcN4cHUnGguy+bAq7
5碉输、修改配置文件
##配置用戶密碼訪問權(quán)限,用戶自己和manager管理員可寫亭珍,
access to attrs=userPassword
? ? by self write
? ? by dn="cn=manager,dc=test,dc=com" write
? ? by anonymous auth
? ? by * none
##管理員可讀寫敷钾,其他用戶只讀
access to *
? ? by self read
? ? by dn="cn=manager,dc=test,dc=com" write
? ? by * read
##其他一些屬性的配置枝哄,只給manager管理員權(quán)限
database monitor
access to *? by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" manage
by dn.base="cn=manager,dc=test,dc=com" manage
by * none
database config
access to *? by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" manage
by dn.base="cn=manager,dc=test,dc=com" manage
by * none
##基礎(chǔ)配置信息
database bdb
suffix? "dc=test,dc=com" ? ? ?
checkpoint? ? ? 1024 15
rootdn? "cn=manager,dc=test,dc=com" ? ?#管理員域
rootpw?{SSHA}T4LCsZI3PR+rKVmPcN4cHUnGguy+bAq7 ? #前面使用slappasswd生成的管理員密碼,中間使用tab隔開
directory? ? ? /var/lib/ldap
6阻荒、測(cè)試配置
?$slaptest -f /etc/openldap/slapd.conf
7挠锥、使用配置文件生成配置信息,并啟動(dòng)服務(wù)
$rm -rf /etc/openldap/slapd.d/*
$slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d
$chown ldap:ldap /etc/openldap/slapd.d
$systemctl restart slapd
8侨赡、導(dǎo)入配置
使用apache Directory studio登陸ldap后蓖租,導(dǎo)入一下配置
dn: dc=test,dc=com
objectClass: dcObject
objectClass: organization
dc: test
o: test
dn: ou=user,dc=test,dc=com
objectClass: organizationalUnit
ou: user
將以上信息保存到user.ldif文件,再通過(guò)directory studio的import方式導(dǎo)入后辆毡,即可在改根域下創(chuàng)建用戶菜秦。
二、nginx-auth-ldap
1舶掖、下載安裝包
$wget http://nginx.org/download/nginx-1.16.0.tar.gz
$git clone https://github.com/kvspb/nginx-auth-ldap.git
$git clone https://github.com/vozlt/nginx-module-vts.git
這里增加了一個(gè)nginx-vts模塊用來(lái)監(jiān)控nginx
2球昨、安裝編譯環(huán)境和依賴
$yum -y install gcc openssl openssl-devel pcre pcre-devel zlib zlib-devel openldap openldap-devel libxml2 libxml2-devel libxslt libxslt-devel gd gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel gperftools
$groupadd www
$useradd www -g www ? ?#創(chuàng)建nginx運(yùn)行用戶和組
3、解壓并編譯安裝
$cd /Your_nginx_directory
$./configure --user=www --group=www --prefix=/usr/local/nginx --add-module=../nginx-auth-ldap/ --add-module=../nginx-module-vts --with-threads --with-file-aio --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-pcre --with-pcre-jit --with-stream --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-google_perftools_module?
$make -j16 ? #多核編譯
$make install
沒有報(bào)錯(cuò)就已經(jīng)安裝完成
4眨攘、配置nginx
根據(jù)nginx-auth-ldap文檔主慰,分別需要在http塊和server塊中增加相關(guān)配置;
http {
......
ldap_server nginx_auth {
? ? ? ? auth_ldap_cache_enabled on;
? ? ? ? auth_ldap_cache_expiration_time 10000;
? ? ? ? auth_ldap_cache_size 1000;
? ? ? ? connect_timeout 5s; ? ? ?#一些請(qǐng)求LDAP的超時(shí)配置
? ? ? ? bind_timeout 5s;
? ? ? ? request_timeout 5s;
? ? ? ? satisfy any;
? ? ? ? url ldap://Your_ldap_Server:389/dc=test,dc=com?uid?sub?(objectClass=account); ? ?#Ldap訪問地址
? ? ? ? binddn "cn=manager,dc=test,dc=com"; ? ? ? ?#Ldap的管理員賬號(hào)
? ? ? ? binddn_passwd ttttt; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #Ldap管理員密碼
? ? ? ? group_attribute uniquemember;
? ? ? ? group_attribute_is_dn on;
? ? ? ? require valid_user;
? ? }
? ? server{
? ? ......
? ? location / {
? ? ? ? ? ? proxy_pass http://upstream;
? ? ? ? ? ??auth_ldap "Forbidden";
? ? ? ? ? ? auth_ldap_servers nginx_auth; ? ? ?#上面定義的ldap_server名稱
? ? ? ? }
? ? }
}
Over
至此鲫售,所有的安裝和配置都已經(jīng)完成共螺,在ldap后臺(tái)添加普通用戶賬號(hào)后,就可以進(jìn)行登陸認(rèn)證了情竹。其他小伙伴就不能愉快的玩耍了藐不。