简体   繁体   English

Laravel Sanctum 授权问题

[英]Laravel Sanctum Auth issue

I am having an issue with a sub domain and laravel sanctum.我在子域和 laravel 密室方面遇到问题。 On the sub domain I am getting unauthorised.在子域上,我未获得授权。 It is working just fine locally.它在本地工作得很好。

I have added the domain to sanctum domain/.env.我已将域添加到 sanctum domain/.env。 I have also setup the cors/config as per laravel documentation regarding sub domains and no luck.我还根据 laravel 有关子域的文档设置了 cors/config,但没有成功。 There is a Htpasswd on the site, could that be causing the issue?网站上有一个 Htpasswd,这会导致问题吗? I am out of ideas:(.我没有想法:(。

Has anyone experienced this before when using htpasswd?在使用 htpasswd 之前有没有人遇到过这种情况? If you need some code please feel free to ask,如果您需要一些代码,请随时询问,

EDIT*编辑*

cors config 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,

];

sanctum config圣所配置

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 .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}"

Iv'e add this to axios我将其添加到 axios

axios.defaults.withCredentials = true;

Vue call视图调用

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));
},

I have removed the domain name as I can't reveal the name but the principle will be the same.我已经删除了域名,因为我不能透露名称,但原则是一样的。

People have asked for code however there isn't much to sanctum.人们已经要求提供代码,但是密室并不多。 The site isn't a full SPA.该站点不是完整的 SPA。 Im just using a vue component in blade view.我只是在刀片视图中使用 vue 组件。

UPDATE更新

I have since removed the htpasswd and I still have the issue.我已经删除了 htpasswd,但问题仍然存在。 I have possible 30 different sites and everything is the same on every site and with no luck on getting it to work on a sub domain我可能有 30 个不同的网站,每个网站上的一切都是一样的,并且没有运气让它在子域上工作

UPDATE更新

I have since replicated this on my localhost by using valet and setting up a sub domain.从那以后,我通过使用 valet 并设置子域在我的本地主机上复制了它。 The exact same issue occurs!出现完全相同的问题!

Thanks all in advance.提前谢谢大家。

You need to allow the top domain name only, Laravel Sanctum does not need your subdomain full name as allowed stateful domain.您只需要允许顶级域名,Laravel Sanctum 不需要您的子域全名作为允许的有状态域。

In your case, allowing the hiddendomain.com is enough.在您的情况下,允许 hiddendomain.com 就足够了。

SANCTUM_STATEFUL_DOMAINS=hiddendomain.com

I hope this helps.我希望这有帮助。

Regards.问候。

Dude you should remove the quotes in the Laravel.env file.老兄,您应该删除 Laravel.env 文件中的引号。

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

to

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

Hope it works.希望它有效。

Very late answer but you had the environment variable key set as SANCTUM_STATEFUL_DOMAIN and the key is supposed to be SANCTUM_STATEFUL_DOMAINS .答案很晚,但是您将环境变量键设置为SANCTUM_STATEFUL_DOMAIN并且键应该是SANCTUM_STATEFUL_DOMAINS You're missing an S.你少了一个S。

for the SPA authentication you need to add EnsureFrontendRequestsAreStateful at the top of your api middleware in kernel file.对于 SPA 身份验证,您需要在 kernel 文件中的 api 中间件顶部添加EnsureFrontendRequestsAreStateful Then you have to use web middleware not sanctum然后你必须使用web中间件而不是 sanctum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM