我在 symfony 3 中的系统登录出现问题。所以我的 security.yml
是:
security:
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_FMTI: ROLE_FMTI
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
secured_area:
pattern: ^/admin
anonymous: ~
form_login:
always_use_default_target_path: true
default_target_path: /admin/homepage
login_path: /admin/login
check_path: /admin/login_check
logout:
path: /admin/logout
invalidate_session: true
target: /admin/login
access_control:
- { path: ^/admin/homepage, roles: ROLE_ADMIN }
providers:
in_memory:
memory:
users:
admin: { password: admin, roles: 'ROLE_ADMIN' }
encoders:
Symfony\Component\Security\Core\User\User: plaintext
路由:
app_admin_homepage:
path: /homepage
defaults: { _controller: AppAdminBundle:Login:index }
login:
path: /login
defaults: { _controller: AppAdminBundle:Login:login }
login_check:
path: /login_check
logout:
path: /logout
以及 LoginController 的登录方法:
public function loginAction(Request $request)
{
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('AppAdminBundle:Member:login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
问题是,如果我使用凭据登录应用程序:admin/admin。之后我注销。
如果我尝试访问
test.dev/admin/homepage
----> 我正在重定向登录页面,而登录页面是test.dev/admin/login
,所以很好,我以admin
身份登录。如果我尝试访问
test.dev/admin/news/all
-----> 我无需登录即可访问此页面,并且我的登录身份为匿名
因此,如果用户未经过身份验证,我想重定向到所有路由 /admin/*
的登录页面。谢谢,对不起我的英语
最佳答案
在访问控制中您需要添加以下内容:
access_control:
- { path: ^/admin/, roles: ROLE_ADMIN }
这意味着对于/admin/路由之外的任何内容,都需要 ROLE_ADMIN。
--更新
如果您需要访问/admin/login/,您需要添加到除/login 之外的每个管理路由,路径模式如/admin/api/,因此在您的访问控制中您将拥有以下内容:
access_control:
- { path: ^/admin/api/, roles: ROLE_ADMIN }
关于php - 保护 symfony 3 中登录系统的安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34514675/