尊龙凯时人生就是搏

NginxÔõÑùʵÏÖ»ùÓÚCookieµÄ»á¼û¿ØÖÆÉèÖÃ

NginxÔõÑùʵÏÖ»ùÓÚCookieµÄ»á¼û¿ØÖÆÉèÖã¬ÐèÒªÏêϸ´úÂëʾÀý

ÔÚWebÓ¦ÓóÌÐòÖУ¬»á¼û¿ØÖÆÊÇÒ»ÏîÒªº¦¹¦Ð§¡£Í¨¹ý»ùÓÚCookieµÄ»á¼û¿ØÖÆÉèÖ㬿ÉÒÔÏÞÖÆÓû§»á¼ûÌض¨µÄÒ³Ãæ»ò×ÊÔ´¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNginxÀ´ÊµÏÖÕâÑùµÄ»á¼û¿ØÖÆ£¬²¢¸ø³öÏêϸµÄ´úÂëʾÀý¡£

¿ªÆôNginxµÄhttp_auth_requestÄ£¿é

Ê×ÏÈ£¬ÐèҪȷ±£NginxÒѾ­ÆôÓÃÁËhttp_auth_requestÄ£¿é¡£ÈôÊÇûÓÐÆôÓ㬿ÉÒÔͨ¹ý±à¼­NginxÉèÖÃÎļþÌí¼Ó¸ÃÄ£¿é¡£

cd /path/to/nginx/source/
./configure --with-http_auth_request_module
make
sudo make install

µÇ¼ºó¸´ÖÆ

ÉèÖÃNginxµÄ»á¼û¿ØÖƹæÔò

ÔÚNginxÉèÖÃÎļþÖУ¬¿ÉÒÔͨ¹ýlocationÖ¸ÁîÀ´½ç˵»á¼û¿ØÖƹæÔò¡£ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽ«ÉèÖÃÖ»ÓÐÓµÓÐÌض¨CookieµÄÓû§²Å»ª»á¼ûÒ»¸öÊܱ£»¤µÄÒ³Ãæ¡£

location /protected {
    auth_request /auth;
    error_page 401 = @error401;
}

location = /auth {
    internal;
    proxy_pass http://backend/auth;
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
    proxy_set_header X-Original-URI $request_uri;
}

µÇ¼ºó¸´ÖÆ

ÉÏÊöÉèÖÃÖУ¬location /protected½ç˵ÁËÒ»¸öÊܱ£»¤µÄÒ³Ã棬auth_request /authÖ¸Á»á·¢ËÍÒ»¸öÇëÇóµ½/authλÖþÙÐÐÈÏÖ¤¡£ÈôÊÇÈÏÖ¤Àֳɣ¬ÔòÔÊÐí»á¼û¸ÃÒ³Ã棻²»È»£¬½«»á·µ»Ø401¹ýʧ¡£

location = /auth½ç˵ÁËÒ»¸öÄÚ²¿ÇëÇó£¬Ëü½«»á±»×ª´ï¸øºó¶ËЧÀÍÆ÷¾ÙÐÐÈÏÖ¤¡£ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǼÙÉèºó¶ËЧÀÍÆ÷µÄµØµãÊÇhttp://backend£¬ÈÏÖ¤½Ó¿ÚΪ/auth¡£Í¨¹ýproxy_passÖ¸ÁîʵÏÖÇëÇóµÄת·¢£¬²¢Í¨¹ýproxy_pass_request_body offºÍproxy_set_header Content-Length “”½ûÓÃÇëÇóÌåµÄת´ï¡£ÁíÍ⣬»¹Í¨¹ýproxy_set_header X-Original-URI $request_uriת´ïԭʼµÄURIÐÅÏ¢¸øºó¶ËЧÀÍÆ÷¡£

±àдºó¶ËЧÀÍÆ÷µÄÈÏÖ¤½Ó¿Ú

ÔÚÉÏÒ»²½µÄÉèÖÃÖУ¬ÎÒÃǼÙÉèºó¶ËЧÀÍÆ÷µÄµØµãΪhttp://backend£¬ÈÏÖ¤½Ó¿ÚΪ/auth¡£ÏÖÔÚ£¬ÎÒÃÇÀ´±àд¸Ã½Ó¿ÚµÄÏÖʵʵÏÖ¡£

ʵÏÖÒ»¸ö¼òÆÓµÄÈÏÖ¤½Ó¿Ú¿ÉÒÔʹÓÃÈκÎWeb±à³ÌÓïÑÔ£¨ÈçPython¡¢PHP»òJava£©À´Íê³É¡£ÔÚÕâÀÎÒÃÇÒÔPythonΪÀý£¬Ê¹ÓÃFlask¿ò¼ÜʵÏÖÒ»¸ö¼òÆӵĽӿÚ¡£

from flask import Flask, request

app = Flask(__name__)

@app.route('/auth', methods=['POST'])
def auth():
    cookie = request.headers.get('Cookie')
    if cookie == 'your_cookie_value':
        return 'OK'
    else:
        return 'Unauthorized', 401

if __name__ == '__main__':
    app.run()

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö/authµÄ·ÓÉ£¬Ëü½ÓÊÜPOSTÇëÇó¡£Í¨¹ýrequest.headers.get(‘Cookie’)»ñÈ¡ÇëÇóÖеÄCookieÐÅÏ¢£¬²¢ÓëÔ¤ÉèµÄCookie¾ÙÐнÏÁ¿¡£ÈôÊÇÏà·û£¬Ôò·µ»Ø”OK”ÌåÏÖÈÏÖ¤Àֳɣ»²»È»£¬·µ»Ø401¹ýʧÌåÏÖÈÏ֤ʧ°Ü¡£

²âÊÔ»ùÓÚCookieµÄ»á¼û¿ØÖÆ

Íê³ÉÒÔÉÏ°ì·¨ºó£¬ÖØÆôNginxЧÀÍ£¬²¢»á¼ûÉèÖÃÖнç˵µÄÊܱ£»¤Ò³Ãæ¡£Ö»ÓÐÔÚ·¢ËÍ°üÀ¨×¼È·CookieµÄÇëÇóʱ£¬²Å»ª¹»Àֳɻá¼ûµ½¸ÃÒ³Ãæ¡£

×ÛÉÏËùÊö£¬ÎÒÃÇͨ¹ýNginxµÄhttp_auth_requestÄ£¿é¡¢»á¼û¿ØÖƹæÔòµÄÉèÖÃÒÔ¼°ºó¶ËЧÀÍÆ÷µÄÈÏÖ¤½Ó¿Ú£¬ÊµÏÖÁË»ùÓÚCookieµÄ»á¼û¿ØÖÆ¡£ÕâÑùµÄÉèÖÿÉÒÔÎÞаµØ¿ØÖÆÓû§¶ÔÌض¨Ò³Ãæ»ò×ÊÔ´µÄ»á¼ûȨÏÞ¡£

×¢ÖØ£ºÔÚÏÖʵÉú²úÇéÐÎÖУ¬ÐèҪƾ֤ÏÖʵÐèÇóºÍÇå¾²ÒªÇó¾ÙÐÐÔ½·¢ÑÏ¿áµÄ»á¼û¿ØÖÆÉèÖ㬲¢ÔÚºó¶ËЧÀÍÆ÷µÄÈÏÖ¤½Ó¿ÚÖÐʵÏÖÔ½·¢ÖØ´óµÄÈÏÖ¤Âß¼­¡£ÒÔÉÏʾÀý½öÌṩÁË»ù±¾µÄ˼Ð÷ºÍÑÝʾ£¬ÏêϸµÄʵÏÖ·½·¨ÐèҪƾ֤ÏêϸÇéÐξÙÐе÷½â¡£

ÒÔÉϾÍÊÇNginxÔõÑùʵÏÖ»ùÓÚCookieµÄ»á¼û¿ØÖÆÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是搏ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是搏ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ