AntPathMatcher 路径匹配

公司项目使用 AntPathMatcher 路径匹配是否登陆,之前没有接触过,刚好趁这次机会学习了一番。 一、基本规则 1、? 匹配一个字符(除过操作系统默认的文件分隔符) 2、* 匹配0个或多个字符 3、** 匹配0个或多个目录 4、{spring:[a-z]+} 将正则表达式 [a-z]+ 匹配到的值,赋值给名为 spring 的路径变量 PS:必须是完全匹配才行,在 SpringMVC 中只有完全匹配才会进入 controller 层的方法 二、注意事项: 1、匹配文件路径,需要匹配某目录下及其各级子目录下所有的文件,使用 /*/* 而非 *.*,因为有的文件不一定含有文件后缀 2、匹配文件路径,使用 AntPathMatcher 创建一个对象时,需要注意 AntPathMatcher 也有有参构造,传递路径分隔符参数 pathSeparator,对于文件路径的匹配来说,可以根据不同的操作系统来传递各自的文件分隔符,以此防止匹配文件路径错误 3、最长匹配规则(has more characters),即越精确的模式越会被优先匹配到。例如,URL请求 /app/dir/file.jsp,现在存在两个路径匹配模式 /*/*.jsp 和 /app/dir/*.jsp,那么会根据模式 /app/dir/*.jsp 来匹配 三、实例 可以参考若依框架:com.ruoyi.gateway.filter.AuthFilter 和 com.ruoyi.gateway.filter.XssFilter

March 27, 2022 · 1 min · 45 words · Bridge Li

SpringMVC中Interceptor和自定义filter的典型应用

今天写写老夫最擅长的Java web,在Java web中Interceptor和filter应用十分广泛,今天就写一个在我们的项目中的一个最基本的应用,过滤或者拦截未登录用户访问某些资源。 SpringMVC中Interceptor SpringMVC 中的Interceptor 拦截器是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理。比如通过它来进行权限验证,或者是来判断用户是否登陆等等。今天就写一个Interceptor在开发中的典型应用:某一系统某些方法肯定是需要用户登陆才能访问的,而另外一些肯定不需要用户登陆就能访问(这样的例子很多,老夫就不举例说明了),那么我们怎么做,才能做到呢?这个时候Interceptor就派上用场了,下面是一个小例子,供参考: spring-servlet.xml核心代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <mvc:interceptors> <bean id="permissionInterceptor" class="cn.bridgeli.demo.interceptor.PermissionInterceptor"></bean> </mvc:interceptors> &#8230;&#8230; </beans> 对应的Interceptor的实现: package cn.bridgeli.demo.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndViewDefiningException; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.web.util.UrlPathHelper; import cn.bridgeli.demo.entity.User; public class PermissionInterceptor extends HandlerInterceptorAdapter { private UrlPathHelper urlPathHelper = new UrlPathHelper(); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { User user = (User) request.getSession().getAttribute("USER"); String url = urlPathHelper.getLookupPathForRequest(request); int flag = url.indexOf("/admin/"); if (user == null && flag != -1) { ModelAndView mav = new ModelAndView("error/permissionerror"); mav.addObject("ERRORMSG", "对不起,您没有登录,无法使用该功能!"); throw new ModelAndViewDefiningException(mav); } return true; } } 关于InterceptorAdapter的更多用法,大家可以参考http://haohaoxuexi.iteye.com/blog/1750680,老夫以为这篇文章说的相对比较详细易懂,除此之外,我们还可以通过自定义filter来实现; ...

March 8, 2015 · 2 min · 220 words · Bridge Li