content: package org.zeus.config;
  
  import org.zeus.bean.Menu;
  import org.zeus.bean.Role;
  import org.zeus.service.MenuService;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.access.ConfigAttribute;
  import org.springframework.security.access.SecurityConfig;
  import org.springframework.security.web.FilterInvocation;
  import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
  import org.springframework.stereotype.Component;
  import org.springframework.util.AntPathMatcher;
  
  import java.util.Collection;
  import java.util.List;
  
  /**
   * 储存请求和权限的对应关系
   * @author: lizhizhong
   * CreatedDate: 2018/12/28.
   */
  @Component
  public class CustomMetadataSource implements FilterInvocationSecurityMetadataSource {
      @Autowired
      MenuService menuService;
      AntPathMatcher antPathMatcher = new AntPathMatcher();
      @Override
      public Collection<ConfigAttribute> getAttributes(Object o) {
          String requestUrl = ((FilterInvocation) o).getRequestUrl();
          List<Menu> allMenu = menuService.getAllMenu();
          for (Menu menu : allMenu) {
              if (antPathMatcher.match(menu.getUrl(), requestUrl)
                      &&menu.getRoles().size()>0) {
                  List<Role> roles = menu.getRoles();
                  int size = roles.size();
                  String[] values = new String[size];
                  for (int i = 0; i < size; i++) {
                      values[i] = roles.get(i).getName();
                  }
                  return SecurityConfig.createList(values);
              }
          }
          //没有匹配上的资源，都是登录访问
          return SecurityConfig.createList("ROLE_LOGIN");
      }
      @Override
      public Collection<ConfigAttribute> getAllConfigAttributes() {
          return null;
      }
  
      @Override
      public boolean supports(Class<?> aClass) {
          return FilterInvocation.class.isAssignableFrom(aClass);
      }
  }
  