第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

來自 Chrome 擴展的獲取請求導(dǎo)致 403 Forbidden

來自 Chrome 擴展的獲取請求導(dǎo)致 403 Forbidden

慕絲7291255 2023-03-03 15:39:22
每當(dāng)我嘗試從background.js我的 Chrome 擴展程序發(fā)送 POST 請求時,我都會收到403 Forbidden錯誤消息。如果我在我的 Chrome 擴展之外執(zhí)行相同的代碼,它會正常工作。我的 API 不需要任何身份驗證。請求代碼:let formData = new FormData();formData.append('message', "This is a test message.");fetch('https://myapi.com/add', {    body: formData,    method: "post"}).then(r => console.log(r));請求響應(yīng):我還檢查了我的 Apache 2 access.log,一切看起來都很正常:x.x.x.x - - [13/Sep/2020:17:02:30 +0000] "POST add HTTP/1.1" 403 581 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"是否有任何 Chrome 擴展程序政策可以阻止我的請求?我需要向我的添加任何特殊權(quán)限嗎manifest.json?我是否需要對我的 API 進行任何更改(將其作為保留,因為它在 Chrome 擴展之外正常工作)?
查看完整描述

3 回答

?
HUH函數(shù)

TA貢獻1836條經(jīng)驗 獲得超4個贊

編輯:閱讀您的服務(wù)器日志。服務(wù)器可能會準(zhǔn)確地告訴您錯誤是什么。確保啟用調(diào)試。

沒有充分理由,403 錯誤并不是真正從服務(wù)器發(fā)出的——而且這個原因通常特定于您的應(yīng)用程序。403 通常是權(quán)限問題:我們知道你是誰,但你無權(quán)做你想做的事

可能存在(或缺失)標(biāo)頭阻止請求進行任何更改(在您的情況下為 POST 請求)。例如,您的服務(wù)器可能正在設(shè)置 cookie 以防止 CSRF 攻擊。很多時候服務(wù)器不會驗證 GET 請求上的令牌,這可以解釋為什么 GET 請求適用于您的情況但不適用于 POST 請求。

如果你可以搜索你的服務(wù)器代碼,我愿意有這樣的偽代碼:

if (requestIsMissingCSRFToken()) {
  throw new Error(STATUS.FORBIDDEN)
}


查看完整回答
反對 回復(fù) 2023-03-03
?
UYOU

TA貢獻1878條經(jīng)驗 獲得超4個贊

我只是有同樣的問題。非常感謝您發(fā)布此信息。我還在使用 Django 后端開發(fā) Chrome 擴展。

對我來說,從“DEFAULT_AUTHENTICATION_CLASSES”中刪除“rest_framework.authentication.SessionAuthentication”就可以了,但我保留了“oauth2_provider.contrib.rest_framework.OAuth2Authentication”。

根據(jù)這篇中篇文章, “rest_framework.authenication.SessionAuthentication”僅在您想保留可瀏覽的 API 時才需要。我啟用了 CSRF 和 CORS。所以我認(rèn)為我的設(shè)置非常安全。

下面是我的 settings.py 文件。

import os


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))



# Quick-start development settings - unsuitable for production

# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/


# SECURITY WARNING: keep the secret key used in production secret!

SECRET_KEY = '*xbmoy2yt4%l=od-dm*w$dxpl+rb(n#rmv0n&0x$a@+io!j+++'


# SECURITY WARNING: don't run with debug turned on in production!

DEBUG = True


ALLOWED_HOSTS = []



# Application definition


INSTALLED_APPS = [

    'audio.apps.AudioConfig',

    'django.contrib.admin',

    'django.contrib.auth',

    'django.contrib.contenttypes',

    'django.contrib.sessions',

    'django.contrib.messages',

    'django.contrib.staticfiles',

    'accounts.apps.AccountsConfig',

    'rest_framework',

    'oauth2_provider',

    #'corsheaders',

]


MIDDLEWARE = [

    #'corsheaders.middleware.CorsMiddleware',

    'django.middleware.security.SecurityMiddleware',

    'django.contrib.sessions.middleware.SessionMiddleware',

    'django.middleware.common.CommonMiddleware',

    'django.middleware.csrf.CsrfViewMiddleware',

    'django.contrib.auth.middleware.AuthenticationMiddleware',

    'django.contrib.messages.middleware.MessageMiddleware',

    'django.middleware.clickjacking.XFrameOptionsMiddleware',

    'oauth2_provider.middleware.OAuth2TokenMiddleware'

]


ROOT_URLCONF = 'zeno.urls'


TEMPLATES = [

    {

        'BACKEND': 'django.template.backends.django.DjangoTemplates',

        'DIRS': [os.path.join(BASE_DIR, 'templates')],

        'APP_DIRS': True,

        'OPTIONS': {

            'context_processors': [

                'django.template.context_processors.debug',

                'django.template.context_processors.request',

                'django.contrib.auth.context_processors.auth',

                'django.contrib.messages.context_processors.messages',

            ],

        },

    },

]


WSGI_APPLICATION = 'zeno.wsgi.application'

CORS_ORIGIN_ALLOW_ALL = True



# Database

# https://docs.djangoproject.com/en/2.2/ref/settings/#databases


DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.sqlite3',

        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

    }

}



# Password validation

# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators


AUTH_PASSWORD_VALIDATORS = [

    {

        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',

    },

    {

        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',

    },

    {

        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',

    },

    {

        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',

    },

]



# Internationalization

# https://docs.djangoproject.com/en/2.2/topics/i18n/


LANGUAGE_CODE = 'en-us'


TIME_ZONE = 'UTC'


USE_I18N = True


USE_L10N = True


USE_TZ = True


CORS_ORIGIN_ALLOW_ALL = False



# Static files (CSS, JavaScript, Images)

# https://docs.djangoproject.com/en/2.2/howto/static-files/


STATIC_URL = '/static/'

LOGIN_REDIRECT_URL = 'home'

LOGOUT_REDIRECT_URL = 'home'


PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))

STATICFILES_DIRS = (

    os.path.join(PROJECT_ROOT, '..', 'static'),

)


REST_FRAMEWORK = {

    'DEFAULT_AUTHENTICATION_CLASSES': (

        'oauth2_provider.contrib.rest_framework.OAuth2Authentication',

        #'rest_framework.authentication.SessionAuthentication', # To keep the Browsable API

    ),

    'DEFAULT_PERMISSION_CLASSES': (

        'rest_framework.permissions.IsAuthenticated',

    ),

    # 'DEFAULT_PAGINATION_CLASS': (

    #     'rest_framework.pagination.PageNumberPagination',

    # ),

    # 'DEFAULT_PERMISSION_CLASSES': (

    #     'rest_framework.permissions.IsAuthenticated',

    # ),

    #'PAGE_SIZE': 10

}



AUTHENTICATION_BACKENDS = (

    'django.contrib.auth.backends.ModelBackend', # To keep the Browsable API

    'oauth2_provider.backends.OAuth2Backend',

)


#STATIC_ROOT = os.path.join(BASE_DIR, "static/")

ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'test.com']


AUTH_USER_MODEL = 'accounts.CustomUser'


EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

#EMAIL_BACKEND = "django.core.mail.backends.filebased.EmailBackend"

#EMAIL_FILE_PATH = os.path.join(BASE_DIR, "sent_emails")


查看完整回答
反對 回復(fù) 2023-03-03
?
縹緲止盈

TA貢獻2041條經(jīng)驗 獲得超4個贊

問題出在 Django REST 框架中。顯然它默認(rèn)啟用了一些身份驗證類。禁用/將它們設(shè)置為空數(shù)組settings.py將擺脫煩人的“403 Forbidden”錯誤。


REST_FRAMEWORK = {

    'DEFAULT_AUTHENTICATION_CLASSES': [

    ]

}

編輯:我不知道這有多安全,但至少它有效。


查看完整回答
反對 回復(fù) 2023-03-03
  • 3 回答
  • 0 關(guān)注
  • 350 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號