2017-06-29 96 views
1

我想將symfony的LexikJWTAuthenticationBundle與fosUserBundle集成在一起,我按照說明here但總是收到401錯誤憑證錯誤。symfony LexikJWTAuthenticationBundle錯誤憑證

這裏是我的config.yml文件:

imports: 
    - { resource: parameters.yml } 
    - { resource: security.yml } 
    - { resource: services.yml } 

# Put parameters here that don't need to change on each machine where the app is deployed 
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration 
parameters: 
    locale: en 

framework: 
    #esi: ~ 
    #translator: { fallbacks: ['%locale%'] } 
    translator: ~ 
    secret: '%secret%' 
    router: 
     resource: '%kernel.project_dir%/app/config/routing.yml' 
     strict_requirements: ~ 
    form: ~ 
    csrf_protection: ~ 
    validation: { enable_annotations: true } 
    #serializer: { enable_annotations: true } 
    templating: 
     engines: ['twig'] 
    default_locale: '%locale%' 
    trusted_hosts: ~ 
    session: 
     # https://symfony.com/doc/current/reference/configuration/framework.html#handler-id 
     handler_id: session.handler.native_file 
     save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%' 
    fragments: ~ 
    http_method_override: true 
    assets: ~ 
    php_errors: 
     log: true 

    serializer: 
     enabled: true 

# Twig Configuration 
twig: 
    debug: '%kernel.debug%' 
    strict_variables: '%kernel.debug%' 

# Doctrine Configuration 
doctrine: 
    dbal: 
     driver: pdo_mysql 
     host: '%database_host%' 
     port: '%database_port%' 
     dbname: '%database_name%' 
     user: '%database_user%' 
     password: '%database_password%' 
     charset: UTF8 
     mapping_types: 
      enum: string 
     # if using pdo_sqlite as your database driver: 
     # 1. add the path in parameters.yml 
     #  e.g. database_path: "%kernel.project_dir%/var/data/data.sqlite" 
     # 2. Uncomment database_path in parameters.yml.dist 
     # 3. Uncomment next line: 
     #path: '%database_path%' 

    orm: 
     auto_generate_proxy_classes: '%kernel.debug%' 
     naming_strategy: doctrine.orm.naming_strategy.underscore 
     auto_mapping: true 

# Swiftmailer Configuration 
swiftmailer: 
    transport: '%mailer_transport%' 
    host: '%mailer_host%' 
    username: '%mailer_user%' 
    password: '%mailer_password%' 
    spool: { type: memory } 
fos_user: 
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel' 
    firewall_name: main 
    user_class: AppBundle\Entity\Collaborator 
    from_email: 
     address: [email protected] 
     sender_name: sahnoun MABROUK 

# Nelmio CORS Configuration 
nelmio_cors: 
    defaults: 
     allow_credentials: false 
     allow_origin: ['*'] 
     allow_headers: ['*'] 
     allow_methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'] 
     max_age: 3600 
     hosts: [] 
     origin_regex: false 


fos_rest: 
    serializer: 
     serialize_null: true 
    routing_loader: 
     include_format: false 
    view: 
     view_response_listener: true 
    format_listener: 
     rules: 
      - { path: '^/', priorities: ['json'], fallback_format: 'json' } 
      - { path: '^/login', priorities: ['html'], fallback_format: 'html' } 
      - { path: '^/register', priorities: ['html'], fallback_format: 'html' } 
      - { path: '^/resetting', priorities: ['html'], fallback_format: 'html' } 

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%' 

security.yml:

# To get started with security, check out the documentation: 
# https://symfony.com/doc/current/security.html 
security: 

    # https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded 
    providers: 
     in_memory: 
      memory: ~ 
     fos_userbundle: 
      id: fos_user.user_provider.username 
    encoders: 
     FOS\UserBundle\Model\UserInterface: bcrypt 

    role_hierarchy: 
     ROLE_ADMIN:  ROLE_USER 
     ROLE_SUPER_ADMIN: ROLE_ADMIN 
    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: ~ 
      pattern: ^/ 
      logout: true 
      form_login: 
       provider: fos_userbundle 
       csrf_token_generator: security.csrf.token_manager 
      logout: true 
      anonymous: true 
      # 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 
      #form_login: ~ 

    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 } 

和我添加api_login_check: path: /api/login_check到路由文件。

我已閱讀所有與此錯誤相關的問題,但沒有爲我工作。

任何一個可以幫助我,請

+0

你能請向我們提供兩個額外的信息: *你如何讓HTTP調用? *你是如何整合成功處理程序的? –

+0

我剛剛配置了捆綁包並測試了捆綁功能,就像curl cmd所說的那樣:curl -X POST http:// localhost:8000/api/login_check -d _username = admin -d _password = admin – sahnoun

回答

0

問題解決!只是讓fos_userbundle作爲第一供應商,以檢查從數據庫憑據

security: 

providers: 
    fos_userbundle: 
     id: fos_user.user_provider.username 
    in_memory: 
     memory: ~ 
    ... 
0

你缺少對每條路線各供應商,看看here

security: 
    firewalls: 
     login: 
      ... 
      provider: in_memory 
      ... 

     api: 
      ... 
      provider: jwt 
      ... 
+0

感謝你的重播,但不工作,它會返回一個html響應,看起來像symfony項目的索引頁的代碼! – sahnoun

+0

解決了!我剛剛將fos_userbundle作爲第一個提供程序來檢查數據庫中的憑據 – sahnoun

相關問題