当前位置:网站首页>拦截器实现网页用户登陆
拦截器实现网页用户登陆
2022-06-21 16:08:00 【冷丁_】
拦截器实现网页用户登陆
实现思路
1.有一个登陆页面,需要写一个controller访问页面。
2. 登陆页面有一提交表单的动作。需要在controller中处理。判断用户名密码是否正确。如果正确,向session中写入用户信息。返回登陆成功。
3. 拦截用户请求,判断用户是否登陆。如果用户已经登陆。放行, 如果用户未登陆,跳转到登陆页面
1.编写一个登陆页面login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<h1>登陆页面</h1>
<hr>
<body>
<form action="${pageContext.request.contextPath}/user/login">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="pwd"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
2.编写一个Controller处理请求
@Controller
@RequestMapping("/user")
public class UserController {
//跳转到登陆页面
@RequestMapping("/jumplogin")
public String jumpLogin(){
return "login";
}
//跳转到成功页面
@RequestMapping("/jumpSuccess")
public String jumpSuccess(){
return "success";
}
//登陆提交
@RequestMapping("/login")
public String login(HttpSession session, String username, String pwd){
//向session记录用户身份信息
System.out.println("接收前端==="+username);
session.setAttribute("user",username);
return "success";
}
//退出登陆
@RequestMapping("logout")
public String logout(HttpSession session){
//session过期
session.invalidate();
return "login";
}
}
3.编写一个登陆成功的页面success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登陆成功页面</h1>
<hr>
${
user}
<a href="${pageContext.request.contextPath}/user/logout">注销</a>
</body>
</html>
4.在 index 页面上测试跳转!启动Tomcat 测试,未登录也可以进入主页!
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1>首页</h1>
<hr>
<%-- 登陆--%>
<a href="${pageContext.request.contextPath}/user/jumplogin">登陆</a>
<a href="${pageContext.request.contextPath}/user/jumpSuccess">成功页面</a>
</body>
</html>
5.编写用户登陆拦截器
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//如果是登陆页面则放行
System.out.println("uri: "+request.getRequestURI());
if(request.getRequestURI().contains("login")){
return true;
}
HttpSession session=request.getSession();
//如果用户已登陆也放行
if(session.getAttribute("user")!=null){
return true;
}
//用户没有登陆跳转到登陆页面
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
return false;
}
}
在springmvc文件中配置拦截器
<!-- 拦截器配置-->
<mvc:interceptors>
<mvc:interceptor>
<!-- /**包括路径及其子路径-->
<!-- /adimin/* 拦截的是/admin/add等等这种,/admin/add/user不会被拦截-->
<!-- /admin/** 拦截的是/admin/下的所有-->
<mvc:mapping path="/**"/>
<bean class="com.lding.config.MyInterceptor"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/user/**"/>
<bean id="loginInterceptor" class="com.lding.config.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>

直接点击成功页面进不去,会自动跳转到登陆页面,登陆后才能进入成功页面

如果对您有帮助,免费的赞点一个~~~感谢

边栏推荐
- Software test system learning and construction (13) - basic requirements for test engineers of test foundation
- 拉格朗日插值
- Clickhouse learning notes 2: basic use tutorial
- My gadget - card learning app is complete
- Characteristic requirements of MES system in sheet metal industry
- The node server res.end() writes Chinese, and the solution to the problem of garbled code in the client
- Common setting modes
- Kotlin DSL构建
- PTA L3-032 关于深度优先搜索和逆序对的题应该不会很难吧这件事 (30 分)
- 窗帘做EN 1101易燃性测试过程是怎么样的?
猜你喜欢

PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分

Google play 应用签名密钥证书,上传签名证书区别

为什么RedisCluster设计成16384个槽?

火山引擎+焱融 YRCloudFile,驱动数据存储新增长

How can aggressive programmers improve R & D efficiency Live broadcast Preview

Analysis of 43 cases of MATLAB neural network: Chapter 26 classification of LVQ Neural Network - breast tumor diagnosis

Why is rediscluster designed with 16384 slots?

Vscade tool

ViT杀疯了,10+视觉Transformer模型详解

Jetpack compose status promotion (II)
随机推荐
How can aggressive programmers improve R & D efficiency Live broadcast Preview
One trick: let logs help you make decisions through Yanrong SaaS data service platform +elk
The fundamental task of Natural Science
Behind Yanrong SaaS service platform, which is as stable as a rock, is the rise of data ecology
加速雲原生應用落地,焱融 YRCloudFile 與天翼雲完成兼容性認證
list的模拟实现
[Error] ‘vector‘ was not declared in this scope
一招教你通过焱融 SaaS 数据服务平台+ELK 让日志帮你做决策
Clickhouse learning notes 2: basic use tutorial
窗帘做EN 1101易燃性测试过程是怎么样的?
node服务器 res.end()中写中文,客户端中乱码问题的解决方法
IEC62133与EN62133有何区别?主要测试哪些项目?
【没搞懂路由策略?盘它!】
What is the difference between IEC62133 and EN62133? Which items are mainly tested?
室内膨胀型防火涂料根据BS 476-21 耐火标准测定需要符合几项?
clickhouse学习笔记2:基本使用教程
Algorithm -- maximum number after parity exchange (kotlin)
Is it safe and reliable to open futures accounts on koufu.com?
经纬度转换为距离
BFS and DFS