不知道如何使用带有Guard的身份验证系统来验证验证码(API令牌示例)共生

时间:2019-09-02 15:10:49

标签: symfony4

我已经完成symfony tutorial的安全身份验证/登录。

代码正在运行,但是现在我不知道如何验证用户输入是否正确识别了我生成的验证码。

  public function login(AuthenticationUtils $authenticationUtils): Response
    {
        if ($this->getUser()) {
           $this->redirectToRoute('app_home');
        }
        $error = $authenticationUtils->getLastAuthenticationError();
        $lastUsername = $authenticationUtils->getLastUsername();
        $captcha = $this->generate_captcha_string();
        $img = $this->add_string_img($captcha);
        return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'captcha' => $img, 'pwd' => $captcha]);
    }
    /**
     * @Route("/logout", name="app_logout")
     */
    public function logout()
    {
        return $this->render('my/index.html.twig', [
            'infos' => 'You are successfully logged out.',
            'controller_name' => 'Test',
        ]);
    }
public function generate_captcha_string()
{
    $length = 6;
    $arr_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $string = '';
    $i = 0;
    while ($i < $length) {
        $string[$i] = $arr_chars[random_int(0, 61)];
        $i++;
    }
    return ($string);
}

public function add_string_img($string)
{
    putenv('GDFONTPATH=' . realpath('.'));
    header('Content-type: image/png');
    $img = imagecreatetruecolor(110, 60);
    $white = imagecolorallocate($img, 255, 255, 255);
    $grey = imagecolorallocate($img, 128, 128, 128);
    $black = imagecolorallocate($img, 0, 0, 0);
    imagefilledrectangle($img, 0, 0, 110, 60, $white);
    $font = 'fonts/Mont-HeavyDEMO.otf';
    imagettftext($img, 16, random_int(-15, 15), random_int(8, 12), random_int(35, 45), $black, $font, $string);
    imagepng($img, 'fonts/myCaptcha.png');
    return ($img);
}

}

0 个答案:

没有答案