令牌身份验证在localhost中正常工作,但在ubuntu服务器(Djangorestframework)上不起作用

问题描述 投票:0回答:2

我在django中编写了一个服务器,并且在django rest框架中制作了一个api。所有api在本地主机上都可以正常工作,但是当我将项目上传到ubuntu服务器时,其令牌身份验证将无法正常工作。它总是给

{"detail":"Authentication credentials were not provided."}

我的所有代码在locallost上运行都很好,但是在ubuntu服务器上却给我一个错误。

views.py

class ManageUserView(generics.RetrieveUpdateAPIView):
    serializer_class = serializers.UserSerializer
    authentication_classes = (TokenAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get_object(self):
        return self.request.user

Serializers.py *

class UserSerializer(serializers.ModelSerializer):
    """ Serializer for the users object """

    class Meta:
        model = get_user_model()
        fields = ('id', 'email', 'password', 'user_type')
        extra_kwargs = {'password': {'write_only': True, 'min_length': 8}}

    def create(self, validated_data):
        """ Create a new user with encrypted password and return it"""
        print(validated_data)
        return get_user_model().objects.create_type_user(**validated_data)

    def update(self, instance, validated_data):
        """ Update a user, setting the password correctly and return it """
        password = validated_data.pop('password', None)
        user = super().update(instance, validated_data)

        if password:
            user.set_password(password)
            user.save()

        return user
django django-rest-framework django-views django-templates
2个回答
0
投票

默认情况下,使用mod_wsgi时,Apache不会传递Authorization标头。您需要在服务器或虚拟主机配置中设置WSGIPassAuthorization

WSGIPassAuthorization On

https://www.django-rest-framework.org/api-guide/authentication/#apache-mod_wsgi-specific-configuration

https://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIPassAuthorization.html


0
投票

这是我的(点conf)文件

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.


    ServerName www.shabber.tech
    ServerAdmin [email protected]
    ServerAlias shabber.tech
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf

    Alias /static /home/shabber/apna/static
    <Directory /home/shabber/apna/static>
        Require all granted
    </Directory>

    Alias /media /home/shabber/apna/media
    <Directory /home/shabber/apna/media>
        Require all granted
    </Directory>

    <Directory /home/shabber/apna/apna>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIScriptAlias / /home/shabber/apna/apna/wsgi.py
    WSGIDaemonProcess apna_app python-path=/home/shabber/apna python-home=/home/shabber/apna/venv
    WSGIProcessGroup apna_app
    WSGIPassAuthorization On

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
© www.soinside.com 2019 - 2024. All rights reserved.