[size=x-large][color=red]
检测用户是否登陆的过滤器:
1). 情景: 系统中的某些页面只有在正常登陆后才可以使用,
用户请求这些页面时要检查 session 中有无该用户信息, 但在所有必要的页面加上session的判断相当麻烦的事情
2). 解决方案:编写一个用于检测用户是否登陆的过滤器, 如果用户未登录, 则重定向到指的登录页面
3). 要求:需检查的在 Session 中保存的关键字; 如果用户未登录,需重定向到指定的页面(URL不包括 ContextPath);
不做检查的URL列表(以分号分开,并且 URL 中不包括 ContextPath)都要采取可配置的方式
<!-- 配置当前 web 应用的安全信息 -->
<!-- 配置用户正常登录后, 把用户信息保存在 Session 中的关键字 -->
<context-param>
<param-name>loginSessionKey</param-name>
<param-value>LOGIN-SESSION-KEY</param-value>
</context-param>
<!-- 配置用户的登录页面 -->
<context-param>
<param-name>loginPage</param-name>
<param-value>/login/login.jsp</param-value>
</context-param>
<!-- 配置不需要受保护的 url 列表 -->
<context-param>
<param-name>uncheckedPages</param-name>
<param-value>/login/login.jsp,/login/login-process.jsp,/login/articles.jsp,/login/logout.jsp</param-value>
</context-param>
4). 具体: article1.jsp, article2.jsp, article3.jsp, article4.jsp 必须登录之后才可以访问, 而 articles.jsp,
login.jsp, login-process.jsp, logout.jsp 即使没有登录也可访问.
[/color][/size]
login.jsp
login-process.jsp
articles.jsp
logout.jsp
article1.jsp
article2.jsp
article3.jsp
article4.jsp
LoginFilter.java
web.xml
检测用户是否登陆的过滤器:
1). 情景: 系统中的某些页面只有在正常登陆后才可以使用,
用户请求这些页面时要检查 session 中有无该用户信息, 但在所有必要的页面加上session的判断相当麻烦的事情
2). 解决方案:编写一个用于检测用户是否登陆的过滤器, 如果用户未登录, 则重定向到指的登录页面
3). 要求:需检查的在 Session 中保存的关键字; 如果用户未登录,需重定向到指定的页面(URL不包括 ContextPath);
不做检查的URL列表(以分号分开,并且 URL 中不包括 ContextPath)都要采取可配置的方式
<!-- 配置当前 web 应用的安全信息 -->
<!-- 配置用户正常登录后, 把用户信息保存在 Session 中的关键字 -->
<context-param>
<param-name>loginSessionKey</param-name>
<param-value>LOGIN-SESSION-KEY</param-value>
</context-param>
<!-- 配置用户的登录页面 -->
<context-param>
<param-name>loginPage</param-name>
<param-value>/login/login.jsp</param-value>
</context-param>
<!-- 配置不需要受保护的 url 列表 -->
<context-param>
<param-name>uncheckedPages</param-name>
<param-value>/login/login.jsp,/login/login-process.jsp,/login/articles.jsp,/login/logout.jsp</param-value>
</context-param>
4). 具体: article1.jsp, article2.jsp, article3.jsp, article4.jsp 必须登录之后才可以访问, 而 articles.jsp,
login.jsp, login-process.jsp, logout.jsp 即使没有登录也可访问.
[/color][/size]
login.jsp
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<form action="login-process.jsp" method="post">
name: <input type="text" name="name" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
login-process.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//处理用户登录的 JSP
//1. 获取用户的登录信息, name
String name = request.getParameter("name") ;
//2. 把登录信息存储 Session 中.
//需要判断用户是否直接访问当前页面或在 login.jsp 页面没有输入任何信息就登录
if(name != null && !name.trim().equals("")) {
//OK, 在 Session 中保存用户信息, 转发到 /login/articles.jsp
session.setAttribute(application.getInitParameter("loginSessionKey"), name) ;
request.getRequestDispatcher("/login/articles.jsp").forward(request, response) ;
}else {
//直接登录或没有输入登录信息, 重定向到 /login/login.jsp
response.sendRedirect(request.getContextPath() + "/login/login.jsp") ;
}
%>
articles.jsp
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<a href="article1.jsp">Article111 Page</a>
<br /><br />
<a href="article2.jsp">Article222 Page</a>
<br /><br />
<a href="article3.jsp">Article333 Page</a>
<br /><br />
<a href="article4.jsp">Article444 Page</a>
<br /><br />
<a href="logout.jsp">Logout</a>
</body>
</html>
logout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Bye!
<%
session.invalidate() ;
%>
<br><br>
<a href="login.jsp">Login</a>
</body>
</html>
article1.jsp
Article 111
article2.jsp
Article 222
article3.jsp
Article 333
article4.jsp
Article 444
LoginFilter.java
package com.syh.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginFilter implements Filter {
private FilterConfig filterConfig ;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
//1. 验证访问的页面是否需要被拦截-->因为这个Filter是将/login/ 下的所有页面全部拦截了!
ServletContext servletContext = filterConfig.getServletContext() ;
//1.1. 获取请求的页面 request.getServletPath()
HttpServletRequest request = (HttpServletRequest) req ;
String servletPath = request.getServletPath() ;
//1.2 获取 web.xml 中配置的 uncheckedPages 参数 --->不需要拦截的页面
String uncheckedPages = servletContext.getInitParameter("uncheckedPages") ;
//1.3 检验uncheckedPages 中是否包县servletPath
//2. 若不需要被拦截(谁都可以直接访问的), 放行
if(uncheckedPages.contains(servletPath)) {
chain.doFilter(req, resp) ;
return ;
}
//3. 若需要被拦截, 检查用户是否登陆 若登陆 允许访问; 若未登录, 重定向到 web.xml 文件中 loginPage 配置的页面
HttpSession session = request.getSession();
String loginSessionKey = servletContext.getInitParameter("loginSessionKey") ;
//获取 户信息保存在 Session 中的关键字
Object obj = session.getAttribute(loginSessionKey) ;
if(null == obj) {
HttpServletResponse response = (HttpServletResponse) resp ;
response.sendRedirect(request.getContextPath() + "/" + servletContext.getInitParameter("loginPage")) ;
} else {
chain.doFilter(req,resp) ;
}
}
@Override
public void init(FilterConfig config) throws ServletException {
this.filterConfig = config ;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.syh.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<!-- 要过滤的页面 -->
<url-pattern>/login/*</url-pattern>
</filter-mapping>
<!-- 配置当前 web 应用的安全信息 -->
<!-- 配置用户正常登录后, 把用户信息保存在 Session 中的关键字 -->
<context-param>
<param-name>loginSessionKey</param-name>
<param-value>LOGIN-SESSION-KEY</param-value>
</context-param>
<!-- 配置用户的登录页面 -->
<context-param>
<param-name>loginPage</param-name>
<param-value>/login/login.jsp</param-value>
</context-param>
<!-- 配置不需要受保护的 url 列表 -->
<context-param>
<param-name>uncheckedPages</param-name>
<param-value>/login/login.jsp,/login/login-process.jsp,/login/articles.jsp,/login/logout.jsp</param-value>
</context-param>
</web-app>