Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将DRF与其他身份验证后端一起使用_Python_Django_Django Rest Framework_Keycloak - Fatal编程技术网

Python 将DRF与其他身份验证后端一起使用

Python 将DRF与其他身份验证后端一起使用,python,django,django-rest-framework,keycloak,Python,Django,Django Rest Framework,Keycloak,我正在使用KeyClove作为我的项目的IAM,这是一个多服务项目。 我想为我的django服务使用django rest框架。 我已成功使身份验证工作并请求。user从django.contrib.auth.models返回一个user实例,问题是drf为视图创建了一个新的请求对象,并且在authentication\u BACKENDS中设置的通过KeyClope backend身份验证的用户丢失, 为了解决这个问题,我创建了如下权限类: class ClientRoleBasedPermi

我正在使用KeyClove作为我的项目的IAM,这是一个多服务项目。
我想为我的django服务使用django rest框架。 我已成功使身份验证工作并请求。user从django.contrib.auth.models返回一个user实例,问题是drf为视图创建了一个新的请求对象,并且在authentication\u BACKENDS中设置的通过KeyClope backend身份验证的用户丢失, 为了解决这个问题,我创建了如下权限类:

class ClientRoleBasedPermission(permissions.BasePermission):
    role = "create_room"

    def has_permission(self, request, view):
        if self.role in request._request.user.get_all_permissions():
            return True
        return False
我使用这个简单的视图来测试东西

class ListUsers(APIView):
    """
    View to list all users in the system.

    * Requires token authentication.
    * Only admin users are able to access this view.
    """
    permission_classes = [CreateRoomPermission]
    permission = "create_room"

    def get(self, request, format=None):
        """
        Return a list of all users.
        """
        usernames = [user.username for user in User.objects.all()]
        return Response(usernames)
我正在使用的身份验证后端:


有没有更好的方法来解决此问题?

显示您的
默认\u身份验证\u类的源代码
更新了到身份验证后端的链接