过滤器通过Session实现权限拦截
程序员文章站
2022-03-09 21:36:33
...
过滤器的权限拦截
- 编写Session拦截过滤器
package com.filter;
import com.util.Constant;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 125827
*/
public class SysFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// ServletRequest HttpServletRequest
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
if (request.getSession().getAttribute(Constant.USER_SESSION) == null){
response.sendRedirect( "/Login.jsp" );
}
filterChain.doFilter(servletRequest,servletResponse);
}
public void destroy() {}
}
- 定义一个Session常量
package com.util;
/**
* @author 125827
*/
public class Constant {
public final static String USER_SESSION = "USER_SESSION";
}
- 注册过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LoginOutServlet</servlet-name>
<servlet-class>com.servlet.LoginOutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginOutServlet</servlet-name>
<url-pattern>/servlet/login_out</url-pattern>
</servlet-mapping>
<!--过滤器-->
<filter>
<filter-name>SysFilter</filter-name>
<filter-class>com.filter.SysFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SysFilter</filter-name>
<url-pattern>/sys/*</url-pattern>
</filter-mapping>
</web-app>
- 准备Servlet
package com.servlet;
import com.util.Constant;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
// 登录
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取前端请求的参数
String username = req.getParameter("username");
if(username.equals("admin")){
//登录成功
req.getSession().setAttribute(Constant.USER_SESSION,req.getSession().getId());
resp.sendRedirect("/sys/success.jsp");
}else{
resp.sendRedirect("/");
}
}
}
package com.servlet;
import com.util.Constant;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
// 登出
public class LoginOutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object user_session = req.getSession().getAttribute(Constant.USER_SESSION);
if(user_session != null ){
req.getSession().removeAttribute(Constant.USER_SESSION);
resp.sendRedirect("/Login.jsp");
}else{
resp.sendRedirect("/Login.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
- 展示页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登录</h1>
<form action="/servlet/login" method="post">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
上一篇: 设计模式---简单工厂模式(通俗易懂)
下一篇: 利用反射打印对象的所有属性及调用对象方法
推荐阅读
-
iOS 隐私权限和通过openURL实现跳转实例
-
SpringMVC拦截器实现监听session是否过期详解
-
Spring Boot使用过滤器和拦截器分别实现REST接口简易安全认证示例代码详解
-
iOS 隐私权限和通过openURL实现跳转实例
-
Spring Boot使用过滤器和拦截器分别实现REST接口简易安全认证示例代码详解
-
SpringBoot快速设置拦截器并实现权限验证的方法
-
Java Web实现session过期后自动跳转到登陆页功能【基于过滤器】
-
SpringBoot快速设置拦截器并实现权限验证的方法
-
hibernate通过session实现增删改查操作实例解析
-
AngularJS实现用户登录状态判断的方法(Model添加拦截过滤器,路由增加限制)