我的项目中有2个包:
每个都有自己的数据库
实际上我的security.yml只有一个提供者:
providers:
korea:
entity: { class: Korea\AlmacenBundle\Entity\Usuario, property: username }
Run Code Online (Sandbox Code Playgroud)
如您所见,两个bundle都通过同一个表进行身份验证:Usuario,在korea_motos中
表:Usuario(korea_motos数据库)
----- ----- 1 | -------------管理---------------- | ------- --- AlmacenBundle ----------
----- ----- 2 | -------------管理---------------- | ------- --- RepuestosBundle -------
现在我想验证用户,对于RepuestosBundle,在galvez_motos中使用表Usuario,删除上一表中的"bundle"列.
问题出在security.yml文件中.如果我这样做:
providers:
korea:
entity: { class: Korea\AlmacenBundle\Entity\Usuario, property: username }
galvez:
entity: { class: Galvez\RepuestosBundle\Entity\Usuario, property: username }
Run Code Online (Sandbox Code Playgroud)
Symfony发布了一个例外:
The class 'Galvez\RepuestosBundle\Entity\Usuario' was not …Run Code Online (Sandbox Code Playgroud) 我正在尝试@Security为我的路线使用注释.像这样:
/**
* @return Response
* @Route("/action")
* @Security("has_role('ROLE_USER')")
* @Template()
*/
public function someAction()
{
return array();
}
Run Code Online (Sandbox Code Playgroud)
当安全限制触发异常时,我收到消息Expression "has_role('ROLE_USER')" denied access.
向最终用户显示这是不可接受的,因此我正在尝试找到一种方法来自定义注释消息.
简单的解决方法是不使用@Secutity注释并编写如下代码:
/**
* @return Response
* @Route("/action")
*
* @Template()
*/
public function someAction()
{
if (!$this->get('security.context')->isGranted('ROLE_USER')) {
throw new AccessDeniedException('You have to be logged in in order to use this feature');
}
return array();
}
Run Code Online (Sandbox Code Playgroud)
但这不太方便,可读性也差.
是否可以将自定义消息写入@Security注释?
我正在尝试实现自定义选民.
从控制器我这样称呼它:
$prj = $this->getDoctrine()->getRepository('AppBundle:Project')->findOneById($id);
if (false === $this->get('security.authorization_checker')->isGranted('responsible', $prj)) {
throw new AccessDeniedException('Unauthorised access!');
}
Run Code Online (Sandbox Code Playgroud)
第一行正确检索Project对象(我使用转储检查).
这个问题发生在选民内部
<?php
namespace AppBundle\Security\Authorization\Voter;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class ProjectVoter implements VoterInterface
{
const RESPONSIBLE = 'responsible';
const ACCOUNTABLE = 'accountable';
const SUPPORT = 'support';
const CONSULTED = 'consulted';
const INFORMED = 'informed';
public function supportsAttribute($attribute)
{
return in_array($attribute, array(
self::RESPONSIBLE,
self::ACCOUNTABLE,
self::SUPPORT,
self::CONSULTED,
self::INFORMED,
));
}
public function supportsClass($class)
{
$supportedClass = 'AppBundle\Entity\Project';
return $supportedClass === $class || is_subclass_of($class, $supportedClass); …Run Code Online (Sandbox Code Playgroud) 如果我有一个安全的路由,让我们说panel如下,Symfony将只允许登录用户访问.
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/panel, role: ROLE_USER }
Run Code Online (Sandbox Code Playgroud)
对于未登录的用户,它总是将它们重定向到login_path(我使用的是FOSUserBundle):
security:
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
login_path: fos_user_security_login
Run Code Online (Sandbox Code Playgroud)
我在哪里可以禁用或覆盖此重定向?我想直接显示登录表单,而不重定向用户.
我认为它与此有关AccessDeniedHandlerInterface,但是在security.yml中需要覆盖哪些密钥?默认实现在哪里?
对于我们拥有的其他情况DefaultLogoutSuccessHandler, DefaultAuthenticationFailureHandler, DefaultAuthenticationSuccessHandler,我们可以针对这些情况中的每一种实现服务,这扩展了它们各自的接口并且可以以自定义方式处理该情况.但是,找不到AccessDenied的任何内容.其目录仅包含接口.
假设我的access_control块下面是security.yml:
access_control:
- { path: ^/$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/reset-password, roles: IS_AUTHENTICATED_ANONYMOUSLY }
Run Code Online (Sandbox Code Playgroud)
在这种情况下,每个人都有权进入homepage和reset-password页面.但我想仅允许匿名身份验证的用户使用这些页面.完全通过身份验证的用户应该获得403 access denied error或404 page not found.
据资料与allow_if我应该ablo创建角色表达式定义访问.但如果我这样做:
access_control:
- { path: ^/reset-password, allow_if: "has_role('IS_AUTHENTICATED_ANONYMOUSLY') and not has_role('IS_AUTHENTICATED_FULLY')" }
Run Code Online (Sandbox Code Playgroud)
现在下面的想法完全验证的用户(登录)不应该被允许访问的页面和匿名认证应该能够访问,但可惜的是,没有任何用户都能够访问它...
我缺少什么想法?
UPDATE
通过正确的答案,正如以下建议的那样:
- { path: ^/reset-password, allow_if: "is_anonymous() and !is_authenticated()" }
Run Code Online (Sandbox Code Playgroud) 在 Symfony 4 中,该AuthenticatorInterface::supports()方法有以下注释:
interface AuthenticatorInterface extends AuthenticationEntryPointInterface
{
/**
* Does the authenticator support the given Request?
*
* If this returns false, the authenticator will be skipped.
*
* @param Request $request
*
* @return bool
*/
public function supports(Request $request);
Run Code Online (Sandbox Code Playgroud)
我发现措辞令人困惑。username当我尝试实现这一点时,我的第一直觉是如果请求包含and字段,则返回 true password,但后来我记得我收到的所有请求都经过身份验证,即使我没有使用登录表单。
该supports()方法是重写参数的方法吗security.firewalls.myFirewall.pattern?它是处理多个身份验证器之间的流的东西吗?
我应该如何使用这个界面?
我在 security.yml 中为我的网站定义安全性
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/ad/new, role: ROLE_USER }
- { path: ^/myAds, role: ROLE_USER }
- { path: ^/payments, role: ROLE_USER }
- { path: ^/pay, role: ROLE_USER }
Run Code Online (Sandbox Code Playgroud)
但我不确定如何在此处添加这样的路线:
mybundle_contact_advertiser:
path: /ad/{id}/contact
defaults: { _controller: MyBundle:Default:contactAdvertiser,id:null}
Run Code Online (Sandbox Code Playgroud)
id考虑到我不能这样做,如何定义:
- { path: ^/ad, role: ROLE_USER }
Run Code Online (Sandbox Code Playgroud)
作为一条路线
mybundle_ad:
path: /ad/{id}
defaults: { _controller: MyBundle:Default:viewAd ,id:null}
Run Code Online (Sandbox Code Playgroud)
不适用于未注册的用户。
我试图阻止用户登录,他的状态为非活动状态。我正在将 API 平台与 LexikJWT 捆绑包一起使用。
我尝试JWTAuthentication通过扩展来进行防护JWTTokenAuthenticator->checkCredentials,但问题是这在用户登录后才起作用。
我想要实现的是向用户返回一条消息,要求他首先激活其帐户,或任何其他消息,最好是任何自定义条件下的任何自定义消息。
我的安全 YAML 如下所示:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
app_user_provider:
entity:
class: App\Entity\User
property: email
firewalls:
dev:
pattern: ^/_(profiler|wdt)
security: false
api:
pattern: ^/api/
stateless: true
anonymous: true
provider: app_user_provider
json_login:
check_path: /api/authentication_token
username_path: email
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
guard:
authenticators:
- app.jwt_token_authenticator
main:
anonymous: true
access_control:
- { path: ^/api/authentication_token, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/graphql, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/public-api, roles: IS_AUTHENTICATED_ANONYMOUSLY …Run Code Online (Sandbox Code Playgroud) 我有一个页面正在尝试将注释转换为 PHP8 属性。
namespace App\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[IsGranted('ROLE_ADMIN')]
#[Route('/page')]
class PageController extends AbstractController
{
#[Route('/', name: 'page')]
public function index(): Response
{
return $this->render('page/index.html.twig', [
'controller_name' => 'PageController',
]);
}
}
Run Code Online (Sandbox Code Playgroud)
该IsGranted属性似乎不起作用,因为页面可以访问,而不是 403 错误。另一方面,当转换为注释时,如下所示,它会按预期工作。是否有我缺少的配置设置?
/**
* @IsGranted("ROLE_ADMIN")
*/
#[Route('/page')]
class PageController extends AbstractController
{
#[Route('/', name: 'page')]
public function index(): Response
{
return $this->render('page/index.html.twig', [
'controller_name' => 'PageController',
]);
}
}
Run Code Online (Sandbox Code Playgroud)
其他属性(例如#[Route]等#[Entity])有效,但安全属性似乎不起作用。
我想验证用户的现有密码(以允许他们更改密码)。
我想走以下路线,但遇到了散列密码总是显示为不同散列的问题。我在用 UserPasswordHasherInterface。
$hashedOldPassword = $passwordHasher->hashPassword(
$user,
$data['oldPassword']
);
if ($hashedOldPassword === $user->getPassword()) {
setNewPassword();
}
Run Code Online (Sandbox Code Playgroud) symfony ×10
symfony-security ×10
php ×6
symfony4 ×2
symfony5 ×2
doctrine ×1
php-8 ×1
symfony-flex ×1