package com.storlead.framework.common.util;

import com.storlead.framework.common.properties.UrlChainDefinitionPorperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

/**
 * @program: sp-sales
 * @description:
 * @author: chenkq
 * @create: 2022-07-04 16:24
 */
@Component
public class UrlChainBlackAndWhiteUtil {
    @Autowired
    private UrlChainDefinitionPorperties properties;

    public String getContextPath() {
        return properties.getContextPath();
    }

    /**
     * 白名单URL
     * @param uri
     * @return
     */
    public boolean IsWhiteUri(String uri)
    {
        String [] AUTH_WHITELIST = properties.getAuthWhitelist();
        uri = uri.replace(properties.getContextPath()+"/","/");
        for (int i = 0; i < AUTH_WHITELIST.length; i++)
        {
            if (AUTH_WHITELIST[i].length() > 0)
            {
                String whiteuri = AUTH_WHITELIST[i];
                whiteuri = whiteuri.trim();
                AntPathMatcher antPathMatcher = new AntPathMatcher();
                boolean match = antPathMatcher.match(whiteuri,uri);
                if (match) {
                    return true;
                }
            }
        }
        return false;
    }
}
