赞
赏
使用 Filter 进行权限的控制,根据用户登录的状态,控制用户的访问权限。
编写过滤器,进行权限的控制,实现以下要求:
百度云
链接:https://pan.baidu.com/s/1aZetLrVLu7Iw3AGDasbmsg 提取码:mpdg
login.jsp 代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h2>用户登录(www.haicoder.net)</h2>
<form action="login" method="post">
用户名:<input type="text" name="username"> <span>${msg}</span><br>
密码:<input type="password" name="password"><br/>
<br/>
<input type="submit" value="登录">
</form>
</body>
</html>
list.jsp 代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>查询页面</title>
</head>
<body>
<h2>查询页面(www.haicoder.net)</h2>
访问此网页无需登录
</body>
</html>
add.jsp 代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加页面</title>
</head>
<body>
<h2>添加页面(www.haicoder.net)</h2>
欢迎您,${username},您可以在此页面完成添加操作!
</body>
</html>
update.jsp 代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改页面</title>
</head>
<body>
<h2>修改页面(www.haicoder.net)</h2>
欢迎您,${username},您可以在此页面完成更新操作!
</body>
</html>
AuthorityFilter 代码:
package net.haicoder.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter(filterName = "AuthorityFilter", urlPatterns = "/admin/*")
public class AuthorityFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
// 向下转型
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
// 获取会话域
HttpSession session = request.getSession();
// 从会话域中取出用户的信息
String username = (String) session.getAttribute("username");
System.out.println(username);
// 判断用户是否登录
if (username == null) {
session.setAttribute("msg", "操作前请先登录");
// 重定向到登录页面
response.sendRedirect(request.getContextPath() + "/login.jsp");
return;
}
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
LoginServlet 代码:
package net.haicoder.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// 获取用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
// 判断用户名和密码是否正确
if ("haicoder".equals(username) && "123".equals(password)) {
// 如果正确就将用户信息保存在会话域中
HttpSession session = request.getSession();
session.setAttribute("username", username);
// 跳转到list.jsp页面
response.sendRedirect("list.jsp");
}
else {
// 登录失败
request.setAttribute("msg","用户名或密码不正确");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
浏览器访问查询页面,如下图:
浏览器访问登录界面,如下图:
浏览器访问添加界面,如下图:
因为此时未登录,所以无法访问添加页面,会跳转到登录界面,如下图:
使用浏览器访问更新页面,如下图:
因为此时未登录,所以无法访问更新页面,会跳转到登录界面,如下图:
此时在登录界面,完成登录,如下图:
再次访问添加页面,如下图:
再次访问更新页面,如下图:
以上案例使用过滤器,进行权限的控制,根据用户的登录状态,控制用户的访问权限。