56fe46999e46bb1b05d2d88d497b6095d78fe1ac
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.admin.bff.security.web.access;
2
3 import java.util.Collection;
4 import java.util.Iterator;
5
6 import org.springframework.security.access.AccessDecisionManager;
7 import org.springframework.security.access.AccessDeniedException;
8 import org.springframework.security.access.ConfigAttribute;
9 import org.springframework.security.authentication.InsufficientAuthenticationException;
10 import org.springframework.security.core.Authentication;
11 import org.springframework.security.core.GrantedAuthority;
12
13 public class MyAccessDecisionManager implements AccessDecisionManager {
14
15   @Override
16   public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes)
17       throws AccessDeniedException, InsufficientAuthenticationException {
18
19     if (null == configAttributes || configAttributes.size() <= 0) {
20       return;
21     }
22     
23     ConfigAttribute ca;
24     String needRole;
25     for (Iterator<ConfigAttribute> iter = configAttributes.iterator(); iter.hasNext();) {
26       ca = iter.next();
27       needRole = ca.getAttribute();
28       for (GrantedAuthority ga : authentication.getAuthorities()) { // authentication 为在注释1 中循环添加到 GrantedAuthority 对象中的权限信息集合
29         if (needRole.trim().equals(ga.getAuthority())) {
30           return;
31         }
32       }
33     }
34     
35     throw new AccessDeniedException("no right");
36   }
37
38   @Override
39   public boolean supports(ConfigAttribute attribute) {
40     return true;
41   }
42
43   @Override
44   public boolean supports(Class<?> clazz) {
45     return true;
46   }
47
48 }