pip install eve python MONGO_HOST = 'localhost' MONGO_PORT = 27017 MONGO_DBNAME = 'Eve' RESOURCE_METHODS = ['GET', 'POST'] ITEM_METHODS = ['GET', 'PATCH', 'DELETE'] DOMAIN = {'people': {}} python from eve import Eve from eve.auth import BasicAuth class MyBasicAuth(BasicAuth): def check_auth(self, username, password, allowed_roles, resource, method): if username == 'admin' and password == 'admin': return True else: return False app = Eve(auth=MyBasicAuth) if __name__ == '__main__': app.run() python from eve import Eve from eve.auth import BasicAuth from functools import wraps from flask import abort class MyBasicAuth(BasicAuth): def check_auth(self, username, password, allowed_roles, resource, method): class SecureResource: def __init__(self, allowed_roles): self.allowed_roles = allowed_roles def check_auth_decorator(self, f): @wraps(f) def decorated(*args, **kwargs): if not app.auth.check_auth(self.allowed_roles, request.method): abort(401) return f(*args, **kwargs) return decorated app = Eve(auth=MyBasicAuth) people = SecureResource(['admin']) app.register_resource('people', people) if __name__ == '__main__': app.run()


上一篇:
下一篇:
切换中文