package com.changhong.epc.config.filter; import com.changhong.epc.config.tool.MybatisTool; import com.changhong.epc.constter.admin.AdminUrlConst; import com.changhong.epc.constter.base.BaseConst; import com.changhong.epc.rely.api.tool.FilterTool; import com.changhong.epc.rely.api.tool.HttpContextTool; import com.codingapi.tx.aop.bean.TxCompensateLocal; import com.codingapi.tx.aop.bean.TxTransactionLocal; import com.iemsoft.framework.cloud.config.interceptor.LoginFilter; import com.iemsoft.framework.cloud.core.thread.ThreadData; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; import static com.changhong.epc.constter.admin.AdminUrlConst.REST_ADMIN_CENTER_MASTER_VALUE_PUSHMASDATAVALUE; @Component @Slf4j public class LoginFileterExtend extends FilterTool implements LoginFilter,BaseConst { public static final List ADMIN_URLS = Arrays.asList(AdminUrlConst.class.getFields()) .stream() .map(field -> { try { return field.get(new AdminUrlConst(){}); } catch (IllegalAccessException e) { e.printStackTrace(); return ""; } }) .map(Object::toString) .map(url->url.replaceAll("[{][^}]+[}]", "").replaceAll("[/]+", "/")) .collect(Collectors.toList()); /** * 向线程中添加需要参数 */ @SuppressWarnings("unchecked") @Override public void login(HttpServletRequest request) { String url; log.debug("{}:进入token拦截", url = HttpContextTool.getRequestUIR(request)); if(url.charAt(0) == '/'){ url = url.substring(1); } FILTER_TYPE.get(url.indexOf("json/addFtpPath")==0?FilterType.MASTER:url.indexOf("json") == 0 ? FilterType.PUBLIC : FilterType.PRIVATE) .filter(request); if(url.indexOf("tenant/org/gettenantOrCompany") >= 0){ return; } if(!isAdmin(url)){ MybatisTool.initDataSource(); }else{ MybatisTool.setAdminDataSource(); } } public static boolean isAdmin(String url) { for(String adminUrl : ADMIN_URLS){ if(!Objects.equals(REST_ADMIN_CENTER_MASTER_VALUE_PUSHMASDATAVALUE, url) && url.indexOf(adminUrl) >= 0){ return true; } } return false; } public static void main(String... args) { System.out.println( isAdmin("admin/select/companys/28") ); } @Override public void afterCompletion(HttpServletRequest request) { log.debug("清空线程参数--->"); TxCompensateLocal.clear(); TxTransactionLocal.clear(); ThreadData.clear(); } }