【问题标题】:Symfony4 security issueSymfony4 安全问题
【发布时间】:2018-11-09 19:22:25
【问题描述】:

我尝试创建一个简单的登录表单,其中包含通过 web 应用创建或注册的用户。

这是我的 security.yml 文件

security:

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            pattern:    ^/
            http_basic: ~
            provider: our_db_provider
    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }

    encoders:
        App\Project\UserBundle\Entity\Users:
        algorithm: bcrypt

    providers:
        our_db_provider:
            entity:
                class: App\Project\UserBundle\Entity\Users:
                property: username

如果我访问像 /nl/admin/.. 这样的页面,即使我没有登录,我也可以访问该页面。 访问控制失败?

即使我尝试登录时,我也得到了无效的凭据..但凭据是正确的..

这是我的控制器

namespace App\Project\UserBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class LoginController extends Controller
{

    public function loginAction(Request $request, AuthenticationUtils $authenticationUtils)
    {

        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();

        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render('@ProjectUser/frontend/login/login.html.twig', array(
            'last_username' => $lastUsername,
            'error'         => $error,
        ));
    }
}

我错过了什么?

【问题讨论】:

    标签: symfony4 symfony-security


    【解决方案1】:

    您必须在 access_control 路径中“正则化”_locale URI 路径,具体取决于它们的定义方式。对于只有 2 个字母的语言环境,您可以编写 access_control 如下:

    access_control:
        - { path: ^/[a-z]{2}/admin/, role: ROLE_ADMIN }
    

    或者如果您更喜欢列出它们,例如:

    access_control:
        - { path: ^/(en|nl|de)/admin/, role: ROLE_ADMIN }
    

    access_control 路径是纯字符串模式,它们不考虑路由参数...

    【讨论】:

      猜你喜欢
      • 2019-01-22
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      • 2014-08-19
      • 2011-11-08
      • 2022-01-16
      相关资源
      最近更新 更多