package com.ytpm.config.custom;

import cn.hutool.core.text.AntPathMatcher;
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 java.util.Collection;
import java.util.List;

@Component
public class CustomFilterInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
    AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Override
    public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
        String requestUrl = ((FilterInvocation) object).getRequestUrl();
        if (antPathMatcher.match("/oauth/*", requestUrl)) {
            return null;
        }
//        List<SecurityMenu> securityMenuList = (List<SecurityMenu>) menuService.getMenuListByRole().getData();
//        for (SecurityMenu securityMenu : securityMenuList) {
//            if(StringUtils.isEmpty(securityMenu.getUrlPattern()))continue;
//            if(antPathMatcher.match(securityMenu.getUrlPattern(),requestUrl)){
//                List<Role> roles = securityMenu.getRoleList();
//                int size = roles.size();
//                String[] str = new String[size];
//                for (int i = 0; i < size; i++) {
//                    str[i] = roles.get(i).getName();
//                }
//                return SecurityConfig.createList(str);
//            }
//        }
        //访问菜单角色权限控制
        return SecurityConfig.createList("ROLE_LOGIN");
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}
