经常会遇到这种情况:访问某个网站的时候弹出一个页面,提示你必须先登录才能访问资源。
使用Filter可以写出这种类似于登录验证的小功能
这个例子中,正确的用户名和密码是"123456"和"000000"。
先看Filter实现类的代码
package javaweb.web.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
//这个Filter拦截的不是登陆界面而是登录成功后的主界面
public class OnLineFilter implements Filter {
//没有初始化参数所以init()方法直接空过
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//处理的是Http请求所以强转
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//如果用户登录过,那么session中被会加上一个特定的标识属性
HttpSession session = httpServletRequest.getSession();
//看看是否能查找到这个属性
Object User = session.getAttribute("Now_user");
//如果没找到说明这个访问者没有登录过这个网页
if(User == null){
//直接请求转发到一个写好的错误页面上,尽量保持地址栏的URL不变
request.getRequestDispatcher("Page404.html").forward(request,response);
}else {
//如果找到了说明这个用户登陆过就继续下一个Filter,没有Filter就打开主页面了
chain.doFilter(request,response);
}
}
@Override
//这个Filter只是用来检测Session属性的,destroy()方法也空过
public void destroy() {}
}
这是Filter实现类的代码,每个步骤我都有详细的注释
登录验证的原理通俗点来讲就是:
在用户第一次登录的时候,处理登录请求的Servlet在该用户的session中加入一个标识属性,表示这个用户已经登陆过了,进入程序的主界面是额,主界面拦截的Filter会去找session中的那个标识属性,如果找到了说明这个用户是登陆上来的,就把主页面的资源展示给他看。
假设我知道资源网站的URL,然后直接通过地址栏输入的方式去访问的话,那么在主页面打开的过程中,Filter发现你的session中没有那个标识属性,所以直接把网页给你跳转到一个别的网页上去了,通常这个网页都会提示你登录。
web.xml配置:
<filter>
<filter-name>OnLine</filter-name>
<filter-class>javaweb.web.filter.OnLineFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OnLine</filter-name>
//main.html是主界面
<url-pattern>/main.html</url-pattern>
</filter-mapping>
index.html:(写的比较low不过符合练习要求)
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8"/>
<title>登录界面</title>
</head>
<body>
<form action="login" method="post">
用户名:<input type="text" id="userName" name="userName" placeholder="请输入用户名" />
<br/>
密码: <input type="text" id="password" name="password" placeholder="请输入密码" />
<br/>
<input type="submit" value="登录" onclick="checkform()">
</form>
<script>
function checkform() {
var $user_name = document.getElementById("userName").value;
var $user_pwd = document.getElementById("password").value;
if ($user_name === '') {
alert("用户名不能为空");
return false;
}
if ($user_pwd === '') {
alert("密码不能为空");
return false;
}
if("123456" !== $user_name && "000000" !==$user_pwd){
alert("用户名或密码错误");
return false;
}
return true;
}
</script>
</body>
</html>
登录提示页面代码
<!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 charset="UTF-8" http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>404-对不起!您还没有登录</title>
<style type="text/css">
.head404{ width:580px; height:234px; margin:50px auto 0 auto; background:url(https://www.daixiaorui.com/Public/images/head404.png) no-repeat; }
.txtbg404{ width:499px; height:169px; margin:10px auto 0 auto; background:url(https://www.daixiaorui.com/Public/images/txtbg404.png) no-repeat;}
.txtbg404 .txtbox{ width:390px; position:relative; top:30px; left:60px;color:#eee; font-size:13px;}
.txtbg404 .txtbox p {margin:5px 0; line-height:18px;}
.txtbg404 .txtbox .paddingbox { padding-top:15px;}
.txtbg404 .txtbox p a { color:#eee; text-decoration:none;}
.txtbg404 .txtbox p a:hover { color:#FC9D1D; text-decoration:underline;}
</style>
</head>
<body bgcolor="#494949">
<div class="head404"></div>
<div class="txtbg404">
<div class="txtbox">
<p>对不起,您还没有登录</p>
<p>请点击以下链接</p>
<p>》<a href="/login">返回登录页面</a></p>
</div>
</div>
</body>
</html>
一、首先进入登陆页面,用户名密码验证
二、不登录直接使用地址访问main.html
凉了,不能跳过登录验证,回到登录页面吧
三、输入正确的账号和密码
OK现在登上去了!
主界面丑的一批但是这不是重点,仅供个人学习使用!