使用nginx+lua進(jìn)行token鑒權(quán)的方法
最近在使用minio文件服務(wù)器,來實(shí)現(xiàn)圖片上傳與展示功能,在展示的時(shí)候出現(xiàn)一個(gè)問題,就是圖片在做鑒權(quán)時(shí),minio提供的有效地址是真的拉跨,沒法實(shí)現(xiàn)圖片鏈接的有效鑒權(quán)功能,而且鏈接直接給出來的話也有風(fēng)險(xiǎn)。
后面想著加一個(gè)反向代理的方式,也就是Nginx做代理,這樣就不會(huì)暴露圖片的ip地址。但是,在做鑒權(quán)時(shí)遇到了麻煩,網(wǎng)絡(luò)上大部分推薦lua腳本來實(shí)現(xiàn)鑒權(quán)的方式,在經(jīng)過幾天的不斷試錯(cuò),才最終將他實(shí)現(xiàn),現(xiàn)在在此記錄下。
工具:openresty(nginx集成lua模塊,比較簡單)
lua包需要下載lua-resty-http工具包,地址lua-resty-http,解壓后將.lua文件放到 lualib\resty目錄下就行。
實(shí)現(xiàn)思路:用戶發(fā)送請求---> nginx ----->根據(jù)圖片路徑策略-----> 執(zhí)行l(wèi)ua代碼(發(fā)送http到后端token鑒權(quán))----->通過就代理到具體圖片服務(wù)器----->否則提示無權(quán)限
nginx關(guān)鍵代碼
http { # 定義全局變量 init_by_lua_block { cjson = require "cjson"; http = require "resty.http"; } server { location ~^/(api)/image/(.*)$ { rewrite_by_lua_block { -- local cjson = require "cjson" -- local http = require "resty.http" local httpc = http.new() local ngx = ngx local headers = ngx.req.get_headers() -- get請求參數(shù)中T就是token local token = headers["token"] local request_method = ngx.var.request_method local args = nil if "GET" == request_method then args = ngx.req.get_uri_args() elseif "POST" == request_method then ngx.req.read_body() args = ngx.req.get_post_args() end token = args["token"]; if not token then ngx.header['Content-Type'] = 'text/plain; charset=utf-8'; ngx.status = ngx.HTTP_FORBIDDEN ngx.say("您無權(quán)限瀏覽該圖片。") ngx.exit(200) end -- 字符串拼接 -- 你要實(shí)現(xiàn)token鑒權(quán)的服務(wù),header和參數(shù)都給你實(shí)現(xiàn)了,根據(jù)實(shí)際需要選擇 local url = "http://127.0.0.1:8080/image/checkToken?token="..token; local res, err = httpc:request_uri(url, {method="GET", headers={["token"]=token}}) if not res then ngx.header['Content-Type'] = 'text/plain; charset=utf-8'; ngx.say(cjson.encode({message = "Error getting response",status = ngx.HTTP_INTERNAL_SERVER_ERROR })); ngx.exit(200) end if res.body == '0' then ngx.header['Content-Type'] = 'text/plain; charset=utf-8'; ngx.say("您無權(quán)限瀏覽該圖片。"); ngx.exit(200) end } proxy_pass http://yourip:9000/$2; } } }
Java后端代碼
@RestController @RequestMapping("/image") public class TokenResource { @RequestMapping("/checkToken") public String checkToken(HttpServletRequest request, String token) { System.out.println("程序進(jìn)來了"); String header = request.getHeader("token"); System.out.println("頭部token:" + header); if (StringUtils.isEmpty(token)) { System.out.println(token + ":0"); return "0"; } if ("1".equals(token)) { System.out.println(token + ":1"); return "1"; } return "0"; } }
當(dāng)token校驗(yàn)通過,就不做攔截,lua代碼執(zhí)行完會(huì)走到nginx的轉(zhuǎn)發(fā)路徑上來
?proxy_pass http://your_ip:9000/$2?
~^/(api)/image/(.*)$ 寫成 ~^/api/image/(.*)$ 時(shí) proxy_pass 改為 http://your_ip:9000/$1
到此這篇關(guān)于使用nginx+lua進(jìn)行token鑒權(quán)的方法的文章就介紹到這了,更多相關(guān)nginx lua token鑒權(quán)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一個(gè)Nginx實(shí)現(xiàn)部署多個(gè)不同的項(xiàng)目方式
這篇文章主要介紹了一個(gè)Nginx實(shí)現(xiàn)部署多個(gè)不同的項(xiàng)目方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03nginx部署前端post請求405?not?allowed問題解決
在配置前端項(xiàng)目的時(shí)候遇到了一個(gè)post請求405 not allowed,簡單記錄一下如何配置,這篇文章主要給大家介紹了關(guān)于nginx部署前端post請求405?not?allowed問題解決方法,需要的朋友可以參考下2023-09-09關(guān)于nginx 實(shí)現(xiàn)jira反向代理的問題
這篇文章主要介紹了關(guān)于nginx 實(shí)現(xiàn)jira反向代理的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09windows下快速安裝nginx并配置開機(jī)自啟動(dòng)的方法
這篇文章主要介紹了windows下快速安裝nginx 并配置開機(jī)自啟動(dòng)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05Nginx中報(bào)錯(cuò):Permission denied與Connection refused的解決
這篇文章主要給大家介紹了在Nginx中報(bào)錯(cuò):13: Permission denied與111: Connection refused的解決方法,文中介紹的非常詳細(xì),相信對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-04-04nginx配置SSL證書實(shí)現(xiàn)https服務(wù)的方法
這篇文章主要介紹了nginx配置SSL證書實(shí)現(xiàn)https服務(wù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05