此例子为一个简单的登录拦截。

首先在web.xml中配置拦截类。

<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter>
<filter-name>SessionFilter</filter-name>
<!-- 拦截类 -->
<filter-class>com.skin.generate.user.UserLoginFilter</filter-class>
<init-param>
<param-name>loginUrl</param-name>
<param-value>/finder/login.html</param-value>
</init-param>
<!-- 配置参数 比如不拦截的路径 -->
<init-param>
<param-name>excludes</param-name>
<param-value>
/register.html,
/resource/**/*,
/note/**/*
</param-value>
</init-param>
</filter>

其次就是拦截类的编写。

package com.skin.generate.user;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import com.skin.finder.security.UserSession;
import com.skin.finder.web.util.Client;
import com.skin.finder.web.util.CookieUtil;
import com.skin.generate.entity.User; public class UserLoginFilter implements Filter {
/*
* String nofilter; String nofilterFiles[]; String sendRedirect;
*/ public void destroy() { } public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
String path = request.getServletPath();
String url = request.getRequestURI(); // 如果不需要过滤的静态文件,直接放行
if (path.endsWith(".css") || path.endsWith(".js")
|| path.endsWith("Login.html")
|| url.indexOf("resource") > 0 || url.indexOf("note") > 0
|| url.endsWith("login.jsp") || path.endsWith(".gif")
|| path.endsWith(".ico")) {
chain.doFilter(request, response);
return;
} else {
if (checkUser(request, response)) {
chain.doFilter(request, response);// 如果用户已经登录,就放行
return;
} else {
// 没有登录,重定向到登录页面
String contextPath = getContextPath(request);
response.sendRedirect(contextPath+ "/template/finder/login.jsp");
return;
}
} } protected String getContextPath(HttpServletRequest request) {
String contextPath = request.getContextPath(); if ((contextPath == null) || (contextPath.equals("/"))) {
return "";
}
return contextPath;
} private boolean isInArray(String path, String nofilterFiles[]) {
for (int i = 0; i < nofilterFiles.length; i++) {
String nofilterFile = nofilterFiles[i];
if (nofilterFile.equals(path)) {
return true;
}
}
return false;
} private boolean checkUser(HttpServletRequest request,
HttpServletResponse response) throws IOException { //1.判断cookie 是否存在
Cookie cookie = CookieUtil.getCookie(request, Client.COOKIE_NAME);
if(cookie ==null ){
return false;
} //2.校验session
HttpSession session = request.getSession(false);// 如果不存在返回空
if (session == null) {
return false;
}
//3.校验登录用户
Object obj = session.getAttribute(SessionUtils.USER_NAME);
if (obj == null) {
return false;
} return true;
} public void init(FilterConfig config) throws ServletException {
/*
* nofilter = config.getInitParameter("excludes");
* nofilterFiles =nofilter.split(","); sendRedirect =
* config.getInitParameter("loginUrl");
*/
// System.out.println(Arrays.asList(nofilterFiles));
} }

java登录拦截Filter的更多相关文章

  1. 过滤器(filter)实现用户登录拦截

    过滤器(filter)实现用户登录拦截 >>>>>>>>>>>>>>>>>>>> ...

  2. Java 中的 Filter 过滤器详解

    Filter简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件 ...

  3. Java中的Filter过滤器

    Filter简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,Web开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件 ...

  4. Spring mvc登录拦截器

    自己实现的第一个Spring mvc登录拦截器 题目要求:拒绝未登录用户进入系统,只要发现用户未登录,则将用户请求转发到/login.do要求用户登录 实现步骤: 1.在spring的配置文件中添加登 ...

  5. java之拦截器Interceptor

    1,拦截器的概念    java里的拦截器是动态拦截Action调用的对象,它提供了一种机制可以使开发者在一个Action执行的前后执行一段代码,也可以在一个Action执行前阻止其执行,同时也提供了 ...

  6. 大型运输行业实战_day05_1_登录+注销+表单重复提交+登录拦截器

    1.登录 登录实现如下步骤: 1.在首页中添加登录按钮 html代码如下: <%@ page contentType="text/html;charset=UTF-8" la ...

  7. struts2自定义登录拦截器

    版权声明:本文为博主原创文章,未经博主允许不得转载. (1)配置web.xml,让xml加载struts2框架 <?xml version="1.0" encoding=&q ...

  8. SpringMVC 使用注解完成登录拦截

    目录 为了实现用户登录拦截你是否写过如下代码呢? 1. 基于Filter 2. 基于Struts 3. 基于SpringMVC 如何使用自定义注解完成自定义拦截呢? 登录注解 SpringMVC 拦截 ...

  9. Java Web之Filter

    Filter被称为过滤器或者拦截器,基本功能就是对调用servler过程的拦截,在servlet进行响应和处理前后实现一些特殊功能.其实,Filter过滤器就是一个实现了javax.servlet.F ...

随机推荐

  1. B. Spider Man

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  2. Android 用Animation-list实现逐帧动画 (转载)

    转自:http://blog.csdn.net/aminfo/article/details/7847761 第一步:先上图片素材,以下素材放到res/drawable目录下: http://blog ...

  3. python 的多线程执行速度

    python 的多线程有点鸡肋,适用场景有局限,单位时间多个核只能跑一个线程. 有泳池一个,四个泵,但只有一个人,一人只能开启管理着其中一个,所以四个泵没什么用.但是,如果泵的工作时间与冷却恢复时间是 ...

  4. Luogu P3946 ことりのおやつ(小鸟的点心) 【最短路】By cellur925

    题目传送门 日本的冬天经常下雪.不幸的是,今天也是这样,每秒钟雪的厚度会增加q毫米. 秋叶原共有n个地点,编号从1到n.每个地点在开始的时候的积雪高度为hi. 有m条双向道路连接这些地点,它们的长度分 ...

  5. scrapy 用法总结

    待更新: 建立python开发虚拟环境 virtualenv  mkvirtualenv --python=the-path-to-the-python-you-want-to use 安装: 使用p ...

  6. hdu1233 还是畅通工程 基础最小生成树

    //克鲁斯卡尔 #include<iostream> #include<algorithm> using namespace std; ; struct node { int ...

  7. Hexo瞎折腾系列(3) - 添加GitHub彩带和GitHub Corner

    页面右上角添加GitHub彩带 你可以在这里找到一共12种样式的GitHub彩带,复制其中的超链代码. 在themes\next\layout\_layout.swig目录下找到头部彩带相关的代码: ...

  8. Codeforces Round #408 (Div. 2) D

    Description Inzane finally found Zane with a lot of money to spare, so they together decided to esta ...

  9. 500 Keyboard Row 键盘行

    给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 详见:https://leetcode.com/problems/keyboard-row/description/ C++: cl ...

  10. RCC 2014 Warmup (Div. 1)

    A 暴力 #include <iostream> #include<cstdio> #include<cstring> #include<algorithm& ...