1. 我们可以在web.xml中配置filter来对指定的URL进行过滤,进行一些特殊操作如权限验证等。

<!– session过滤filter –>
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>com.xm.chris.SessionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/resources/*</url-pattern>
</filter-mapping>
public class SessionFilter implements Filter {
private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
private FilterConfig _filterConfig = null; public void init(FilterConfig filterConfig) throws ServletException {
_filterConfig = filterConfig;
} public void destroy() {
_filterConfig = null;
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,
ServletException {
HttpServletRequest rq = (HttpServletRequest) request;
HttpSession httpSession = rq.getSession();
Long userId = (Long) httpSession.getAttribute("userId");
if (userId == null) {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Error</title></head>");
out.println("<body>");
out.println("<p id='Message'>错误.</p>");
out.println("</body></html>");
out.close();
} else {
chain.doFilter(request, response);
} }
}

这时所有请求了contextPath/resources/*路径的request都会被SessionFilter验证是否登录。

2. 但是我们有一些特定的url不想验证登录,想要直接能够访问,怎么办呢?

这时可以配置一个参数,告诉Filter哪些url不想验证。

<filter>
<filter-name>SecurityFilter</filter-name>
<filter-class>com.oracle.ccsc.jcs.sx.filter.SecurityFilter</filter-class>
<init-param>
<param-name>excludedPages</param-name>
<param-value>/xm/portal/notice</param-value>
</init-param>
</filter>

然后在Filter中就可以根据参数判断是否需要过滤。

public class SecurityFilter implements Filter {
private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
private FilterConfig _filterConfig = null; private String excludedPages;
private String[] excludedPageArray; public void init(FilterConfig filterConfig) throws ServletException {
_filterConfig = filterConfig; excludedPages = filterConfig.getInitParameter("excludedPages");
if (StringUtils.isNotEmpty(excludedPages)) {
excludedPageArray = excludedPages.split(",");
}
} public void destroy() {
_filterConfig = null;
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,
ServletException {
HttpServletRequest rq = (HttpServletRequest) request; boolean isExcludedPage = false;
for (String page : excludedPageArray) { //判断是否在过滤url之外if (rq.getPathInfo().equals(page)) {
isExcludedPage = true;
break;
}
}
if (isExcludedPage) { //在过滤url之外
chain.doFilter(request, response);
} else { //不在过滤url之外,判断登录
HttpSession httpSession = rq.getSession();
Long userId = (Long) httpSession.getAttribute("userId");
if (userId == null) {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Error</title></head>");
out.println("<body>");
out.println("<p id='Message'>错误.</p>");
out.println("</body></html>");
out.close();
} else {
chain.doFilter(request, response);
}
}
}
}

3. 关于用Servlet获取URL地址。

在HttpServletRequest类里,有以下六个取URL的函数

getContextPath 取得项目名 
getServletPath 取得Servlet名 
getPathInfo 取得Servlet后的URL名,不包括URL参数 
getRequestURL 取得不包括参数的URL 
getRequestURI 取得不包括参数的URI,即去掉协议和服务器名的URL

具体如下图:

相对应的函数的值如下:

getContextPath:/ServletTest 
getServletPath:/main 
getPathInfo:/index/testpage/test 
getRequestURL:http://localhost:8080/ServletTest/main/index/testpage/test 
getRequestURI:/ServletTest/main/index/testpage/test

Filter中排除对指定URL的过滤的更多相关文章

  1. 《Python CookBook2》 第一章 文本 - 过滤字符串中不属于指定集合的字符 && 检查一个字符串是文本还是二进制

    过滤字符串中不属于指定集合的字符 任务: 给定一个需要保留的字符串的集合,构建一个过滤函数,并可将其应用于任何字符串s,函数返回一个s的拷贝,该拷贝只包含指定字符集合中的元素. 解决方案: impor ...

  2. javascript怎么获取指定url网页中的内容

    javascript怎么获取指定url网页中的内容 一.总结 一句话总结:推荐jquery中ajax,简单方便. 1.js能跨域操作么? javascript出于安全机制不允许跨域操作的. 二.用ph ...

  3. 实验:用Unity抓取指定url网页中的所有图片并下载保存

    突发奇想,觉得有时保存网页上的资源非常麻烦,有没有办法输入一个网址就批量抓取对应资源的办法呢. 需要思考的问题: 1.如何得到网页url的html源码呢? 2.如何在浩瀚如海的html中匹配出需要的资 ...

  4. JAVA判断指定url地址是否匹配指定url集合中的任意一个

    判断字符串为空和判断集合是否为空用到依赖,也可以改成自己的方式 <!-- Spring Web --> <dependency> <groupId>org.spri ...

  5. wemall doraemon中Android app商城系统向指定URL发送GET方法的请求代码

    URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接.程序可以通过URLConnection实例向该URL发送请求.读取U ...

  6. Laravel 更新数据时在表单请求验证中排除自己,检查指定字段唯一性

    原文地址:https://moell.cn/article/24 不错的laravel网站 需求场景 修改用户信息时,在表单请求验证中排除当前邮箱所在的记录行,并检查邮箱的唯一性. Laravel版本 ...

  7. 爬取文件时,对已经操作过的URL进行过滤

    爬取文件时,对已经操作过的URL进行过滤 1.创建过滤规则文件filter.py在spiders同级目录 class RepeatUrl: def __init__(self): self.visit ...

  8. [转]spring的filter中targetFilterLifecycle作用

    在web.xml中进行配置,对所有的URL请求进行过滤,就像"击鼓传花"一样,链式处理. 配置分为两种A和B. A:普通配置 在web.xml中增加如下内容:<filter& ...

  9. 布隆过滤器 - 如何在100个亿URL中快速判断某URL是否存在?

    题目描述 一个网站有 100 亿 url 存在一个黑名单中,每条 url 平均 64 字节.这个黑名单要怎么存?若此时随便输入一个 url,你如何快速判断该 url 是否在这个黑名单中? 题目解析 这 ...

随机推荐

  1. C#WebService 出现No 'Access-Control-Allow-Origin' header is present on the requested resource

    C#WebService 出现No 'Access-Control-Allow-Origin' header is present on the requested resource 解决办法: 在c ...

  2. 导入项目 idea

    下的java核心编程的源码,只有java文件,没有idea或者eclipse的项目结构信息. 分别用eclipse和idea打开了一遍,方便学习调试. 项目文件夹:E:\学习资料\Java\语法\ja ...

  3. yii2实战之初见端倪

    PHP框架大PK php框架有很多种,在国内应用较多的有:Thinkphp, Yii, Laravel, Codeigniter等.关于这些框架,孰优孰劣,是一个极具争议性的话题.各方支持者总能拿出自 ...

  4. 开源纯C#工控网关+组态软件(十)移植到.NET Core

    一.   引子 写这个开源系列已经十来篇了.自从十年前注册博客园以来,关注了张善友.老赵.xiaotie.深蓝色右手等一众大牛,也围观了逗比的吉日嘎啦.精密顽石等形形色色的园友.然而整整十年一篇文章都 ...

  5. JS window对象的top、parent、opener含义介绍(转载)

    1.top该变更永远指分割窗口最高层次的浏览器窗口.如果计划从分割窗口的最高层次开始执行命令,就可以用top变量. 2.openeropener用于在window.open的页面引用执行该window ...

  6. python 基础(四) 正则,递归 生成器

    字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求几乎无处不在.比如判断一个字符串是否是合法的Email地址,虽然可以编程提取@前后的子串,再分别判断是否是单词和域名,但这样做不但麻烦, ...

  7. Python_doc文件写入SQLite数据库

    #docx文档题库包含很多段,每段一个题目,格式为:问题.(答案) #数据库datase.db中tiku表包含kechengmingcheng.zhanngji.timu.daan四个字段 impor ...

  8. shell 中test命令

    test可用于测试表达式,支持测试的范围包括:字符串比较,算术比较,文件存在性.属性.类型等判断.例如,判断文件是否为空.文件是否存在.是否是目录.变量是否大于5.字符串是否等于"longs ...

  9. Spring Security 实战:QQ登录实现

    准备工作 1.在 QQ互联 申请成为开发者,并创建应用,得到APP ID 和 APP Key.2.了解QQ登录时的 网站应用接入流程.(必须看完看懂) 为了方便各位测试,直接把我自己申请的贡献出来:A ...

  10. composer安装以及更新问题,配置中国镜像源。

    配置国内镜像源 中国镜像源 https://pkg.phpcomposer.com/ composer 中文官网地址 http://www.phpcomposer.com/ 下载 Composer 安 ...