content: package org.zeus.config;
  
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.security.access.AccessDecisionManager;
  import org.springframework.security.access.AccessDeniedException;
  import org.springframework.security.access.ConfigAttribute;
  import org.springframework.security.authentication.AnonymousAuthenticationToken;
  import org.springframework.security.authentication.BadCredentialsException;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.stereotype.Component;
  
  import java.util.Collection;
  import java.util.Iterator;
  
  /**
   * 自定义实现投票器实现用户鉴权
   * @author: lizhizhong
   * CreatedDate: 2018/11/30.
   */
  @Component
  @Slf4j
  public class UrlAccessDecisionManager implements AccessDecisionManager {
      @Override
      public void decide(Authentication auth, Object o, Collection<ConfigAttribute> cas){
          Iterator<ConfigAttribute> iterator = cas.iterator();
          while (iterator.hasNext()) {
              ConfigAttribute ca = iterator.next();
              //当前请求需要的权限
              String needRole = ca.getAttribute();
              if ("ROLE_LOGIN".equals(needRole)) {
                  if (auth instanceof AnonymousAuthenticationToken) {
                      throw new BadCredentialsException("未登录");
                  } else {
                      return;
                  }
              }
              //当前用户所具有的权限
              Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();
              for (GrantedAuthority authority : authorities) {
                  if (authority.getAuthority().equals(needRole)) {
                      return;
                  }
              }
          }
          throw new AccessDeniedException("权限不足!");
      }
      @Override
      public boolean supports(ConfigAttribute configAttribute) {
          return true;
      }
      @Override
      public boolean supports(Class<?> aClass) {
          return true;
      }
  }