package com.zswl.dataservice.auth; import com.zswl.dataservice.model.user.LoginUser; import jakarta.servlet.http.HttpServletRequest; import org.springframework.util.AntPathMatcher; import org.springframework.util.PathMatcher; import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.List; import java.util.Map; public class UserAuthUtil { private static PathMatcher pathMatcher = new AntPathMatcher(); public static boolean isFree(AuthSettings authSettings, HttpServletRequest request) { String path = request.getRequestURI(); String method = request.getMethod(); List> freeUrls = authSettings.getFreeUrls(); if (freeUrls != null) { for (Map kv : freeUrls) { String path0 = kv.get("path"); String method0 = kv.get("method"); if (pathMatcher.match(path0, path)) { if (method0.indexOf(method) >= 0) { return true; } } } } return false; } public static boolean isCodeValidate(AuthSettings authSettings, HttpServletRequest request) { String path = request.getRequestURI(); String method = request.getMethod(); List> codeValidateUrls = authSettings.getCodeValidateUrls(); if (codeValidateUrls != null) { for (Map kv : codeValidateUrls) { String path0 = kv.get("path"); String method0 = kv.get("method"); if (pathMatcher.match(path0, path)) { if (method0.indexOf(method) >= 0) { return true; } } } } return false; } /** * 模拟权限校验, 可以根据自己项目需要定制不同的策略,如查询数据库获取具体的菜单url或者角色等等. * * @param user */ public static boolean verify(LoginUser user, HttpServletRequest request) { String url = request.getHeader("x-user-serviceName"); if (StringUtils.isEmpty(user)) { return false; } else { List str = new ArrayList();//user.getAllowPermissionService(); for (String permissionService : str) { if (url.equalsIgnoreCase(permissionService)) { return true; } } return false; } } /** * 模拟权限赋值, 可以根据自己项目需要定制不同的策略,如查询数据库获取具体的菜单url或者角色等等. * * @param user */ public static void permission(LoginUser user) { if (user.getLoginName().equals("admin")) { List allowPermissionService = new ArrayList(); allowPermissionService.add("client-service"); allowPermissionService.add("provider-service"); //user.setAllowPermissionService(allowPermissionService); } else if (user.getLoginName().equals("spring")) { List allowPermissionService = new ArrayList(); allowPermissionService.add("client-service"); //user.setAllowPermissionService(allowPermissionService); } else { List allowPermissionService = new ArrayList(); //user.setAllowPermissionService(allowPermissionService); } } }