Referencia de configuración de Security

El sistema de seguridad es una de las piezas más poderosas de Symfony2, y en gran medida se puede controlar por medio de su configuración.

Configuración predeterminada completa

La siguiente es la configuración predeterminada para el sistema de seguridad completo. Cada parte se explica en la siguiente sección.

  • YAML
    # app/config/security.yml
    security:
        access_denied_url:    ~ # Ejemplo: /foo/error403
    
        # la estrategia puede ser: none, migrate, invalidate
        session_fixation_strategy:  migrate
        hide_user_not_found:  true
        always_authenticate_before_granting:  false
        erase_credentials:    true
        access_decision_manager:
            strategy:             affirmative
            allow_if_all_abstain:  false
            allow_if_equal_granted_denied:  true
        acl:
    
            # cualquier nombre configurado en la sección doctrine.dbal
            connection:           ~
            cache:
                id:                   ~
                prefix:               sf2_acl_
            provider:             ~
            tables:
                class:                acl_classes
                entry:                acl_entries
                object_identity:      acl_object_identities
                object_identity_ancestors:  acl_object_identity_ancestors
                security_identity:    acl_security_identities
            voter:
                allow_if_object_identity_unavailable:  true
    
        encoders:
            # Ejemplos:
            Acme\DemoBundle\Entity\Usuario1: sha512
            Acme\DemoBundle\Entity\Usuario2:
                algorithm:           sha512
                encode_as_base64:    true
                iterations:          5000
    
            # codificador PBKDF2
            # ve la nota sobre PBKDF2 abajo para detalles sobre seguridad y velocidad
            Acme\Your\Class\Name:
                algorithm:            pbkdf2
                hash_algorithm:       sha512
                encode_as_base64:     true
                iterations:           1000
    
            # Example options/values for what a custom encoder might look like
            Acme\DemoBundle\Entity\User3:
                id:                   my.encoder.id
    
        providers:            # Requerido
            # Ejemplos:
            my_in_memory_provider:
                memory:
                    users:
                        foo:
                            password:           foo
                            roles:              ROLE_USER
                        bar:
                            password:           bar
                            roles:              [ROLE_USER, ROLE_ADMIN]
    
            my_entity_provider:
                entity:
                    class:              SecurityBundle:User
                    property:           username
    
            # Example custom provider
            my_some_custom_provider:
                id:                   ~
    
            # Chain some providers
            my_chain_provider:
                chain:
                    providers:          [ my_in_memory_provider, my_entity_provider ]
    
        firewalls:            # Requerido
            # Ejemplos:
            cualquiernombre:
                pattern: .*
                request_matcher: un.id.de.servicio
                access_denied_url: /foo/error403
                access_denied_handler: un.id.de.servicio
                entry_point: un.id.de.servicio
                provider: some_key_from_above
                # manages where each firewall stores session information
                # See "Firewall Context" below for more details
                context: context_key
                stateless: false
                x509:
                    provider: una_clave_de_arriba
                http_basic:
                    provider: una_clave_de_arriba
                http_digest:
                    provider: una_clave_de_arriba
                form_login:
                    # aquí es dónde se presenta el formulario de acceso
                    check_path: /login_check
    
                    # el usuario es redirigido aquí cuando necesita ingresar
                    login_path: /login
    
                    # si es 'true', reenvía al usuario al formulario de acceso en lugar de redirigirlo
                    use_forward: false
    
                    # opciones para redirigir en ingreso satisfactorio (lee más adelante)
                    always_use_default_target_path: false
                    default_target_path:            /
                    target_path_parameter:          _target_path
                    use_referer:                    false
    
                    # opciones para redirigir en ingreso fallido (lee más adelante)
                    failure_path:    /foo
                    failure_forward: false
                    failure_path_parameter: _failure_path
                    failure_handler: un.id.de.servicio
                    success_handler: some.service.id
    
                    # nombres de campo para el nombre de usuario y contraseña
                    username_parameter: _username
                    password_parameter: _password
    
                    # opciones del elemento csrf
                    csrf_parameter: _csrf_token
                    intention:      authenticate
                    csrf_provider:  my.csrf_provider.id
    
                    # por omisión, el método del formulario de acceso DEBE ser POST, no GET
                    post_only:      true
                    remember_me:    false
                remember_me:
                    token_provider: name
                    key: someS3cretKey
                    name: NameOfTheCookie
                    lifetime: 3600 # en segundos
                    path: /foo
                    domain: undominio.foo
                    secure: false
                    httponly: true
                    always_remember_me: false
                    remember_me_parameter: _remember_me
                logout:
                    path:   /logout
                    target: /
                    invalidate_session: false
                    delete_cookies:
                        a: { path: null, domain: null }
                        b: { path: null, domain: null }
                    handlers: [un.id.de.servicio, otro.id.de.servicio]
                    success_handler: un.id.de.servicio
                anonymous: ~
    
            # Valores y opciones predefinidos para algún cortafuegos
            un_escucha_del_cortafuegos:
                pattern:              ~
                security:             true
                request_matcher:      ~
                access_denied_url:    ~
                access_denied_handler:  ~
                entry_point:          ~
                provider:             ~
                stateless:            false
                context:              ~
                logout:
                    csrf_parameter:       _csrf_token
                    csrf_provider:        ~
                    intention:            logout
                    path:                 /logout
                    target:               /
                    success_handler:      ~
                    invalidate_session:   true
                    delete_cookies:
    
                        # Prototipo
                        name:
                            path:                 ~
                            domain:               ~
                    handlers:             []
                anonymous:
                    key:                  4f954a0667e01
                switch_user:
                    provider:             ~
                    parameter:            _switch_user
                    role:                 ROLE_ALLOWED_TO_SWITCH
    
        access_control:
            requires_channel:     ~
    
            # usa el formato urldecoded
            path:                 ~ # Ejemplo: ^/ruta al recurso/
            host:                 ~
            ip:                   ~
            methods:              []
            roles:                []
        role_hierarchy:
            ROLE_ADMIN:      [ROLE_ORGANIZER, ROLE_USER]
            ROLE_SUPERADMIN: [ROLE_ADMIN]
    

Configurando el formulario de acceso

Cuando usas el escucha de autenticación form_login bajo un cortafuegos, hay varias opciones comunes para experimentar en la configuración del «formulario de acceso»:

Para más información, consulta Cómo personalizar el formulario de acceso.

Procesando el formulario de acceso

  • login_path (tipo: string, predefinido: /login) This is the route or path that the user will be redirected to (unless use_forward is set to true) when he/she tries to access a protected resource but isn’t fully authenticated.

    This path must be accessible by a normal, un-authenticated user, else you may create a redirect loop. Para más información, consulta «Evitando errores comunes».

  • check_path (tipo: string, predefinido: /login_check) This is the route or path that your login form must submit to. The firewall will intercept any requests (POST requests only, by default) to this URL and process the submitted login credentials.

    Asegúrate de que esta dirección está cubierta por el cortafuegos principal (es decir, no crees un servidor de seguridad independiente sólo para la URL check_path).

  • use_forward (tipo: Boolean, predefinido: false) Si deseas que el usuario sea remitido al formulario de acceso en vez de ser redirigido, marca esta opción a true.

  • username_parameter (tipo: string, predefinido: _username) Este es el nombre del campo que debes dar al campo ‘nombre de usuario’ de tu formulario de acceso. Cuando se presenta el formulario a check_path, el sistema de seguridad buscará un parámetro POST con este nombre.

  • password_parameter (tipo: string, predefinido: _password) Este es el nombre de campo que se debe dar al campo de la contraseña de tu formulario de inicio de sesión. Cuando se presenta el formulario a check_path, el sistema de seguridad buscará un parámetro POST con este nombre.

  • post_only (tipo: Boolean, predefinido: true) De forma predeterminada, debes enviar tu formulario de acceso a la URL check_path como una petición POST. Al poner esta opción a false, puedes enviar una petición GET a la URL check_path.

Redirigiendo después del inicio de sesión

  • always_use_default_target_path (tipo: Boolean, predefinido: false)
  • default_target_path (tipo: string, predefinido: /)
  • target_path_parameter (tipo: string, predefinido: _target_path)
  • use_referer (tipo: Boolean, predefinido: false)

Usando el codificador PBKDF2: seguridad y velocidad

Nuevo en la versión 2.2: The PBKDF2 password encoder was added in Symfony 2.2.

El codificador PBKDF2 proporciona seguridad criptográfica de alto nivel, tal como la recomendada por el Instituto Nacional de Estándares y Tecnología (NIST por «National Institute of Standards and Technology»).

You can see an example of the pbkdf2 encoder in the YAML block on this page.

Pero usando PBKDF2 también garantizas un aviso: Usándolo (con un número de iteraciones alto) ralentiza el proceso. Por lo que, PBKDF2 se tendría que utilizar con precaución y muchísimo cuidado.

Una buena configuración está alrededor al menos 1000 iteraciones y sha512 para el algoritmo de codificación.

Using the BCrypt Password Encoder

Nuevo en la versión 2.2: The BCrypt password encoder was added in Symfony 2.2.

  • YAML
    # app/config/security.yml
    security:
        # ...
    
        encoders:
            Symfony\Component\Security\Core\User\User:
                algorithm: bcrypt
                cost:      15
    
  • XML
    <!-- app/config/security.xml -->
    <config>
        <!-- ... -->
        <encoder
            class="Symfony\Component\Security\Core\User\User"
            algorithm="bcrypt"
            cost="15"
        />
    </config>
    
  • PHP
    // app/config/security.php
    $container->loadFromExtension('security', array(
        // ...
        'encoders' => array(
            'Symfony\Component\Security\Core\User\User' => array(
                'algorithm' => 'bcrypt',
                'cost'      => 15,
            ),
        ),
    ));
    

The cost can be in the range of 4-31 and determines how long a password will be encoded. Each increment of cost doubles the time it takes to encode a password.

If you don’t provide the cost option, the default cost of 13 is used.

Nota

You can change the cost at any time — even if you already have some passwords encoded using a different cost. New passwords will be encoded using the new cost, while the already encoded ones will be validated using a cost that was used back when they were encoded.

A salt for each new password is generated automatically and need not be persisted. Since an encoded password contains the salt used to encode it, persisting the encoded password alone is enough.

Nota

All the encoded passwords are 60 characters long, so make sure to allocate enough space for them to be persisted.

Firewall Context

Most applications will only need one firewall. But if your application does use multiple firewalls, you’ll notice that if you’re authenticated in one firewall, you’re not automatically authenticated in another. In other words, the systems don’t share a common “context”: each firewall acts like a separate security system.

However, each firewall has an optional context key (which defaults to the name of the firewall), which is used when storing and retrieving security data to and from the session. If this key were set to the same value across multiple firewalls, the “context” could actually be shared:

  • YAML
    # app/config/security.yml
    security:
        # ...
    
        firewalls:
            cualquiernombre:
                # ...
                context: my_context
            othername:
                # ...
                context: my_context
    
  • XML
    <!-- app/config/security.xml -->
    <security:config>
       <firewall name="somename" context="my_context">
         <! ... ->
       </firewall>
       <firewall name="othername" context="my_context">
         <! ... ->
       </firewall>
    </security:config>
    
  • PHP
    // app/config/security.php
    $container->loadFromExtension('security', array(
         'firewalls' => array(
             'somename' => array(
                 // ...
                 'context' => 'my_context'
             ),
             'othername' => array(
                 // ...
                 'context' => 'my_context'
             ),
         ),
    ));
    

HTTP-Digest Authentication

To use HTTP-Digest authentication you need to provide a realm and a key:

  • YAML
    # app/config/security.yml
    security:
       firewalls:
          cualquiernombre:
            http_digest:
             key: "a_random_string"
             realm: "secure-api"
    
  • XML
    <!-- app/config/security.xml -->
    <security:config>
       <firewall name="somename">
          <http-digest key="a_random_string" realm="secure-api" />
       </firewall>
    </security:config>
    
  • PHP
    // app/config/security.php
    $container->loadFromExtension('security', array(
         'firewalls' => array(
             'somename' => array(
                 'http_digest' => array(
                     'key'   => 'a_random_string',
                     'realm' => 'secure-api',
                 ),
             ),
         ),
    ));
    
Bifúrcame en GitHub