当前位置:网站首页>SSM项目使用过滤器实现登录监听
SSM项目使用过滤器实现登录监听
2022-07-27 16:21:00 【qq_42042158】
SSM项目中配置过滤器实现登录监听,未登录不能访问某些页面
- 编写一个过滤器的类实现Filter接口,在实现方法中添加具体操作
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class LoginFilter implements Filter {
//初始化:web服务器启动时就会初始化
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response =(HttpServletResponse)servletResponse;
HttpSession session = request.getSession();
if (session.getAttribute("loginuser")==null){
response.sendRedirect(request.getContextPath()+"/login/adminLogin.jsp");
}else {
//让请求继续往下走,如果不写,到这里请求就会被拦截
filterChain.doFilter(servletRequest,servletResponse);
}
}
//销毁,web服务器关闭的时候会销毁
@Override
public void destroy() {
}
- 在web.xml中配置过滤器
<filter>
<filter-name>LoginFilter</filter-name>
<!-- 实现filter接口的类 -->
<filter-class>edu.gnnu.newcomer.aop.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<!-- 要过滤的页面 -->
<url-pattern>/manager/*</url-pattern>
</filter-mapping>
配置过后这些页面都会先经过过滤器,如果不满足过滤器中的条件则执行过滤器中定义的操作
边栏推荐
- Here are all the MySQL interview questions you can't expect (the latest version of 2022)
- Full automatic breast pump chip dltap703sd
- Talking about JVM (frequent interview)
- 迷你洗衣机触摸芯片-DLT8MA12TS-杰力科创
- org.gradle.api. UncheckedIOException: Could not load properties for module ‘gradle-kotlin-dsl‘ from C
- 连接查询和子查询
- nacos显示服务注册地址错误
- Commodity comment information and comment information classification
- Low noise anion fan touch IC
- How to realize the full-text content retrieval of word, PDF and txt files?
猜你喜欢

Music rhythm colorful gradient lamp chip -- dlt8s04a- Jericho

JDBC MySQL 01 JDBC operation MySQL (add, delete, modify and query)

Full automatic breast pump chip dltap703sd

Uni app for wechat login (to be finished)

JDBC-MySql 02 数据访问和DAO模式

Jianmu continuous integration platform v2.5.2 release

订单超时取消 及 按类别查询商品

MySQL 02 初体验

JS to realize simple form verification and select all functions

地图找房的实例
随机推荐
How to send external mail to the company mailbox server on the Intranet
How to realize the full-text content retrieval of word, PDF and txt files?
Mode= "widthfix" attribute in image tag
Uploading and downloading of files
LED学习护眼台灯触摸芯片-DLT8T10S-杰力科创
Wechat applet wechat payment overview
Arrays and objects in JS
Nodejs 模板引擎ejs
MySQL create event execution task
Examples of map search
Valueerror: found input variables with inconsistent numbers of samples: [80019456, 26673152] [error reporting]
飞机大战碰撞检测
Leetcode brushes questions the next day
Wechat applet multi file upload
Hash、Set、List、Zset、BitMap、Scan
JDBC-MySql 01 JDBC操作MySql(增删改查)
What if MySQL database forgets its password???
Infrared hyperspectral survey
npm的身份证和依赖
JS tool - Cookie simple encapsulation