使用 flask_pymongo 时身份验证失败
Posted
技术标签:
【中文标题】使用 flask_pymongo 时身份验证失败【英文标题】:Authentication failed when using flask_pymongo 【发布时间】:2016-10-23 01:14:04 【问题描述】:我确信 MONGO_URI 是正确的,因为它在 pymongo 中运行良好。 但是当使用flask_pymongo时,它不起作用...... 这是我的测试代码:
from flask import Flask
from flask_pymongo import PyMongo
app = Flask(__name__)
app.config['MONGO_URI'] ='mongodb://root:aaa2016@localhost:27017/mongo_test'
mongo = PyMongo(app, config_prefix='MONGO')
@app.route('/')
def hello_world():
mongo.db.user.insert('username': "aaa")
return 'Hello World!'
if __name__ == '__main__':
app.run()
运行它并访问127.0.0.1:5000,给出500的响应..
OperationFailure: Authentication failed.
127.0.0.1 - - [21/Jun/2016 20:40:25] "GET / HTTP/1.1" 500 -
任何帮助将不胜感激。
更新: 这是回溯:
File "C:\Python27\lib\site-packages\flask\app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python27\lib\site-packages\flask\app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "F:\PycharmProjects\flask_\flask_.py", line 12, in hello_world
mongo.db.user.insert('username': "aaa")
File "C:\Python27\lib\site-packages\pymongo\collection.py", line 2203, in insert
with self._socket_for_writes() as sock_info:
File "C:\Python27\lib\contextlib.py", line 17, in __enter__
return self.gen.next()
File "C:\Python27\lib\site-packages\pymongo\mongo_client.py", line 718, in _get_socket
with server.get_socket(self.__all_credentials) as sock_info:
File "C:\Python27\lib\contextlib.py", line 17, in __enter__
return self.gen.next()
File "C:\Python27\lib\site-packages\pymongo\server.py", line 152, in get_socket
with self.pool.get_socket(all_credentials, checkout) as sock_info:
File "C:\Python27\lib\contextlib.py", line 17, in __enter__
return self.gen.next()
File "C:\Python27\lib\site-packages\pymongo\pool.py", line 541, in get_socket
sock_info.check_auth(all_credentials)
File "C:\Python27\lib\site-packages\pymongo\pool.py", line 306, in check_auth
auth.authenticate(credentials, self)
File "C:\Python27\lib\site-packages\pymongo\auth.py", line 436, in authenticate
auth_func(credentials, sock_info)
File "C:\Python27\lib\site-packages\pymongo\auth.py", line 416, in _authenticate_default
return _authenticate_scram_sha1(credentials, sock_info)
File "C:\Python27\lib\site-packages\pymongo\auth.py", line 188, in _authenticate_scram_sha1
res = sock_info.command(source, cmd)
File "C:\Python27\lib\site-packages\pymongo\pool.py", line 213, in command
read_concern)
File "C:\Python27\lib\site-packages\pymongo\network.py", line 99, in command
helpers._check_command_response(response_doc, None, allowable_errors)
File "C:\Python27\lib\site-packages\pymongo\helpers.py", line 196, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
OperationFailure: Authentication failed.
【问题讨论】:
你能告诉我们你用来确认它在直接使用 PyMongo 时有效的代码吗?import pymongo
connection = pymongo.MongoClient('mongodb://root:aaa2016@localhost:27017/')
db = connection.mongo_test
db.user.insert('username': "aaa")
效果很好........@dim
请edit您的原始问题并确保包含所有相关代码。导入 PyMongo 并不足以确认您可以连接到数据库。
这里有我所有的测试代码,和youtube@dirn给我的视频几乎一样
【参考方案1】:
问题是,如果您使用MONGO_URI
配置参数,那么 pymongo 会尝试根据字符串中包含的数据库名称进行身份验证。您应该将配置参数分解为以下内容,以便您可以指定数据库名称和身份验证源。
app.config['MONGO_HOST'] = 'localhost'
app.config['MONGO_PORT'] = '27017'
app.config['MONGO_DBNAME'] = 'mongo_test'
app.config['MONGO_USERNAME'] = 'root'
app.config['MONGO_PASSWORD'] = 'aaa2016'
app.config['MONGO_AUTH_SOURCE'] = 'admin' . # root user is typically defined in admin db
【讨论】:
请问为什么MONGO_AUTH_SOURCE
是必不可少的?我执行db.authenticate(name=xx, password=xxx)
,它失败了。但它通过添加source='admin'
成功。
我相信这会让 pymongo 知道哪个 db 表保存了要进行身份验证的凭据。【参考方案2】:
这个问题可能很老,但我遇到了同样的问题,并找到了另一个可能对其他人有用的解决方案。
将?authSource=admin
附加到MONGO_URI
变量的末尾将根据管理员数据库而不是您要连接的数据库验证您的凭据。
示例:
app.config["MONGO_URI"] = "mongodb://username:password@host:port/db_name?authSource=admin"
【讨论】:
【参考方案3】:如果您使用 Flask-MongoEngine 并使用 app.config['MONGODB_*']
设置 mongo 配置,那么您的 uri 将从这些变量生成,例如
app.config['MONGODB_DB'] = 'project1'
app.config['MONGODB_HOST'] = '192.168.1.35'
app.config['MONGODB_PORT'] = 12345
app.config['MONGODB_USERNAME'] = 'webapp'
app.config['MONGODB_PASSWORD'] = 'pwd123'
但是,如果您需要在 uri 中添加一些额外的参数(例如查询参数 authSource=admin
),则必须通过 app.config['MONGODB_SETTINGS']
传递 mongo 配置
app.config['MONGODB_SETTINGS'] =
'connect': False,
'host': 'mongodb://webapp:pwd123@192.168.1.35:12345/project1?authSource=admin'
【讨论】:
以上是关于使用 flask_pymongo 时身份验证失败的主要内容,如果未能解决你的问题,请参考以下文章
使用 Firebase Auth 进行身份验证时,短信验证码请求失败
使用 SendGrid/Gmail 时 SMTP 身份验证失败
Xamarin Forms Android - 使用 MSAL 库部署时身份验证失败