Java Code Examples for org.springframework.util.PathMatcher#match()

The following examples show how to use org.springframework.util.PathMatcher#match() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DynamicSecurityMetadataSource.java    From mall-swarm with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
    if (configAttributeMap == null) this.loadDataSource();
    List<ConfigAttribute>  configAttributes = new ArrayList<>();
    //获取当前访问的路径
    String url = ((FilterInvocation) o).getRequestUrl();
    String path = URLUtil.getPath(url);
    PathMatcher pathMatcher = new AntPathMatcher();
    Iterator<String> iterator = configAttributeMap.keySet().iterator();
    //获取访问该路径所需资源
    while (iterator.hasNext()) {
        String pattern = iterator.next();
        if (pathMatcher.match(pattern, path)) {
            configAttributes.add(configAttributeMap.get(pattern));
        }
    }
    // 未设置操作请求权限,返回空集合
    return configAttributes;
}
 
Example 2
Source File: DynamicSecurityFilter.java    From mall-swarm with Apache License 2.0 6 votes vote down vote up
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    FilterInvocation fi = new FilterInvocation(servletRequest, servletResponse, filterChain);
    //OPTIONS请求直接放行
    if(request.getMethod().equals(HttpMethod.OPTIONS.toString())){
        fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
        return;
    }
    //白名单请求直接放行
    PathMatcher pathMatcher = new AntPathMatcher();
    for (String path : ignoreUrlsConfig.getUrls()) {
        if(pathMatcher.match(path,request.getRequestURI())){
            fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
            return;
        }
    }
    //此处会调用AccessDecisionManager中的decide方法进行鉴权操作
    InterceptorStatusToken token = super.beforeInvocation(fi);
    try {
        fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
    } finally {
        super.afterInvocation(token, null);
    }
}
 
Example 3
Source File: DynamicSecurityMetadataSource.java    From mall with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
    if (configAttributeMap == null) this.loadDataSource();
    List<ConfigAttribute>  configAttributes = new ArrayList<>();
    //获取当前访问的路径
    String url = ((FilterInvocation) o).getRequestUrl();
    String path = URLUtil.getPath(url);
    PathMatcher pathMatcher = new AntPathMatcher();
    Iterator<String> iterator = configAttributeMap.keySet().iterator();
    //获取访问该路径所需资源
    while (iterator.hasNext()) {
        String pattern = iterator.next();
        if (pathMatcher.match(pattern, path)) {
            configAttributes.add(configAttributeMap.get(pattern));
        }
    }
    // 未设置操作请求权限,返回空集合
    return configAttributes;
}
 
Example 4
Source File: DynamicSecurityFilter.java    From mall with Apache License 2.0 6 votes vote down vote up
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    FilterInvocation fi = new FilterInvocation(servletRequest, servletResponse, filterChain);
    //OPTIONS请求直接放行
    if(request.getMethod().equals(HttpMethod.OPTIONS.toString())){
        fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
        return;
    }
    //白名单请求直接放行
    PathMatcher pathMatcher = new AntPathMatcher();
    for (String path : ignoreUrlsConfig.getUrls()) {
        if(pathMatcher.match(path,request.getRequestURI())){
            fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
            return;
        }
    }
    //此处会调用AccessDecisionManager中的decide方法进行鉴权操作
    InterceptorStatusToken token = super.beforeInvocation(fi);
    try {
        fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
    } finally {
        super.afterInvocation(token, null);
    }
}
 
Example 5
Source File: SysUserController.java    From jeecg-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 根据请求地址获取正则
 * @param url
 * @return
 */
private String getRegexpUrl(String url) {
    List<String> list = sysPermissionService.queryPermissionUrlWithStar();
    if(list!=null && list.size()>0) {
        for (String p : list) {
            PathMatcher matcher = new AntPathMatcher();
            if(matcher.match(p, url)) {
                return p;
            }
        }
    }
    return null;
}
 
Example 6
Source File: PermissionDataAspect.java    From jeecg-boot-with-activiti with MIT License 5 votes vote down vote up
/**
 * 匹配前端传过来的地址 匹配成功返回正则地址
 * AntPathMatcher匹配地址
 *()* 匹配0个或多个字符
 *()**匹配0个或多个目录
 */
private String getRegexpUrl(String url) {
	List<String> list = sysPermissionService.queryPermissionUrlWithStar();
	if(list!=null && list.size()>0) {
		for (String p : list) {
			PathMatcher matcher = new AntPathMatcher();
			if(matcher.match(p, url)) {
				return p;
			}
		}
	}
	return null;
}
 
Example 7
Source File: PermissionDataAspect.java    From teaching with Apache License 2.0 5 votes vote down vote up
/**
 * 匹配前端传过来的地址 匹配成功返回正则地址
 * AntPathMatcher匹配地址
 *()* 匹配0个或多个字符
 *()**匹配0个或多个目录
 */
private String getRegexpUrl(String url) {
	List<String> list = sysPermissionService.queryPermissionUrlWithStar();
	if(list!=null && list.size()>0) {
		for (String p : list) {
			PathMatcher matcher = new AntPathMatcher();
			if(matcher.match(p, url)) {
				return p;
			}
		}
	}
	return null;
}
 
Example 8
Source File: PermissionDataAspect.java    From jeecg-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 匹配前端传过来的地址 匹配成功返回正则地址
 * AntPathMatcher匹配地址
 *()* 匹配0个或多个字符
 *()**匹配0个或多个目录
 */
private String getRegexpUrl(String url) {
	List<String> list = sysPermissionService.queryPermissionUrlWithStar();
	if(list!=null && list.size()>0) {
		for (String p : list) {
			PathMatcher matcher = new AntPathMatcher();
			if(matcher.match(p, url)) {
				return p;
			}
		}
	}
	return null;
}
 
Example 9
Source File: PathFilter.java    From artifactory-resource with Apache License 2.0 5 votes vote down vote up
private boolean hasMatch(PathMatcher pathMatcher, String path, List<String> patterns) {
	for (String pattern : patterns) {
		pattern = cleanPattern(path, pattern);
		if (pathMatcher.match(pattern, path)) {
			return true;
		}
	}
	return false;
}
 
Example 10
Source File: PageDescribeController.java    From wallride with Apache License 2.0 5 votes vote down vote up
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
	BlogLanguage blogLanguage = (BlogLanguage) request.getAttribute(BlogLanguageMethodArgumentResolver.BLOG_LANGUAGE_ATTRIBUTE);
	if (blogLanguage == null) {
		Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
		blogLanguage = blog.getLanguage(blog.getDefaultLanguage());
	}

	String path = urlPathHelper.getLookupPathForRequest(request);

	PathMatcher pathMatcher = new AntPathMatcher();
	if (!pathMatcher.match(PATH_PATTERN, path)) {
		throw new HttpNotFoundException();
	}

	Map<String, String> variables = pathMatcher.extractUriTemplateVariables(PATH_PATTERN, path);
	request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, variables);

	Page page = pageService.getPageByCode(variables.get("code"), blogLanguage.getLanguage());
	if (page == null) {
		page = pageService.getPageByCode(variables.get("code"), blogLanguage.getBlog().getDefaultLanguage());
	}
	if (page == null) {
		throw new HttpNotFoundException();
	}
	if (page.getStatus() != Post.Status.PUBLISHED) {
		throw new HttpNotFoundException();
	}

	return createModelAndView(page);
}
 
Example 11
Source File: PathVariableResolver.java    From specification-arg-resolver with Apache License 2.0 5 votes vote down vote up
public static Map<String, String> resolvePathVariables(String pathPattern, String actualPath) {
	PathMatcher pathMatcher = new AntPathMatcher();
	
	if (pathMatcher.match(pathPattern, actualPath)) {
		return pathMatcher.extractUriTemplateVariables(pathPattern, actualPath);
	} else {
		return emptyMap();
	}
}
 
Example 12
Source File: ResourceServlet.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean isAllowed(String resourcePath) {
	if (resourcePath.matches(PROTECTED_PATH)) {
		return false;
	}
	PathMatcher pathMatcher = new AntPathMatcher();
	for (String pattern : allowedResourcePaths) {
		if (pathMatcher.match(pattern, resourcePath)) {
			return true;
		}
	}
	return false;
}
 
Example 13
Source File: ResourceController.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean isAllowed(String resourcePath) {
	if (resourcePath.matches(PROTECTED_PATH)) {
		return false;
	}
	PathMatcher pathMatcher = new AntPathMatcher();
	for (String pattern : (allowedResourcePaths == null ? ALLOWED_RESOURCE_PATHS : allowedResourcePaths)) {
		if (pathMatcher.match(pattern, resourcePath)) {
			return true;
		}
	}
	return false;
}
 
Example 14
Source File: PathMatchingHandlerChainResolver.java    From disruptor-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
protected boolean pathMatches(String pattern, String path) {
    PathMatcher pathMatcher = getPathMatcher();
    return pathMatcher.match(pattern, path);
}