【发布时间】:2014-01-15 07:20:44
【问题描述】:
我正在尝试使用 this code snipp from scott 应用安全令牌,但我似乎无法在 symfony2 中解决它,这是我的代码:
<?php
namespace Acme\UserManagementBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class TokenController extends Controller
{
public function randSecureAction($min, $max) {
$range = $max - $min;
if ($range < 0) return $min; // not so random...
$log = log($range, 2);
$bytes = (int) ($log / 8) + 1; // length in bytes
$bits = (int) $log + 1; // length in bits
$filter = (int) (1 << $bits) - 1; // set all lower bits to 1
do {
$rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
$rnd = $rnd & $filter; // discard irrelevant bits
} while ($rnd >= $range);
return new Response ($min + $rnd);
}
public function getTokenAction($length=32) {
$token = "";
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
$codeAlphabet.= "0123456789";
for($i=0;$i<$length;$i++) {
$token .= $codeAlphabet[randSecureAction(0,strlen($codeAlphabet))];
}
return new Response ($token);
}
}
我将此 TokenController 创建为像 this 这样的服务,因此我可以将其调用到我的 DefaultController,现在该服务无法调用此控制器内的其他函数,是我做错了还是我的代码中有问题因为里面的函数(getTokenAction)好像不能使用TokenController类中的其他函数(randSecureAction)。
【问题讨论】: