美文网首页
使用 Symfony 5.4 优化 Voter 性能

使用 Symfony 5.4 优化 Voter 性能

作者: forks1990 | 来源:发表于2021-12-04 10:51 被阅读0次

Voter 有很强的灵活性:

interface VoterInterface
{
    public const ACCESS_GRANTED = 1;
    public const ACCESS_ABSTAIN = 0;
    public const ACCESS_DENIED = -1;

    /**
     * Returns the vote for the given parameters.
     *
     * This method must return one of the following constants:
     * ACCESS_GRANTED, ACCESS_DENIED, or ACCESS_ABSTAIN.
     *
     * @param mixed $subject    The subject to secure
     * @param array $attributes An array of attributes associated with the method being invoked
     *
     * @return int either ACCESS_GRANTED, ACCESS_ABSTAIN, or ACCESS_DENIED
     */
    public function vote(TokenInterface $token, $subject, array $attributes);
}

通过实现 vote() 方法,结合 DI 几乎可以实现任意想要的判断条件,但是有一个巨大的开销:

NOTE: 在结果没有确定之前,所有的 Voter 对象都要被调用

其实可能只是比较一下用户的 RoleSymfony 5.4 通过实现这两个额外的方法解决这个问题:

/**
 * Let voters expose the attributes and types they care about.
 *
 * By returning false to either `supportsAttribute` or `supportsType`, the
 * voter will never be called for the specified attribute or subject.
 *
 * @author Jérémy Derussé <jeremy@derusse.com>
 */
interface CacheableVoterInterface extends VoterInterface
{
    public function supportsAttribute(string $attribute): bool;

    /**
     * @param string $subjectType The type of the subject inferred by `get_class` or `get_debug_type`
     */
    public function supportsType(string $subjectType): bool;
}

如果这两个方法中有一个返回 false,则Symfony再遇到相同的 $subject 类型和/或 $attribute的时候跳过这个Voter。

NOTE: 符合条件时跳过

反向逻辑比较安全,减少安全漏洞的机会。

  1. 用户之前有权限,下一秒可能就没权限了。
  2. 和更复杂的条件有关,比如别的相关数据的状态

相关文章

  • 使用 Symfony 5.4 优化 Voter 性能

    Voter 有很强的灵活性: 通过实现 vote() 方法,结合 DI 几乎可以实现任意想要的判断条件,但是有一个...

  • UITableView性能优化

    1.性能优化-UITableView的优化使用 2.老生常谈之UITableView的性能优化

  • 简述http缓存

    简介 网站性能第一优化定律:优先考虑使用缓存优化性能。合理的使用缓存,对网站的性能优化的意义重大。以下对于缓存,都...

  • 2016/12/21前端性能优化

    体验与性能优化 1. 体验优化 使用lazyload实现懒加载,优化体验与性能。 使用连续的jpg与交错的png。...

  • react性能优化

    React 性能优化 React 性能优化 | 包括原理、技巧、Demo、工具使用[https://juejin....

  • 2020 前端 React 面试

    性能优化 性能优化,永远是面试的重点,性能优化对于 React 更加重要 在页面中使用了setTimout()、a...

  • React-Redux性能优化

    前面写了两篇文章《React组件性能优化》《Redux性能优化》,分别针对React和Redux在使用上的性能优化...

  • 02Homestead环境搭建

    按照官方的教程,搭了一遍laravel5.4开发环境,因为之前用symfony也是virtual box+vagr...

  • iOS UIWebView 与WKWebView集锦

    使用WKWebView进行性能调优 WebView性能、体验分析与优化 UIWebView、WKWebView使用...

  • 【Unity项目实战】http网络插件和性能优化

    使用http网络请求:Untiy中常用通信(HTTP)插件BestHttp 性能优化:Unity性能的全面优化详解

网友评论

      本文标题:使用 Symfony 5.4 优化 Voter 性能

      本文链接:https://www.haomeiwen.com/subject/aruvxrtx.html