web.xml 中配置
<!-- token filter-->
<filter>
<filter-name>tokenFilter</filter-name>
<filter-class>com.zpkj.template.filter.TokenFilter</filter-class>
<init-param>
<param-name>ignores</param-name>
<param-value>/app/register,/app/login</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>tokenFilter</filter-name>
<url-pattern>/app/*</url-pattern>
</filter-mapping>
Filter 实现类中代码
public class TokenFilter implements Filter {
private String excludedPage;
private String[] excludedPages;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
excludedPage = filterConfig.getInitParameter("ignores");//此处的ignores就是在web.xml定义的名称一样。
if (excludedPage != null && excludedPage.length() > 0){
excludedPages = excludedPage.split(",");
}
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
// 定义表示变量 并验证用户请求URL 是否包含不过滤路径
boolean flag = false;
for (String page:excludedPages) {
if (request.getRequestURI().equals(page)){
flag = true;
}
}
if(flag){
chain.doFilter(request,response);
}
}
@Override
public void destroy() {
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于