2

我遇到了子域和 laravel sanctum 的问题。在子域上,我未经授权。它在本地工作得很好。

我已将域添加到 sanctum domain/.env。我还根据有关子域的 laravel 文档设置了 cors/config,但没有运气。网站上有一个 Htpasswd,这会导致问题吗?我没有想法:(。

以前有没有人在使用 htpasswd 时遇到过这种情况?如果您需要一些代码,请随时询问,

编辑*

cors配置

return [

    /*
    |--------------------------------------------------------------------------
    | Cross-Origin Resource Sharing (CORS) Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure your settings for cross-origin resource sharing
    | or "CORS". This determines what cross-origin operations may execute
    | in web browsers. You are free to adjust these settings as needed.
    |
    | To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
    |
    */

    'paths' => ['api/*'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,

];

圣所配置

return [

    /*
    |--------------------------------------------------------------------------
    | Stateful Domains
    |--------------------------------------------------------------------------
    |
    | Requests from the following domains / hosts will receive stateful API
    | authentication cookies. Typically, these should include your local
    | and production domains which access your API via a frontend SPA.
    |
    */

    'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', 'localhost,127.0.0.1')),

    /*
    |--------------------------------------------------------------------------
    | Expiration Minutes
    |--------------------------------------------------------------------------
    |
    | This value controls the number of minutes until an issued token will be
    | considered expired. If this value is null, personal access tokens do
    | not expire. This won't tweak the lifetime of first-party sessions.
    |
    */

    'expiration' => null,

    /*
    |--------------------------------------------------------------------------
    | Sanctum Middleware
    |--------------------------------------------------------------------------
    |
    | When authenticating your first-party SPA with Sanctum you may need to
    | customize some of the middleware Sanctum uses while processing the
    | request. You may change the middleware listed below as required.
    |
    */

    'middleware' => [
        'verify_csrf_token' => App\Http\Middleware\VerifyCsrfToken::class,
        'encrypt_cookies' => App\Http\Middleware\EncryptCookies::class,
    ],

];

.env

APP_NAME="Events"
APP_ENV=local
APP_KEY=base64:COUzIB3fLtC9H7VYX4UdQAx8I2V7Hq9BoeJNjPATwMM=
APP_DEBUG=true
APP_URL=http://events.hiddendomain.com

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=hiddenname
DB_USERNAME=hiddenuser
DB_PASSWORD=hiddenpw

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=cookie
SESSION_LIFETIME=120
SESSION_DOMAIN='.hiddendomain.com'
SANCTUM_STATEFUL_DOMAIN="events.hiddendomain.com"

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

我把它添加到 axios

axios.defaults.withCredentials = true;

调用

getEventDays() {
axios.get(`/api/event/${this.eventSlug}/${this.slugify(this.selectedAgendaType)}/get-days`)
 .then((res) => {
 // Set the days
 this.days = res.data.data;
 // set the selected day to the day id
 this.selectedDay = res.data.data[0].id;
 // get the sessions for the day(selectedDay) e.g api call
 this.getDayWithSessions();
 })
 .catch((err) => console.error(err.response || err));
},

我已经删除了域名,因为我不能透露名称,但原理是一样的。

人们已经要求提供代码,但是没有太多可避难所。该网站不是一个完整的 SPA。我只是在刀片视图中使用 vue 组件。

更新

我已经删除了 htpasswd,但我仍然有这个问题。我可能有 30 个不同的站点,每个站点上的一切都是一样的,并且没有运气让它在子域上工作

更新

我已经通过使用代客服务和设置子域在我的本地主机上复制了这个。发生完全相同的问题!

提前谢谢大家。

4

4 回答 4

1

老兄,您应该删除 Laravel .env 文件中的引号。

SESSION_DOMAIN='.hiddendomain.com' SANCTUM_STATEFUL_DOMAIN="events.hiddendomain.com"

SESSION_DOMAIN=.hiddendomain.com
SANCTUM_STATEFUL_DOMAIN=events.hiddendomain.com

希望它有效。

于 2020-09-06T08:43:34.503 回答
1

您只需要允许顶级域名,Laravel Sanctum 不需要您的子域全名作为允许的有状态域。

在您的情况下,允许 hiddendomain.com 就足够了。

SANCTUM_STATEFUL_DOMAINS=hiddendomain.com

我希望这有帮助。

问候。

于 2020-06-09T17:46:04.520 回答
0

答案很晚,但是您将环境变量键设置为SANCTUM_STATEFUL_DOMAIN,并且键应该是SANCTUM_STATEFUL_DOMAINS. 你少了一个S。

于 2022-02-15T19:32:44.523 回答
-1

对于 SPA 身份验证,您需要EnsureFrontendRequestsAreStateful在内核文件中的 api 中间件顶部添加。然后你必须使用web中间件而不是圣所

于 2020-06-10T05:17:44.113 回答