基于安全元信息进行访问决策。

定义

  1. export interface AccessDecisionManager {
  2. decide(securityMetadata: SecurityMetadata): Promise<void>;
  3. }

默认实现

  1. @Component(AccessDecisionManager)
  2. export class AccessDecisionManagerImpl implements AccessDecisionManager {
  3. protected prioritized: AccessDecisionVoter[];
  4. constructor(
  5. @Autowired(AccessDecisionVoter)
  6. protected readonly accessDecisionVoters: AccessDecisionVoter[]
  7. ) {
  8. this.prioritized = Prioritizeable.prioritizeAllSync(this.accessDecisionVoters).map(c => c.value);
  9. }
  10. async decide(securityMetadata: SecurityMetadata): Promise<void> {
  11. let grant = 0;
  12. for (const voter of this.prioritized) {
  13. if (await voter.support(securityMetadata)) {
  14. const result = await voter.vote(securityMetadata);
  15. if (result === ACCESS_DENIED) {
  16. throw new AccessDeniedError('Access is denied');
  17. } else if (result === ACCESS_GRANTED) {
  18. grant++;
  19. }
  20. }
  21. }
  22. if (grant <= 0) {
  23. throw new AccessDeniedError('Access is denied');
  24. }
  25. }
  26. }