使用预认证的Security防火墙

3.4 版本
维护中的版本

一些web服务器已经可以提供很多的认证模块了,包括Apache。总地说来,这些模块设置了一些环境变量,用来确定哪些用户访问你的程序。Symfony支持大多数认证机制(authentication mechanisms)。这些请求被称为预认证请求(pre authenticated requests),因为用户在抵达你的程序时已经通过身份认证。

模拟用户 不兼容预认证防火墙。因为模拟(impersonation)要求认证状态(authentication state)必须在服务器端保持,而预认证信息(SSL_CLIENT_S_DN_EmailREMOTE_USER 或其他)是在每一次的请求中发送。

X.509客户端证书认证 

当使用客户端证书(client certificates)时,你的web服务器自行处理所有的身份认证进程。例如,在Apache中你可以使用 SSLVerifyClient Require 指令。

在安全配置中开启特定的X509 authentication防火墙:

1
2
3
4
5
6
7
8
9
# app/config/security.yml
security:
    # ...

    firewalls:
        secured_area:
            pattern: ^/
            x509:
                provider: your_user_provider
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- app/config/security.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<srv:container xmlns="http://symfony.com/schema/dic/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:srv="http://symfony.com/schema/dic/services"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        http://symfony.com/schema/dic/services/services-1.0.xsd">
 
    <config>
        <!-- ... -->
 
        <firewall name="secured_area" pattern="^/">
            <x509 provider="your_user_provider" />
        </firewall>
    </config>
</srv:container>
1
2
3
4
5
6
7
8
9
10
11
12
13
// app/config/security.php
$container->loadFromExtension('security', array(
    // ...
 
    'firewalls' => array(
        'secured_area' => array(
            'pattern' => '^/',
            'x509'    => array(
                'provider' => 'your_user_provider',
            ),
        ),
    ),
));

默认时,防火墙把 SSL_CLIENT_S_DN_Email 提供给user provider,并在 PreAuthenticatedToken 里把 SSL_CLIENT_S_DN 设为凭证。通过在X509防火墙中分别配置 usercredentials 选项键可以覆写它们。

authentication provider将只向“发起请求的用户名”的user provider发起通知。你需要创建(或使用)一个由 provider 配置参数(在本例中即 your_user_provider)所引用“user provider”。这个provider将用户名转化为你需要的User对象。关于创建和配置用户user provider的更多信息,参考:

基于REMOTE_USER的身份认证 

许多认证模块,像Apache的 auth_kerb 使用 REMOTE_USER 环境变量来提供用户名。这个变量可以被程序信任,因为认证过程在请求到达之前已经完成。

要使用 REMOTE_USER 环境变量来配置 Symfony ,只需在安全配置中启用相应的防火墙:

1
2
3
4
5
6
7
# app/config/security.yml
security:
    firewalls:
        secured_area:
            pattern: ^/
            remote_user:
                provider: your_user_provider
1
2
3
4
5
6
7
8
9
10
11
<!-- app/config/security.xml -->
<?xml version="1.0" ?>
<srv:container xmlns="http://symfony.com/schema/dic/security"
    xmlns:srv="http://symfony.com/schema/dic/services">
 
    <config>
        <firewall name="secured_area" pattern="^/">
            <remote-user provider="your_user_provider"/>
        </firewall>
    </config>
</srv:container>
1
2
3
4
5
6
7
8
9
10
11
// app/config/security.php
$container->loadFromExtension('security', array(
    'firewalls' => array(
        'secured_area' => array(
            'pattern'     => '^/'
            'remote_user' => array(
                'provider' => 'your_user_provider',
            ),
        ),
    ),
));

防火墙将为用户提供器提供REMOTE_USER环境变量。通过设置防火墙配置中的remote_useruser键你可以改变这个变量的名字。

如同X509 authentication,你需要配置一个user provider,参考 上一小节 以了解更多。

本文,包括例程代码在内,采用的是 Creative Commons BY-SA 3.0 创作共用授权。

登录symfonychina 发表评论或留下问题(我们会尽量回复)