2017-07-31 16 views
6

मेरे पास फ्लास्क-उपयोगकर्ता के साथ वेब पेज की ओर रखी गई उपयोगकर्ता भूमिकाओं का एक सेट है और संरक्षित है। अब मैं उन्हें अपने एपीआई में आरईएसटी कॉल करने की अनुमति देना चाहता हूं जिन्हें अनुरोधों को फ़िल्टर करने के लिए @roles_required का उपयोग करके विभाजित किया जाएगा। काम करने के लिए @roles_required के लिए फ्लास्क-उपयोगकर्ता को आरईएसटी लॉगिन और पास टोकन \ credentials कैसे करें?फ्लास्क-उपयोगकर्ता प्रबंधन के साथ फ्लास्क-रीस्टफुल की रक्षा कैसे करें?

उत्तर

3

आपको Dillon Dixan की रिपॉजिटरी जांचनी होगी, जहां उसने एक बहुत ही सुंदर उदाहरण का प्रस्ताव दिया था, जो आपकी क्वेरी को प्राप्त करने में आपकी मदद कर सकता है।

from flask import Flask 
from flask_basic_roles import BasicRoleAuth 
app = Flask(__name__) 
auth = BasicRoleAuth() 

# Let's add some users. 
auth.add_user(user='bob', password='secret123', roles='producer') 
auth.add_user(user='alice', password='drowssap', roles=('producer','consumer')) 
auth.add_user(user='bill', password='54321') 
auth.add_user(user='steve', password='12345', roles='admin') 

# Only producers and admins can post, while consumers can only get. 
# Admins can also perform all other verbs. 
@app.route("/task") 
@auth.require(roles={ 
    'POST': 'producer', 
    'GET': 'consumer', 
    'DELETE,POST,PATCH,PUT,GET': 'admin' 
}) 
def tasks_endpoint(methods=(...)): 
    return "Here tasks get produced and consumed!" 

# We can secure by user too. Steve can use any verb on this 
# endpoint and everyone else is denied access. 
@app.route("/task_status") 
@auth.require(users='steve') 
def task_status_endpoint(methods=(...)): 
    return "Here are the task statuses!" 

# Alice, Bill and users with an 'admin' role can access this, while everyone 
# else is denied on all verbs. 
@app.route("/task_failures") 
@auth.require(users=('alice', 'bill'), roles='admin') 
def task_failures(methods=(...)): 
    return "Here are the task failures!" 

# Everyone including unauthenticated users can view task results. 
@app.route("/task_results") 
def task_results(methods=(...)): 
    return "Here are the task results!" 

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

तुम सब करने की ज़रूरत पुस्तकालय flask_basic_rolespip का उपयोग कर स्थापित है: यहाँ नमूना कोड है। आराम आप उदाहरण में जांच सकते हैं और निश्चित रूप से आपकी मदद करेंगे।

इसके अलावा, आप भी जाएँ और देख सकते हैं: https://github.com/raddevon/flask-permissions
कृपया यहां से कुप्पी अनुमति पढ़ें: https://pythonhosted.org/Flask-Security/

संबंधित मुद्दे