| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- 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<Map<String, String>> freeUrls = authSettings.getFreeUrls();
- if (freeUrls != null) {
- for (Map<String, String> 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<Map<String, String>> codeValidateUrls = authSettings.getCodeValidateUrls();
- if (codeValidateUrls != null) {
- for (Map<String, String> 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<String> str = new ArrayList<String>();//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);
- }
- }
- }
|