filter filters, SpringBoot @WebFilter in a class can be used to filter the annotations in the tomcat servlet packages from servlet3.0 version began to support this annotated.
This note contains the following parameters can be set:
Property name | Types of | Explanation | other |
description |
String | The description of annotations | |
displayName |
String | Filters were equivalent with <display-name> | |
initParams |
WebInitParam[] |
Used for setting the initialization parameters of the filter type, equivalent to <init-param> | 如:initParams = {@WebInitParam(name = "ignoredUrl", value = ".css#.js#.jpg#.png#.gif#.ico") |
filterName |
String | Filter Name, equivalent to <filter-name> | |
servletNames |
String[] |
String type array that specifies filtering servlet. The value is the value of the name attribute @WebServlet, or in web.xml <servlet-name> values. | |
value,urlPatterns | String[] | URL pattern matching filter. It is equivalent to <url-pattern> tag. | Such as: urlPatterns = { "/ *"} block all, only need to pay attention value and URLPATTERN is selected from a recommended URLPATTERN is, look better |
dispatcherTypes | DispatcherType[] | Forwarding mode filter. Specific values include: ASYNC, ERROR, FORWARD, INCLUDE, REQUEST. | Has a default value, see above, is the default request |
asyncSupported | boolean | Filter support asynchronous mode of operation, equivalent to <async-supported> tag. | 默认为false |
还有就是,被@WebFilter注解的类,会在容器启动时被加载,并进行属性配置。即项目一启动容器自动加载init方法。
可以看到,项目启动后进入到filter的init方法里面了
先看一下filterconfig:
可以看到在filterDef中有我们之前提到的注解里面的所有内容。
当filter的init方法执行完毕后就注册完毕了,进入类:org.apache.catalina.core.ApplicationFilterConfig,方法:initFilter。
@WebFilter(filterName = "LogCostFilter", urlPatterns = {"/*"}, initParams = {@WebInitParam(name = "ignoredUrl", value = ".css#.js#.jpg#.png#.gif#.ico"), @WebInitParam(name = "filterPath", value = "/user/login#/user/registerUser")}) public class LogCostFilter implements Filter { private static final String FILTER_PATH = "filterPath"; private static final String IGNORED_PATH = "ignoredUrl"; private static final List<String> ignoredList = new ArrayList<>(); private String[] allowUrls; private String[] ignoredUrls; @Override public void init(FilterConfig filterConfig) throws ServletException { String filterPath = filterConfig.getInitParameter(FILTER_PATH); if (!StringUtils.isEmpty(filterPath)) { allowUrls = filterPath.contains("#") ? filterPath.split("#") : new String[]{filterPath}; } String ignoredPath = filterConfig.getInitParameter(IGNORED_PATH); if (!StringUtils.isEmpty(ignoredPath)) { ignoredUrls = ignoredPath.contains("#") ? ignoredPath.split("#") : new String[]{ignoredPath}; for (String ignoredUrl : ignoredUrls) { ignoredList.add(ignoredUrl); } } } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest servletRequest = (HttpServletRequest) request; HttpServletResponse servletResponse = (HttpServletResponse) response; String requestUrl = servletRequest.getRequestURI(); //具体,比如:处理若用户未登录,则跳转到登录页 Object userInfo = servletRequest.getSession().getAttribute("user"); if(userInfo!=null) { //如果已登录,不阻止 chain.doFilter(request, response); return; } if (requestUrl != null && (requestUrl.contains("/login.html") || requestUrl.contains("/register.html"))) { chain.doFilter(request, response); return; } if (verify(ignoredList, requestUrl)) { chain.doFilter(servletRequest, response); return; } if (null != allowUrls && allowUrls.length > 0) { for (String url : allowUrls) { if (requestUrl.startsWith(url)) { chain.doFilter(request, response); return; } } } servletResponse.sendRedirect("/login.html"); } private static String regexPrefix = "^.*"; private static String regexSuffix = ".*$"; private static boolean verify(List<String> ignoredList, String url) { for (String regex : ignoredList) { Pattern pattern = Pattern.compile(regexPrefix + regex + regexSuffix); Matcher matcher = pattern.matcher(url); if (matcher.matches()) { return true; } } return false; } @Override public void destroy() { } }
urlPatterns