一類動詞二類動詞三類動詞_基于http動詞的完全無效授權技術

一類動詞二類動詞三類動詞

Authorization is a basic feature of modern web applications. It’s a mechanism of specifying access rights or privileges to resources according to user roles. In case of CMS like applications, it needs to be equipped with advanced libraries and authorization techniques. But for minimal applications a full fledged library can be an overhead.

授權是現代Web應用程序的基本功能。 這是一種根據用戶角色指定對資源的訪問權限或特權的機制。 如果是類似CMS的應用程序,則需要配備高級庫和授權技術。 但是對于最少的應用程序來說,完整的庫可能會增加開銷。

I will discuss a dead simple authorization technique based on HTTP verbs, for this particular purpose.

為此,我將討論一種基于HTTP動詞的簡單授權技術。

事前要考慮的事情 (Things to consider beforehand)

This technique isn’t something you can implement anywhere. Use this only if your requirements match the particular scenario.

您無法在任何地方實施此技術。 僅當您的要求符合特定情況時才使用此選項。

  • It works only for REST APIs. Everything happens on middleware layer. If you have a simple MVC based REST APIs, this is for you.

    它僅適用于REST API。 一切都發生在中間件層上。 如果您有一個簡單的基于MVC的REST API,則適合您。
  • It heavily relies on the HTTP verbs and the URL naming convention. So API endpoints should be super clear and structured. Similar to some structure like this one.

    它在很大程度上依賴于HTTP動詞和URL命名約定。 因此,API端點應該超級清晰和結構化。 類似于這種結構。
List Products  : GET    /products
Product Detail : GET /products/{id}
Create Product : POST /products
Update Product : PUT /products/{id}
Delete Product : DELETE /products/{id}
  • A URL can perform many stuffs; but all cannot be expressed just in its naming and HTTP verb. If you require complex authorization, you can’t just rely on this technique.

    URL可以執行許多工作; 但不能僅使用其命名和HTTP動詞來表示所有內容。 如果您需要復雜的授權,則不能僅僅依靠這種技術。

Lets implement the dead simple authorization technique based on HTTP verbs. For demo purpose we will be using Nodejs. You can implement it on any language and platform of your choice: core Nodejs, ExpressJS, aws Lambda etc..

讓我們基于HTTP動詞實現完全無效的簡單授權技術。 出于演示目的,我們將使用Nodejs。 您可以在您選擇的任何語言和平臺上實現它:核心Node.js,ExpressJS,aws Lambda等。

步驟1:將用戶角色編碼為JWT令牌 (Step 1: Encode user role into JWT Token)

JWT token is the key thing here. It contains the user role encoded in it. The token is returned when user logs in.

JWT令牌是這里的關鍵。 它包含其中編碼的用戶角色。 用戶登錄時將返回令牌。

const jwt = require(‘jsonwebtoken’);const token = jwt.sign({

role: userData.role
}, JWT_KEY);

On the next API call, the token is passed as the value of Authorization header field.

在下一個API調用中,令牌作為Authorization標頭字段的值傳遞。

Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdW...

第2步:解碼令牌并檢查權限 (Step 2: Decode token and check permissions)

When request is sent to the web server with JWT token attached on header, it goes through a middleware layer. Inside the layer the token is extracted, decoded. To check for permission we require two information.

當請求發送到帶有標頭上的JWT令牌的Web服務器時,請求將通過中間件層。 在該層內部,令牌被提取,解碼。 要檢查許可,我們需要兩個信息。

  • User role: decoded from token

    用戶角色:從令牌解碼
  • Resource name: identified from request URL

    資源名稱:從請求URL標識
const jwt = require('jsonwebtoken');// extract token from header
let authHeader = request.header.Authorization;
let token = authHeader.split(" ")[1];// decode token and get user's 'role'
let decodedVal = jwt.verify(token, process.env.JWT_KEY);
let role = decodedVal.role;// get resource name(based on your web framework)
// eg:
// GET /products/1 => 'products'
// PUT /users/3    => 'users'
// POST /orders    => 'orders'
let resourceName = request.url.split("/")[1];

The mechanism of retrieving HTTP verb and resource name may differ according to the language or framework being used. Above code is only for demonstration purpose.

根據所使用的語言或框架,檢索HTTP動詞和資源名稱的機制可能有所不同。 上面的代碼僅用于演示目的。

The permissions for resources according to user roles are stored in the following manner. Each of the roles have access to certain resources. Within resources they can perform certain actions determined by HTTP verbs.

根據用戶角色的資源許可以以下方式存儲。 每個角色都可以訪問某些資源。 在資源內,他們可以執行由HTTP動詞確定的某些動作。

const PERMISSIONS = {"vendor": {"products": ["POST", "PUT", "DELETE", "GET"],"orders": ["POST", "PUT", "DELETE", "GET"],"stores": ["POST", "PUT", "DELETE", "GET"],"dashboard": ["GET"]},"customer": {"products": ["GET"],"orders": ["GET"],"stores": ["GET"],"comments": ["GET", "POST"],"shopping-carts": ["GET", "POST"],"dashboard": ["GET"]},"admin": {"products": ["POST", "PUT", "DELETE", "GET"],"orders": ["POST", "PUT", "DELETE", "GET"],"stores": ["POST", "PUT", "DELETE", "GET"],"comments": ["POST", "PUT", "DELETE", "GET"],"shopping-carts": ["POST", "PUT", "DELETE", "GET"],"dashboard": ["POST", "PUT", "DELETE", "GET"]}
};

The method below returns whether the user is allowed to access the resource or not.

下面的方法返回是否允許用戶訪問資源。

function checkPermission(role, resource, httpVerb){if (PERMISSIONS[role] && PERMISSIONS[role][resource]) return PERMISSIONS[role][resource].includes(httpVerb);return false;
}// Example// request from "admin" 
// POST https://test-domain.com/products/ => true// request from "customer" 
// POST https://test-domain.com/products/ => false

Based on the result, the API request can be forwarded to the next middleware layer/controller or the request can be denied with error response.

根據結果??,可以將API請求轉發到下一個中??間件層/控制器,也可以通過錯誤響應拒絕該請求。

The approach may work only for certain use cases(as mentioned above). If you have the same scenario, instead of relying on heavy libraries you can implement the technique fast and easy.

該方法可能僅適用于某些用例(如上所述)。 如果您具有相同的方案,則無需依賴繁瑣的庫,而是可以快速輕松地實現該技術。

What do you think about this technique ? Do you have some other better approach ? Please share it on the comments below.

您如何看待這種技術? 您還有其他更好的方法嗎? 請在下面的評論中分享。

翻譯自: https://medium.com/@bibhutipd/dead-simple-authorization-technique-based-on-http-verbs-7a2c3cfbde2f

一類動詞二類動詞三類動詞

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/387832.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/387832.shtml
英文地址,請注明出處:http://en.pswp.cn/news/387832.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

主成份分析(PCA)詳解

主成分分析法(Principal Component Analysis)大多在數據維度比較高的時候,用來減少數據維度,因而加快模型訓練速度。另外也有些用途,比如圖片壓縮(主要是用SVD,也可以用PCA來做)、因…

thinkphp5記錄

ThinkPHP5 隱藏index.php問題 thinkphp模板輸出cookie,session中… 轉載于:https://www.cnblogs.com/niuben/p/10056049.html

portainer容器可視化管理部署簡要筆記

參考鏈接:https://www.portainer.io/installation/ 1、單個宿主機部署in Linux:$ docker volume create portainer_data$ docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer 2、單…

證明您履歷表經驗的防彈五步法

How many times have you gotten the question “Tell me more about your work experience at …” or “Describe an experience when you had to overcome a technical challenge”? Is your answer solid and bullet-proof every single time you have to respond? If no…

2018-2019-1 20165231 實驗四 外設驅動程序設計

博客鏈接:https://www.cnblogs.com/heyanda/p/10054680.html 轉載于:https://www.cnblogs.com/Yhooyon/p/10056173.html

如何安裝pylab:python如何導入matplotlib模塊

pylab是python下挺不錯的一個畫圖模塊,使用也非常簡單,記得Mit的計算機科學及編程導論有節課也是用到了這個工具,但這個工具安裝不象用起來那么方便,小編就圖文全程直播下吧 工具/原料 python2.7.10win10 32位方法/步驟 1缺省狀態…

微信掃描二維碼和瀏覽器掃描二維碼 ios和Android 分別進入不用的提示頁面

實現微信掃描二維碼和瀏覽器掃描二維碼 ios和Android 分別進入不用的提示頁面 而進入商城下載該項目 詳情地址:gitee.com/DuJiaHui123… 1.創建完之后 替換文件里面的ios項目地址和Android地址 2.網頁上線 3.百度搜索 二維碼生成 把上線后的地址生成二維碼 4.可以把…

詳解getchar()函數與緩沖區

1、首先,我們看一下這段代碼: 它的簡單意思就是從鍵盤讀入一個字符,然后輸出到屏幕。理所當然,我們輸入1,輸出就是1,輸入2,輸出就是2。 那么我們如果輸出的是12呢? 它的輸出是1。 這…

windows下python安裝Numpy、Scipy、matplotlib模塊

python 2.7 針對2.7的軟件。numpy :http://sourceforge.net/projects/numpy/files/NumPy/1.8.1/ 下載下面的numpy-1.8.2-win32-superpack-python2.7 scipy: http://sourceforge.net/projects/scipy/files/matplotlib:matplotlib-1.1.0.win32-py2.7 以上都是exe文件&#xff0…

restTemplate使用和踩坑總結

日常工作中肯定會遇到服務之間的調用,尤其是現在都是微服務的架構,所以總結一下restTemplate的最常用的用法以及自己踩過的坑。 restTemplate的使用 restTemplate底層調用的是Execute方法,而Execute底層調用的是doExecute,它是基于…

常見編碼總結

本文總結自:https://blog.csdn.net/zmx729618/article/details/51821024 1. ISO 8859-1 字節數:1 范圍:0-255(編碼范圍是0x00-0xFF),其中0x00-0x7F之間完全和ASCII一致(ASCII是7位編碼&#xff…

啟動一個Java進程

windows版本 startup.bat -------------------------------------------------------- rem --------------------------------------------------------------------------- rem Start SMS Server by zhangjin rem --------------------------------------------------------…

Flask框架從入門到精通之參數配置(二)

知識點: 1、參數配置 一、概況 上一篇我們已經把Flask第一個程序運行起來了,那么這一篇主要講一下Flask參數的配置。 二、配置參數 Flask參數配置方式有很多種,每一種都可以達到結果,在合適的場景選擇合適的配置方式。 配置文件 在…

BP神經網絡python簡單實現

BP神經網絡的原理在網上有很詳細的說明,這里就不打算細說,這篇文章主要簡單的方式設計及實現BP神經網絡,并簡單測試下在恒等計算(編碼)作測試。 BP神經網絡模型圖如下 BP神經網絡基本思想 BP神經網絡學習過程由信息的…

golang的reflection(轉)(一)

2019獨角獸企業重金招聘Python工程師標準>>> 反射reflection 可以大大提高程序的靈活性,使得interface{}有更大的發揮余地反射可以使用TypeOf和ValueOf函數從接口中獲取目標對象信息反射會將匿名字段作為獨立字段(匿名字段的本質)…

idea教程--Maven 骨架介紹

簡單的說,Archetype是Maven工程的模板工具包。一個Archetype定義了要做的相同類型事情的初始樣式或模型。這個名稱給我們提供來了一個一致的生成Maven工程的方式。Archetype會幫助作者給用戶創建Maven工程模板,并給用戶提供生成相關工程模板版本的參數化…

datatables.js 簡單使用--多選框和服務器端分頁

說明:datatables是一款jQuery表格插件。感覺EasyUI的datagrid更易用 內容:多選框和服務器端分頁 緣由:寫這篇博客的原因是datatables的文檔寫的不怎么樣,找東西很麻煩 環境:asp.net mvc , vs2015sqlserver2012 顯示效…

python異常(高級) Exception

異常(高級) Exception 異常回顧:     try-except 語句 捕獲(接收)異常通知,把異常流程變為正常流程     try-finally 語句 執行必須要執行的語句.     raise 語句 發送異常通知,同時進入異常流程     assert 語句 發送AssertionError異常     with 語句 wi…

反射賦值

目前例子為NPOI Excel導入 入庫時調用 var file file1.PostedFile.InputStream;var fileExt System.IO.Path.GetExtension(file1.FileName);IWorkbook workbook;if (fileExt ".xlsx")workbook new XSSFWorkbook(file);elseworkbook new HSSFWorkbook(file);DB.D…

基于PCA(主成分分析)的人臉識別

代碼下載:基于PCA(主成分分析)的人臉識別 人臉識別是一個有監督學習過程,首先利用訓練集構造一個人臉模型,然后將測試集與訓練集進行匹配,找到與之對應的訓練集頭像。最容易的方式是直接利用歐式距離計算測…