Struts2拦截器实例.docx
### Struts2拦截器实例——登录校验 #### 概述 在Web应用开发中,登录验证是一项重要的功能。为了确保只有已登录的用户才能访问特定的资源或页面,通常会采用拦截器来实现这一需求。本文档将详细介绍如何在Struts2框架中创建一个登录验证拦截器,以确保除了登录请求外,所有其他请求都会被拦截,并检查用户的登录状态。 #### 登录校验拦截器的工作原理 登录校验拦截器的主要作用是检查用户是否已经登录系统。对于未登录的用户试图访问受保护的资源时,拦截器会将其重定向回登录页面。一旦用户完成登录并被验证通过后,他们就可以正常访问这些资源了。 #### 实现过程详解 **1. 前端页面实现** - **登录表单**:首先需要设计一个简单的登录表单,其中包含用户名和密码字段。表单提交至后端的Action进行处理。 ```html <body> <form action="${pageContext.request.contextPath}/UserAction_login" method="post"> 用户名:<input type="text" name="user_code"><br/> 密码:<input type="password" name="user_password"><br/> <input type="submit" value="提交"> </form> </body> ``` 在这里,`UserAction_login`是后端处理登录逻辑的Action。 **2. 编写User实体类** - **User类**:用于存储用户信息,如用户名和密码。 ```java package cn.itheima.domain; public class User { private String user_code; private String user_password; public String getUser_code() { return user_code; } public void setUser_code(String user_code) { this.user_code = user_code; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } } ``` **3. Dao层与Service层** - **Dao层**:负责从数据库中获取用户信息,并封装到User对象中。 - **Service层**:处理用户数据,包括验证用户是否存在以及密码是否正确。这部分代码示例省略,但主要包括: - 使用Dao层获取的User对象进行判断。 - 如果User对象为空,则表示账户不存在。 - 如果User对象不为空,则比较前端传递的密码和数据库中的密码是否一致。 - 根据判断结果进行相应的异常抛出或登录成功处理。 **4. Action类编写** - **UserAction类**:负责处理前端发送的登录请求。 ```java package cn.itheima.web.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import cn.itheima.domain.User; import cn.itheima.service.UserService; import cn.itheima.service.impl.UserServiceImpl; public class UserAction extends ActionSupport implements ModelDriven<User> { private User user = new User(); private UserService us = new UserServiceImpl(); public String login() throws Exception { // 1. 调用Service执行登录操作 User u = us.login(user); // 2. 将返回的User对象放入session域作为登录标识 ActionContext.getContext().getSession().put("user", u); // 3. 重定向到项目的首页 return "toHome"; } @Override public User getModel() { return user; } } ``` **5. 编写自定义拦截器** - **LoginInterceptor类**:该拦截器用于实现登录验证功能。 ```java package cn.itheima.web.interceptor; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; public class LoginInterceptor extends MethodFilterInterceptor { // 指定不拦截登录方法,其他方法都拦截 @Override protected String doIntercept(ActionInvocation invocation) throws Exception { // 1. 获取session Map<String, Object> session = ActionContext.getContext().getSession(); // 2. 判断session中是否有用户信息 if (session.get("user") != null) { // 如果存在,则放行 return invocation.invoke(); } else { // 如果不存在,则重定向到登录页面 return "login"; } } } ``` #### 配置拦截器 - 在Struts.xml配置文件中添加自定义拦截器,并设置其适用范围。 ```xml <interceptors> <interceptor name="loginInterceptor" class="cn.itheima.web.interceptor.LoginInterceptor"/> <interceptor-stack name="defaultStack"> <interceptor-ref name="loginInterceptor"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors> <action name="home" class="cn.itheima.web.action.HomeAction"> <result name="success">/home.jsp</result> <interceptor-ref name="defaultStack"/> </action> ``` #### 总结 通过以上步骤,我们可以实现一个简单的登录验证拦截器,它可以有效地控制用户的访问权限,确保只有已登录的用户才能访问特定的资源或页面。这种做法不仅提高了系统的安全性,也使得用户界面更加友好。在实际项目中,还可以根据需要进一步优化和完善这个拦截器的功能,比如增加更多的安全措施、改善用户体验等。
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![emmx](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/release/download_crawler_static/11341666/bg1.jpg)
![](https://csdnimg.cn/release/download_crawler_static/11341666/bg2.jpg)
![](https://csdnimg.cn/release/download_crawler_static/11341666/bg3.jpg)
剩余14页未读,继续阅读
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 0
- 资源: 2
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- 操作系统实验ucore lab3
- DG储能选址定容模型matlab 程序采用改进粒子群算法,考虑时序性得到分布式和储能的选址定容模型,程序运行可靠 这段程序是一个改进的粒子群算法,主要用于解决电力系统中的优化问题 下面我将对程序进行详
- final_work_job1(1).sql
- 区块链与联邦学习结合:FedChain项目详细复现指南
- 西门子S7 和 S7 Plus 协议开发示例
- 模块化多电平变流器 MMC 的VSG控制 同步发电机控制 MATLAB–Simulink仿真模型 5电平三相MMC,采用VSG控制 受端接可编辑三相交流源,直流侧接无穷大电源提供调频能量 设置频率
- 微电网(两台)主从控制孤岛-并网平滑切的分析 分析了: 1.孤岛下VF控制 2.并网下PQ控制 3.孤岛下主从控制 4.孤岛到并网的平滑切控制 5.除模型外还对分布式发电与主动配电网一些常见问题做了
- 第四组二手产品.zip
- 基于小程序的智慧物业平台源代码(java+小程序+mysql+LW).zip
- MVIMG_20241222_194113.jpg
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)