用Django和mod_wsgi設定SVN基本的授權認證
Consider this use case:
You have Django web application up and running on Apache and ‘mod_wsgi’. You plan to host Subversion repository on the same server and want it to be accessible to your web application users.
By default, Subversion will use ‘htpasswd’ file to authenticate users. So you will need to maintain two sets of users – one in ‘Django’ database and the other in Subversion ‘htpasswd’ file. You also need to keep them in sync so that the users have the same password and username for Subversion as well for logging in the web application.
Does that look like a lot of work to do? Is there a better way to do this? Yes, there is!
You can configure Apache to use Django’s authentication mechanism to authenticate users.
The ‘mod_wsgi’ comes to your rescue and makes things a lot easier for you.
Firstly, you need to define a ‘wsgi’ authentication handler script. which will tell Apache to use Django’s authentication for users accessing a Subversion repository.
import sys
import os
from django.contrib.auth.models import User
sys.path[0:0] = [
'/usr/lib/python/site-packages',
'/srv/src/app',
]
os.environ['DJANGO_SETTINGS_MODULE']='apphandler.settings'
from django.conf import settings
def check_password(environ, username, password):
user = None
try :
user = User.objects.get(**kwargs)
except :
return None
return user.check_password(password)
You have Django web application up and running on Apache and ‘mod_wsgi’. You plan to host Subversion repository on the same server and want it to be accessible to your web application users.
By default, Subversion will use ‘htpasswd’ file to authenticate users. So you will need to maintain two sets of users – one in ‘Django’ database and the other in Subversion ‘htpasswd’ file. You also need to keep them in sync so that the users have the same password and username for Subversion as well for logging in the web application.
Does that look like a lot of work to do? Is there a better way to do this? Yes, there is!
You can configure Apache to use Django’s authentication mechanism to authenticate users.
The ‘mod_wsgi’ comes to your rescue and makes things a lot easier for you.
Firstly, you need to define a ‘wsgi’ authentication handler script. which will tell Apache to use Django’s authentication for users accessing a Subversion repository.
CODE:
#! /usr/local/bin/pythonimport sys
import os
from django.contrib.auth.models import User
sys.path[0:0] = [
'/usr/lib/python/site-packages',
'/srv/src/app',
]
os.environ['DJANGO_SETTINGS_MODULE']='apphandler.settings'
from django.conf import settings
def check_password(environ, username, password):
user = None
try :
user = User.objects.get(**kwargs)
except :
return None
return user.check_password(password)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-739609/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Django(59)驗證和授權Django
- Django 使用者認證系統:基本設定Django
- 授權(Authorization)和認證(Authentication)
- 認證授權
- shiro授權和認證(四)
- 細說API - 認證、授權和憑證API
- 認證授權的設計與實現
- 認證授權方案之授權初識
- 【認證與授權】Spring Security的授權流程Spring
- 認證授權:IdentityServer4 - 各種授權模式應用IDEServer模式
- 認證授權方案之JwtBearer認證JWT
- 認證授權方案之授權揭祕 (上篇)
- SpringSecurity認證和授權流程詳解SpringGse
- Ocelot(四)- 認證與授權
- OAuth 2.0 授權碼認證OAuth
- puppet自動認證授權
- Ceph配置與認證授權
- 【認證與授權】2、基於session的認證方式Session
- Spring Security OAuth2.0認證授權四:分散式系統認證授權SpringOAuth分散式
- 認證/授權與許可權的問題
- Java Web系列:JAAS認證和授權基礎JavaWeb
- 認證授權問題概覽
- OAuth 2.0 授權認證詳解OAuth
- EMQX Cloud 更新:外部認證授權MQCloud
- 認證授權:IdentityServer4IDEServer
- 認證授權:學習OIDC
- 安全測試之認證授權
- DRF比Django的認證和許可權高在哪裡Django
- ASP.NET Core 6.0 新增 JWT 認證和授權ASP.NETJWT
- EMQX Cloud 更新:新增 Redis 和 JWT 外部認證授權MQCloudRedisJWT
- JAAS中的認證與授權問題
- 認證授權:學習OAuth協議OAuth協議
- OAuth2.0認證授權workflow探究OAuth
- 【認證與授權】Spring Security系列之認證流程解析Spring
- Django REST framework中認證和許可權的使用方法DjangoRESTFramework
- 聊聊常見的服務(介面)認證授權
- 微服務下認證授權框架的探討微服務框架
- 07.Django中的自定義認證方式和許可權的設計與使用Django