我们在 Django 前面使用 nginx,带有客户端证书检查。NGINX 执行 SSL 终止、客户端证书验证和检查吊销列表。客户端证书字段在标头变量中传递到 django 应用程序。
那么我们的 django 应用程序没有收到证书,它只是查看头变量。我认为同样的机制也适用于 Apache。
对于访问开发服务器的客户端(例如'./manage.py runserver'),我们只是在客户端有一个特殊情况。python客户端示例:
if (proto == "https"):
conn = http.client.HTTPSConnection( "cert."+webhost+":"+port,
key_file = certfile, cert_file = certfile)
headers = {}
else:
# fake client for local connections. pass cert info in headers, as it would come
# out of nginx
conn = http.client.HTTPConnection( webhost+":"+port)
headers = { 'X_SSL_CLIENT_S_DN':'/C=US/ST=California/O=yyyy/CN=zzzz',
'X_SSL_CLIENT_I_DN':'/C=US/ST=California/O=xxxx/CN=wwww',
'X_SSL_CLIENT_SERIAL':hex(serialnum),
'USER_AGENT':"test client user agent",}
对于单元测试,我们使用 Django 测试客户端做同样的事情:
from django.test.client import Client
self.client = Client()
response = self.client.get(url, data,
**{
'HTTP_X_SSL_CLIENT_S_DN':'/C=US/ST=California/O=yyyy/CN=zzzz',
'HTTP_X_SSL_CLIENT_I_DN':'/C=US/ST=California/O=xxxx/CN=wwww',
'HTTP_X_SSL_CLIENT_SERIAL':hex(serialnum),
'HTTP_USER_AGENT':"test client user agent",
})