• 17-12-2022, 23:20:18
    #1
    Herkese Merhabalar,

    Benim bir Django rest-api projesine sahibim. Front tarafında vue js kullandım.
    Vue aracılığıyla istekte bulunurken aşağıdaki hatayı alıyorum.

    Console çıktısı:

    Access to XMLHttpRequest at 'https://api.iyziwell.com/api/user/login/' from origin 'https://main.d398abgajqt044.amplifyapp.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request


    proje/settings.py


    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'webpack_loader',
    'account',
    'rest_framework',
    'rest_framework.authtoken',
    'rest_framework_simplejwt',
    'corsheaders',
    ]

    MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddlew are',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMidd leware',
    'django.contrib.messages.middleware.MessageMiddlew are',
    'django.middleware.clickjacking.XFrameOptionsMiddl eware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ]

    CORS_ORIGIN_ALLOW_ALL_ORIGINS = True
    CORS_ALLOW_CREDENTIALS = True
    CORS_ALLOWED_ORIGINS = [ 'https://main.d398abgajqt044.amplifyapp.com', 'http://localhost:8082',
    ]
    CORS_ALLOWED_ORIGIN_REGEXES = [ 'https://main.d398abgajqt044.amplifyapp.com', 'http://localhost:8082',
    ]
    CORS_ALLOW_HEADERS = ( 'accept', 'accept-encoding', 'authorization', 'content-type', 'dnt', 'origin', 'user-agent', 'x-csrftoken', 'x-requested-with', )


    proje/urls.py

    urlpatterns = [
    path('admin/', admin.site.urls),
    path('', TemplateView.as_view(template_name='index.html'), name='index'),
    path('api/client/',include('client.api.urls')),
    path('api/user/',include('account.api.urls')),
    path('api/appointment/',include('appointment.api.urls')),
    path('api/user/login', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/user/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
    path('api/password-reset/', include('django_rest_passwordreset.urls', namespace='password-reset')),

    ]


    istek atan sayfa = https://main.d398abgajqt044.amplifyapp.com

    istek alan sayfa = https://api.iyziwell.com/api/user/login/

    Postman ile rahatça istek atabiliyorum lakin vue üzerinden istek atamıyorum.
  • 18-12-2022, 12:39:45
    #2
    CSRF_TRUSTED_ORIGINS = [
    "http://change.allowed.com",
    ]
  • 18-12-2022, 13:20:51
    #3
    aktan adlı üyeden alıntı: mesajı görüntüle
    CSRF_TRUSTED_ORIGINS = [
    "http://change.allowed.com",
    ]

    Hocam dediğinizi denedim lakin sonuç yine aynı. Nginxden de izinleri verdirttim. Hiçbir türlü istek kabul etmiyor
  • 18-12-2022, 15:32:29
    #4
    from corsheaders.defaults import default_headers
    
    ALLOWED_HOSTS = ['*']
    
    USE_X_FORWARDED_HOST = True
    
    CORS_ORIGIN_WHITELIST = (
        "http://localhost:8082",
    "https://main.d398abgajqt044.amplifyapp.com",
    )
    CSRF_TRUSTED_ORIGINS = CORS_ORIGIN_WHITELIST
    CORS_ALLOWED_ORIGINS = CORS_ORIGIN_WHITELIST
    
    CORS_ALLOW_HEADERS = list(default_headers) + [
        "Özel-Header", 
        "authorization"
    ]
    
    MIDDLEWARE = (
    'django.contrib.sessions.middleware.SessionMiddleware',  # Bunun Altına corsheaders ekle
    'corsheaders.middleware.CorsMiddleware',
    )
    Bu Configleri dener misin?
  • 18-12-2022, 15:39:55
    #5
    Brave mi kullanıyorsunuz?
  • 18-12-2022, 17:54:12
    #6
    aktan adlı üyeden alıntı: mesajı görüntüle
    from corsheaders.defaults import default_headers
    
    ALLOWED_HOSTS = ['*']
    
    USE_X_FORWARDED_HOST = True
    
    CORS_ORIGIN_WHITELIST = (
        "http://localhost:8082",
    "https://main.d398abgajqt044.amplifyapp.com",
    )
    CSRF_TRUSTED_ORIGINS = CORS_ORIGIN_WHITELIST
    CORS_ALLOWED_ORIGINS = CORS_ORIGIN_WHITELIST
    
    CORS_ALLOW_HEADERS = list(default_headers) + [
        "Özel-Header",
        "authorization"
    ]
    
    MIDDLEWARE = (
    'django.contrib.sessions.middleware.SessionMiddleware',  # Bunun Altına corsheaders ekle
    'corsheaders.middleware.CorsMiddleware',
    )
    Bu Configleri dener misin?


    Hocam denedim olmadı da bu sorunların Vue den kaynaklı olma ihtimali var mı?




    arkadaş bu kodları yazmış

    sunucuda ise https://enable-cors.org/server_nginx.html ayarları yaptırdık
  • 18-12-2022, 17:54:39
    #7
    DieAnotherDay adlı üyeden alıntı: mesajı görüntüle
    Brave mi kullanıyorsunuz?

    Hayır chrome, opera hocam
  • 18-12-2022, 18:48:36
    #8
    Vue da Accept ve content type olması yeterli diger istekleri ngnix engelleyebilir. Çünkü allow origin axios veya fetch ile request edilirse ngnix güvenlik sebebiyle engeller. Ngnix aradan çıkarıp sunucuda ki vue ile kendi local Django ya istekte bulubursan çalışıp çalışmadığını anlarsın.
  • 18-12-2022, 21:02:36
    #9
    aktan adlı üyeden alıntı: mesajı görüntüle
    Vue da Accept ve content type olması yeterli diger istekleri ngnix engelleyebilir. Çünkü allow origin axios veya fetch ile request edilirse ngnix güvenlik sebebiyle engeller. Ngnix aradan çıkarıp sunucuda ki vue ile kendi local Django ya istekte bulubursan çalışıp çalışmadığını anlarsın.

    hocam localde test ettim "OPTIONS /api/user/login/ HTTP/1.1" 200 0 dönüyor yani bu yanlışım yok ise sorunun vue tarafından kaynaklı olduğu dimi ben şahsen biraz böyle yorumladım ama