• ldap集成nginx


    nginx版本:1.10.2


    nginx安装:
    wget http://nginx.org/download/nginx-1.10.2.tar.gz
    tar zxvf nginx-1.10.2.tar.gz
    cd nginx-1.10.2
    ./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_ssl_module --with-http_sub_module --with-http_auth_request_module --with-http_stub_status_module
    make
    make install
    已安装nginx服务的话,通过 nginx -V 检查是否支持编译时是否添加 --with-http_auth_request_module,因为认证需要用到ngx_http_auth_request_module,如果不支持,需要重新编译添加(参考:https://anyof.me/articles/236)。

    nginx的ldap认证需要通过nginx官方提供的服务去实现,首先需要把代码下载下来。
    git clone https://github.com/nginxinc/nginx-ldap-auth
    nginx-ldap-auth主要涉及的有两个脚本:
    backend-sample-app.py:提供登录服务
    nginx-ldap-auth-daemon.py:提供认证服务
    脚本需要ldap模块支持: yum install python-ldap -y
    backend-sample-app.py的登录界面会将密码明文显示,很不友好,将html内容替换成:

    html="""
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8"/>
    <title>登陆框</title>
    </head>
    <style>
    *{margin:0;padding:0;}
    .login{
    width:334px;
    height:220px;
    margin:0 auto;
    position:absolute;
    left:40%;
    top:40%;
    }
    .login_title{
    color: #000000;
    font: bold 14px/37px Arial,Helvetica,sans-serif;
    height: 37px;
    padding-left: 35px;
    text-align: left;
    }
    
    .login_cont {
        background: none repeat scroll 0 0 #FFFFFF;
        border: 1px solid #B8B7B7;
        height: 152px;
        padding-top: 30px;
    }
    .form_table {
        float: left;
        margin-top: 10px;
        table-layout: fixed;
        width: 100%;
    }
    .form_table th {
        color: #333333;
        font-weight: bold;
        padding: 5px 8px 5px 0;
        text-align: right;
        white-space: nowrap;
    }
    .form_table td {
        color: #717171;
        line-height: 200%;
        padding: 6px 0 5px 10px;
        text-align: left;
    }
    .login_cont input.submit {
        background-position: 0 -37px;
        height: 29px;
        margin: 10px 14px 0 0;
        width: 82px;
    }
    </style>
    <body>
        <div class="login">
            <div class="login_cont">
                <form action='/login' method='post'>
                    <table class="form_table">
                        <col width="90px" />
                        <col />
                        <tr>
                            <th>用户名:</th><td><input class="normal" type="text" name="username" alt="请填写用户名" /></td>
                        </tr>
                        <tr>
                            <th>密码:</th><td><input class="normal" type="password" name="password" alt="请填写密码" /></td>
                        </tr>
                        <tr>
                            <th></th><td><input class="submit" type="submit" value="登录" /><input class="submit" type="reset" value="取消" /></td>
                        </tr>
                    </table>
                        <input type="hidden" name="target" value="TARGET">
                </form>
            </div>
        </div>
    </body>
    </html>"""

    接下来把两个脚本运行起来:
    nohup python backend-sample-app.py > login.log 2>&1 &
    nohup python nginx-ldap-auth-daemon.py --host 0.0.0.0 >auth.log 2>&1 &
    脚本默认监听localhost,根据需要自行修改。


    nginx ldap认证 配置
    在需要认证的server里加入以下:

    location / {
    #auth_basic "Welcome to solr";
    #auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
    auth_request /auth-proxy;
    error_page 401 403 =200 /login;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #需要认证的服务
    proxy_pass http://dev-solr:8983;
    }

    location /login {
    #登录服务
    proxy_pass http://172.31.29.18:9000/login;
    proxy_set_header X-Target $request_uri;
    }

    location = /auth-proxy {
    internal;
    #认证服务
    proxy_pass http://172.31.29.18:8888;
    proxy_cache_key "$http_authorization$cookie_nginxauth";
    proxy_cache_valid 200 403 1m;

    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
    proxy_set_header X-Ldap-URL "ldap://ldap.xxxx.net:389";
    proxy_set_header X-Ldap-BaseDN "DC=ldap,DC=xxxx,DC=net";
    #proxy_set_header X-Ldap-Template "(|(cn=xxxxx@xxxxx.com)(cn=xxxxx@xxxxxx.com))";
    proxy_set_header X-Ldap-BindDN "cn=Manager,dc=ldap,dc=xxxxx,dc=net";
    proxy_set_header X-Ldap-BindPass "xxxxxxx";
    proxy_set_header X-CookieName "nginxauth";
    proxy_set_header Cookie nginxauth=$cookie_nginxauth;
    }

    可以通过X-Ldap-Template配置ldap某个组的用户访问server:proxy_set_header X-Ldap-Template "(&(cn=%(username)s)(memberOf=cn=groupname,cn=Users,dc=ldap,dc=xxxxx,dc=com))";

    但我这边配置后发现ldap 不支持 memberOf,需要手动开启。因为是一些内部服务,这边采用一个折中的方法,设置白名单方式限制访问。
    缺陷:
    不支持权限控制
    管理账号明文显示,有风险(可以把账号配置在nginx-ldap-auth-daemon.py脚本里)

    参考链接:https://sapser.github.io/devops/2016/07/22/nginx-ldap

    赠人玫瑰,手有余香,如果我的文章有幸能够帮到你,麻烦帮忙点下右下角的推荐,谢谢!

    作者: imcati

    出处: https://www.cnblogs.com/imcati/>

    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接

  • 相关阅读:
    接口:
    抽象类:
    构造方法与重载
    get方法和set方法
    类的成员变量与方法、构造方法
    构造方法
    Java面向对象学习-----类的成员变量2
    Java面向对象学习-----类的成员变量
    java字符串分割
    android 使用讯飞人脸识别api报错:java.lang.UnsatisfiedLinkError
  • 原文地址:https://www.cnblogs.com/imcati/p/9372094.html
Copyright © 2020-2023  润新知