×ðÁú¿­Ê±

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¡¿