Lexikjwtauthenticationbundle: no controller found for /api/login_check

Created on 17 Aug 2015  路  21Comments  路  Source: lexik/LexikJWTAuthenticationBundle

Hi,

i just tried the bundle and followed the instructions in the documentation, but I only get the error, that there is no controller for the route /api/login_check.

Whats the error?

Most helpful comment

You should try to put the main firewall after the 2 other

All 21 comments

Did you add this line in routing.yml :

api_login_check:
   path: /api/login_check

yeah of course...

i set up a new symfony 2.7 with fos userbundle as described in the doku and created one user via cli.
Then followed each step of lexikjwt bundle, but it doesnt work.
I'll give it a new try later, but I'm sure I made every thing correct.

Just to confirm, security.yml is like that :

        login:
            pattern:  ^/api/login
            stateless: true
            anonymous: true
            form_login:
                check_path:               /api/login_check
                success_handler:          lexik_jwt_authentication.handler.authentication_success
                failure_handler:          lexik_jwt_authentication.handler.authentication_failure
                require_previous_session: false

And of course, you emptied your cache (I had the same problem you had, and my security.yml wasn't right, neither did I empty the cache...)

Now I started from scratch and documentated any step :-)

  1. download symfony installer and install it gloablly
sudo curl -LsS http://symfony.com/installer -o /usr/local/bin/symfony
  1. make it executable
sudo chmod a+x /usr/local/bin/symfony
  1. create new symfony project
symfony new api
  1. set up parameters.yml for database connection
  2. install fos-userbundle via composer
composer require friendsofsymfony/user-bundle "~2.0@dev"
  1. activate it in AppKernel.php
   $bundles = array(
           // ...
           new FOS\UserBundle\FOSUserBundle(),
           // ...
       );
  1. create user entity
        namespace AppBundle\Entity;
        use FOS\UserBundle\Model\User as BaseUser;
        use Doctrine\ORM\Mapping as ORM;

        /**
        * @ORM\Entity
        * @ORM\Table(name="fos_user")
        */
        class User extends BaseUser
        {
            /**
            * @ORM\Id
            * @ORM\Column(type="integer")
            * @ORM\GeneratedValue(strategy="AUTO")
            */
            protected $id;

            public function __construct()
            {
                parent::__construct();
            }
        }
  1. configure security.yml
# app/config/security.yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4

            logout:       true
            anonymous:    true

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }
  1. configure bundle in config.yml
# app/config/config.yml
fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: AppBundle\Entity\User
  1. configure routes in routing.yml
# app/config/routing.yml
fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"
  1. create database and update schemo
php app/console doctrine:database:create
php app/console doctrine:schema:update --force
  1. install lexik/jwt-authentication-bundle via composer
composer require "lexik/jwt-authentication-bundle"
  1. activate in AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle(),
    );
}
  1. generate rsa-keys
$ openssl genrsa -out app/var/jwt/private.pem -aes256 4096
$ openssl rsa -pubout -in app/var/jwt/private.pem -out app/var/jwt/public.pem
  1. configure bundle in config.yml
lexik_jwt_authentication:
    private_key_path: %jwt_private_key_path%
    public_key_path:  %jwt_public_key_path%
    pass_phrase:      %jwt_key_pass_phrase%
    token_ttl:        %jwt_token_ttl%
  1. set parameters in parameters.yml (used same pass_phrase as step 13)
    jwt_private_key_path: %kernel.root_dir%/var/jwt/private.pem   # ssh private key path
    jwt_public_key_path:  %kernel.root_dir%/var/jwt/public.pem    # ssh public key path
    jwt_key_pass_phrase:  ''                                      # ssh key pass phrase
    jwt_token_ttl:        86400
  1. edit security.yml to:
# app/config/security.yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4

            logout:       true
            anonymous:    true

        login:
            pattern:  ^/api/login
            stateless: true
            anonymous: true
            form_login:
                check_path:               /api/login_check
                success_handler:          lexik_jwt_authentication.handler.authentication_success
                failure_handler:          lexik_jwt_authentication.handler.authentication_failure
                require_previous_session: false

        api:
            pattern:   ^/api
            stateless: true
            lexik_jwt: ~


    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }
        - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }
  1. edit routing.yml to :
app:
    resource: "@AppBundle/Controller/"
    type:     annotation

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

api_login_check:
   path: /api/login_check
  1. create user and promote him to ROLE_ADMIN via cli
php app/console fos:user:create
php app/console fos:user:promote
  1. test login_check route via curl
curl -X POST http://localhost:8000/api/login_check -d _username=admin -d _password=admin
  1. error message:
            <h1>
                Unable to find the controller for path &quot;/api/login_check&quot;. The route is wrongly configured.
            </h1>

            <div>
                <strong>404</strong> Not Found - <abbr title="Symfony\Component\HttpKernel\Exception\NotFoundHttpException">NotFoundHttpException</abbr>
            </div>

I pushed it into a github repo.
https://github.com/bambamboole/symfony-jwt

You should try to put the main firewall after the 2 other

b盲盲m you are the best :-)

but can you explain why its like that?

Yep, the pattern used for "main" catches everything, the pattern for "api" catches "/api".
"As usual", you should put the "wildcard" (sort of) at the end, after the specific cases ;)

nicolaskern, thank you, your answer helped me a lot, I had the same problem.

Thank you @nicolaskern

thank you @nicolaskern

thank you @nicolaskern

thank you @nicolaskern you're so the best !

@nicolaskern Thank you! This should be added to symfony/lexik-jwt docs!

I'm experiencing the same error - and my main firewall is behind all others. Any other hints?

security:
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        in_memory: { memory: ~ }
    firewalls:
        login:
            pattern:  ^/api/login
            stateless: true
            anonymous: true
            form_login:
                check_path:               /api/login_check
                success_handler:          lexik_jwt_authentication.handler.authentication_success
                failure_handler:          lexik_jwt_authentication.handler.authentication_failure
                require_previous_session: false
        api:
            pattern:   ^/api
            stateless: true
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: true

            # activate different ways to authenticate

            # http_basic: true
            # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate

            # form_login: true
            # https://symfony.com/doc/current/security/form_login_setup.html


    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        # - { path: ^/admin, roles: ROLE_ADMIN }
        # - { path: ^/profile, roles: ROLE_USER }
        - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }

@tvogt For json_login should be sent correct json document. Try
curl -X POST -H "Content-Type: application/json" http://localhost:8000/api/login_check -d '{"username":"admin", "password":"admin"}'

Hi, i am experiencing the same error. I've successfully used this bundle on another project and I had no problem, but this time I get this error and I don't understand why.

Both projects are on Sf 3.4.

Here is my security.yml :

`firewalls:

    login:
        pattern:  ^/api/login
        stateless: true
        anonymous: true
        form_login:
            check_path:               /api/login_check
            success_handler:          lexik_jwt_authentication.handler.authentication_success
            failure_handler:          lexik_jwt_authentication.handler.authentication_failure
            require_previous_session: false

    api:
        pattern:   ^/api
        stateless: true
        guard:
            authenticators:
                - lexik_jwt_authentication.jwt_token_authenticator

    # disables authentication for assets and the profiler, adapt it according to your needs          
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        anonymous: ~
        logout_on_user_change: true
        form_login:
            login_path: backOffice_login
            check_path: backOffice_login
            csrf_token_generator: security.csrf.token_manager
            default_target_path: backOffice_index

        logout:
            path: backOffice_logout
        # activate different ways to authenticate

        # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
        #http_basic: ~

        # https://symfony.com/doc/current/security/form_login_setup.html
access_control: 
# to do : add ip allowed
    - { path: ^/admin,      roles: ROLE_ADMIN}
    - { path: ^/api/login,  roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api,        roles: IS_AUTHENTICATED_FULLY }

`

I also add the route in routing.yml :
api_login_check: path: /api/login_check

My main firewall is after all others. I've cleared the cache and I don't see any typo or misconfiguration in my security.yml... Anyone has an idea ?

Thanks !

Hi @Zooba21 !

I've the same problem. Do you got solve?

Thanks!

Ops!
O get success now. The problem is with Content-Type.
This should work:
curl -X POST -H "Content-Type: application/json" http://localhost/api/login_check -d '{"username":"johndoe","password":"test"}'

@ewertoncode you are the greatest! I thought I'll get crazy while I didn't find your comment!

@nicolaskern You're talking about effectively

Was this page helpful?
0 / 5 - 0 ratings