gpt4 book ai didi

php - 在 Silex 中将用户添加到防火墙的用户列表

转载 作者:搜寻专家 更新时间:2023-10-31 21:28:38 25 4
gpt4 key购买 nike

我是 Silex 和 Symfony 的新手,决定使用 Silex 构建一个使用身份验证的(真正的)小型应用程序。身份验证过程正在运行。我目前正在开发一个小设置页面,允许用户定义他的用户名和密码。授权看起来像这样:

$app['security.firewalls'] = array(
'admin' => array(
'pattern' => '/private',
'form' => array('login_path' => '/login', 'check_path' => '/private/login_check'),
'logout' => array('logout_path' => '/private/logout', 'invalidate_session' => true),
'users' => array(
$username => array('ROLE_ADMIN', $passwd)
)
)
);
$app->register(new Silex\Provider\SecurityServiceProvider());

除管理员外,没有定义其他类型的用户。该设置应由管理员运行一次。

我尝试通过以下方式自定义用户名和密码:第一件事(这对我尝试过的一切都很常见):

// Default password and username 
$username='admin'; $passwd='5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg==';`

The following didn't work (no magical update):

`$app->post('/setup', function(Request $request) use ($app, $username, $passwd) {
$username = $request->get('username');
$passwd= $request->get('passwd');
});

所以我尝试了类似的方法:

$app->post('/setup', function(Request $request) use ($app) {
$username = $request->get('username');
$passwd= $request->get('passwd');
$app['security.firewalls']['admin']['users'] = array($username => array('ROLE_ADMIN', $passwd));
$app->register(new Silex\Provider\SecurityServiceProvider());
});

注册和不注册新的 SecurityServiceProvider,并重新定义数组的“用户”部分。合并也没有帮助。

我在这里有点不知所措 :S 我该怎么做?

最佳答案

动态方式

您不会在任何地方保存您的用户。我假设您在定义 $app['security.firewalls'] 之前定义了 $username$passwd,否则应该已经抛出错误.

您需要的是一个User Provider。现在你没有保存新用户,你只是想重新定义你的两个静态变量——如果我可以添加的话,不检查给定的值——这在很多方面都是错误的:)

要么你深入了解一下如何Define a Custom User Provider in Silex如果您对用户对象有特殊要求,也可能是 how to create an User-Entity ,或者您使用一些已经编写好的 Silex 安全服务的用户提供程序,例如:

Silex-SimpleUser通过杰森格兰姆斯


静态方式

如果您不想动态添加/编辑/删除用户,或者您只有少数用户,您始终可以将静态用户添加到用户数组:

'users' => array(
'admin1' => array('ROLE_ADMIN', 'encodedAndSaltedPW1'),
'admin2' => array('ROLE_ADMIN', 'encodedAndSaltedPW2'),
'admin3' => array('ROLE_ADMIN', 'encodedAndSaltedPW3'),
)

在这种情况下,您需要像文档中描述的那样对 PW 进行编码和加盐:

// find the encoder for a UserInterface instance

$encoder = $app['security.encoder_factory']->getEncoder($user);

// compute the encoded password for foo

$password = $encoder->encodePassword('foo', $user->getSalt());

另见 docs for the SecurityServiceProvider获取更多信息。


您的方式:单用户提供者

在您发表评论后,我更了解您的需求。

你当然可以有自己的普通“单用户提供者”——这可以是这样的:

$app->post('/setup', function(Request $request) use ($app) {

$username = $request->get('username');
$passwd= $request->get('passwd');

$file = 'config.json';
$config = array($username => array('ROLE_ADMIN', $passwd));
file_put_contents($file, json_encode($config));

return $app->redirect($app['url_generator']->generate('login'));
});

请注意,如果没有适当的权限,任何人都可以读取 json 文件 - 因此请确保将编码/加盐密码放入其中,绝不是可读的密码。

然后您需要将 config.json 读入您的防火墙配置:

$app['security.firewalls'] = array(
'admin' => array(
'pattern' => '/private',
'form' => array('login_path' => '/login', 'check_path' => '/private/login_check'),
'logout' => array('logout_path' => '/private/logout', 'invalidate_session' => true),
'users' => json_decode(file_get_contents('config.json'))
)
);

请注意,此代码未经测试,应该更有可能为您提供一个想法。我希望这会有所帮助。

关于php - 在 Silex 中将用户添加到防火墙的用户列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32697338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com