You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony Flex集成LexikJWTAuthenticationBundle 2.4遇/login_check路由配置问题

Fixing the "/login_check" NotFoundHttpException with LexikJWTAuthenticationBundle 2.4 and Symfony Flex

Great news—you don’t need to create a custom controller for /login_check! This endpoint is handled internally by LexikJWTAuthenticationBundle, but your security and routing configurations are missing key pieces that tell Symfony to route requests to the bundle’s built-in handler. Let’s walk through the fixes step by step:

1. Correct Your security.yaml Configuration

Your current login firewall has an incomplete pattern and missing critical settings. Update it to match this full, working setup:

security:
  encoders:
    App\Entity\User:
      algorithm: bcrypt
  providers:
    entity_provider:
      entity:
        class: App\Entity\User
        property: username
    users:
      id: App\Metier\Provider\UserProvider
  firewalls:
    login:
      pattern: ^/login_check  # Targets exactly the login_check endpoint
      stateless: true         # Required for JWT (no session storage)
      anonymous: true         # Allow unauthenticated access to this route
      json_login:
        check_path: /login_check
        success_handler: lexik_jwt_authentication.handler.authentication_success
        failure_handler: lexik_jwt_authentication.handler.authentication_failure
    main:
      pattern: ^/             # Protect all other routes
      stateless: true
      jwt: ~                  # Use JWT authentication for these routes
  access_control:
    - { path: ^/login_check, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, roles: IS_AUTHENTICATED_FULLY }

Key Details:

  • The login firewall explicitly targets /login_check with pattern: ^/login_check.
  • stateless: true ensures Symfony doesn’t create sessions (essential for JWT’s stateless nature).
  • anonymous: true lets unauthenticated users reach the login endpoint to get their token.
  • The json_login section connects the endpoint to the bundle’s success/failure handlers, which generate and return the JWT token.

2. Define the /login_check Route

Symfony needs to recognize the /login_check path as a valid route. Add this to your config/routes.yaml:

login_check:
  path: /login_check
  methods: [POST]  # Only accept POST requests (required for login)

3. Verify the Request Format

When testing, make sure you send a POST request to /login_check with a JSON body containing your user credentials:

{
  "username": "your-username",
  "password": "your-password"
}

4. Clear Your Cache

After making these config changes, clear Symfony’s cache to ensure the updates take effect:

php bin/console cache:clear

Troubleshooting Quick Checks

  • Confirm LexikJWTAuthenticationBundle is enabled in config/bundles.php (Symfony Flex should have added this automatically, but double-check):
    Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle::class => ['all' => true],
    
  • Ensure your custom UserProvider (if using it) correctly returns a valid App\Entity\User object for existing users.

内容的提问来源于stack exchange,提问作者Nirina-aj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:15:41