Magento 2 Experten — Hyvä Theme, Tailwind CSS & SEO aus einer Hand ›

Login-Endpunkt und Token erhalten

Login-Endpunkt und Token erhalten

~13 Min. Lesezeit Zuletzt aktualisiert am 8. August 2026

Der Login-Endpunkt /api/login ist BEREITS TEIL der api-platform-Distribution – NUR die security.yaml muss ihn korrekt AKTIVIEREN.

security.yaml konfigurieren

api/config/packages/security.yaml
security:
    password_hashers:
        App\Entity\User: 'auto'
    providers:
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email
    firewalls:
        login:
            pattern: ^/api/login
            stateless: true
            json_login:
                check_path: /api/login
                username_path: email
                password_path: password
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure
        api:
            pattern: ^/api
            stateless: true
            jwt: ~
    access_control:
        - { path: ^/api/login, roles: PUBLIC_ACCESS }
        - { path: ^/api/docs, roles: PUBLIC_ACCESS }
        - { path: ^/api, roles: IS_AUTHENTICATED_FULLY }

ZWEI SEPARATE Firewalls: login für den TOKEN-Erhalt (per E-Mail/Passwort), api für ALLE anderen Endpunkte (per JWT-Token). stateless: true ist ENTSCHEIDEND – KEINE Server-Session, JEDER Request trägt SEINE EIGENE Authentifizierung.

Einen Nutzer anlegen

curl -k -X POST https://localhost/api/users \
  -H 'Content-Type: application/json' \
  -d '{"email": "max@example.com", "plainPassword": "sicheres-passwort-123"}'

Ein Token anfordern

curl -k -X POST https://localhost/api/login \
  -H 'Content-Type: application/json' \
  -d '{"email": "max@example.com", "password": "sicheres-passwort-123"}'
{
  "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE3NTQ1..."
}

Achtung: Beachten: json_login erwartet password_path: password – NICHT plainPassword. Der Login-Endpunkt ist KEIN API-Platform-Endpunkt und kennt daher die Serialisierungsgruppen aus Kapitel 47 GAR NICHT.

Falsche Anmeldedaten

curl -k -i -X POST https://localhost/api/login \
  -H 'Content-Type: application/json' \
  -d '{"email": "max@example.com", "password": "falsch"}'

Status 401 Unauthorized – KEIN Hinweis darauf, OB die E-Mail existiert oder NUR das Passwort falsch ist. Diese ABSICHTLICHE Ungenauigkeit verhindert, dass Angreifer registrierte E-Mail-Adressen ERRATEN können.

Tipp: Das erhaltene Token in einer Umgebungsvariable zwischenspeichern (TOKEN=$(curl ... | jq -r .token)) erspart in den kommenden Kapiteln, es bei JEDEM curl-Aufruf neu einzufügen.