下面列出了org.springframework.util.PathMatcher#match ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
@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);
}
}
@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;
}
@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);
}
}
/**
* 根据请求地址获取正则
* @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;
}
/**
* 匹配前端传过来的地址 匹配成功返回正则地址
* 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;
}
/**
* 匹配前端传过来的地址 匹配成功返回正则地址
* 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;
}
/**
* 匹配前端传过来的地址 匹配成功返回正则地址
* 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;
}
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;
}
@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);
}
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();
}
}
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;
}
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;
}
protected boolean pathMatches(String pattern, String path) {
PathMatcher pathMatcher = getPathMatcher();
return pathMatcher.match(pattern, path);
}