基于ldap的nginx用戶訪問(wèn)認(rèn)證

背景

最近使用的APM系統(tǒng)skywalking在升級(jí)后,其將UI的用戶認(rèn)證功能去掉了(開(kāi)發(fā)者基于安全考慮)。這個(gè)后臺(tái)雖然也沒(mé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、修改配置文件

##配置用戶密碼訪問(wèn)權(quán)限,用戶自己和manager管理員可寫(xiě),

access to attrs=userPassword

? ? by self write

? ? by dn="cn=manager,dc=test,dc=com" write

? ? by anonymous auth

? ? by * none

##管理員可讀寫(xiě),其他用戶只讀

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隔開(kāi)

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)境和依賴(lài)

$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

沒(méi)有報(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訪問(wèn)地址

? ? ? ? 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名稱(chēng)

? ? ? ? }

? ? }

}

Over

至此,所有的安裝和配置都已經(jīng)完成,在ldap后臺(tái)添加普通用戶賬號(hào)后,就可以進(jìn)行登陸認(rèn)證了。其他小伙伴就不能愉快的玩耍了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容