应用情形:在web项目中,经常会遇到用户未登录或SESSION失效时用户发出非法的权限操作,如新闻的评论、文件的下载等等,在此我们可以使用struts拦截器对该用户发出的请求进行拦截,拦截后判断用户是否登录或SESSION是否有效,然后进行其正常操作。具体实例如下:

新建一个拦截器类UserInterceptor ,UserInterceptor.java代码如下

[java] view plaincopyprint?
  1. package com.hsinghsu.test.interceptor;
  2. import com.opensymphony.xwork2.*;
  3. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
  4. import java.util.*;
  5. import javax.servlet.http.HttpServletRequest;
  6. import org.apache.struts2.ServletActionContext;
  7. public class UserInterceptor extends AbstractInterceptor {
  8. private static final long serialVersionUID = 4956767125951165062L;
  9. // 拦截Action处理的拦截方法
  10. public String intercept(ActionInvocation invocation) throws Exception {
  11. // 取得请求相关的ActionContext实例
  12. ActionContext ctx = invocation.getInvocationContext();
  13. Map<String, Object> session = ctx.getSession();
  14. // 取出名为user的Session属性
  15. String user = (String) session.get("user");
  16. // 如果已经登录,放行
  17. if (user != null && user.equals("hsing")) {
  18. return invocation.invoke();
  19. }
  20. // 获取HttpServletRequest对象
  21. HttpServletRequest req = ServletActionContext.getRequest();
  22. // 获取此请求的地址
  23. String path = req.getRequestURI();
  24. System.out.println("path:" + path);
  25. // 存入session,方便调用
  26. session.put("prePage", path);
  27. // 没有登录,将服务器提示设置成一个HttpServletRequest属性
  28. ctx.put("tip", "您还没有登录,请输入hsing,hsu登录系统");
  29. // 直接返回login的逻辑视图
  30. return "login";
  31. }
  32. }
package com.hsinghsu.test.interceptor;import com.opensymphony.xwork2.*;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;public class UserInterceptor extends AbstractInterceptor {private static final long serialVersionUID = 4956767125951165062L;// 拦截Action处理的拦截方法public String intercept(ActionInvocation invocation) throws Exception {// 取得请求相关的ActionContext实例ActionContext ctx = invocation.getInvocationContext();Map<String, Object> session = ctx.getSession();// 取出名为user的Session属性String user = (String) session.get("user");// 如果已经登录,放行if (user != null && user.equals("hsing")) {return invocation.invoke();}// 获取HttpServletRequest对象HttpServletRequest req = ServletActionContext.getRequest();// 获取此请求的地址String path = req.getRequestURI();System.out.println("path:" + path);// 存入session,方便调用session.put("prePage", path);// 没有登录,将服务器提示设置成一个HttpServletRequest属性ctx.put("tip", "您还没有登录,请输入hsing,hsu登录系统");// 直接返回login的逻辑视图return "login";}
}

新建登录action,LoginAction.java代码如下:

[java] view plaincopyprint?
  1. package com.hsinghsu.test.action;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import com.opensymphony.xwork2.ActionContext;
  4. import java.util.*;
  5. public class LoginAction extends ActionSupport {
  6. private static final long serialVersionUID = 8013816027944871760L;
  7. private String username;// 登录用户名
  8. private String password;// 登录密码
  9. private String prePage;// 登录前页面
  10. public String execute() throws Exception {
  11. if (null != username && null != password && username.equals("hsing") && password.equals("hsu")) {
  12. ActionContext ctx = ActionContext.getContext();
  13. Map<String, Object> session = ctx.getSession();
  14. //保存用户信息session
  15. session.put("user", getUsername());
  16. // 获取跳转到登陆界面之前的页面地址,由拦截器提供
  17. prePage = (String) session.get("prePage");
  18. // 清除session中的数据
  19. session.remove("prePage");
  20. if (null == prePage) {
  21. return "usercenter";// 不是拦截器跳转到登陆页面的,直接访问的登陆页面
  22. } else {
  23. return SUCCESS;// 是拦截器跳转到登陆登录前页面
  24. }
  25. } else {
  26. return INPUT;
  27. }
  28. }
  29. public void setUsername(String username) {
  30. this.username = username;
  31. }
  32. public String getUsername() {
  33. return this.username;
  34. }
  35. public void setPassword(String password) {
  36. this.password = password;
  37. }
  38. public String getPassword() {
  39. return this.password;
  40. }
  41. public String getPrePage() {
  42. return prePage;
  43. }
  44. public void setPrePage(String prePage) {
  45. this.prePage = prePage;
  46. }
  47. }
package com.hsinghsu.test.action;import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;import java.util.*;public class LoginAction extends ActionSupport {private static final long serialVersionUID = 8013816027944871760L;private String username;// 登录用户名private String password;// 登录密码private String prePage;// 登录前页面public String execute() throws Exception {if (null != username && null != password && username.equals("hsing") && password.equals("hsu")) {ActionContext ctx = ActionContext.getContext();Map<String, Object> session = ctx.getSession();//保存用户信息sessionsession.put("user", getUsername());// 获取跳转到登陆界面之前的页面地址,由拦截器提供prePage = (String) session.get("prePage");// 清除session中的数据session.remove("prePage");if (null == prePage) {return "usercenter";// 不是拦截器跳转到登陆页面的,直接访问的登陆页面} else {return SUCCESS;// 是拦截器跳转到登陆登录前页面}} else {return INPUT;}}public void setUsername(String username) {this.username = username;}public String getUsername() {return this.username;}public void setPassword(String password) {this.password = password;}public String getPassword() {return this.password;}public String getPrePage() {return prePage;}public void setPrePage(String prePage) {this.prePage = prePage;}
}

配置拦截器与action映射关系,struts.xml代码如下:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
  4. "http://struts.apache.org/dtds/struts-2.1.7.dtd">
  5. <struts>
  6. <constant name="struts.custom.i18n.resources" value="globalMessages" />
  7. <constant name="struts.i18n.encoding" value="UTF-8" />
  8. <package name="hsinghsu" extends="struts-default">
  9. <!-- 用户拦截器定义 -->
  10. <interceptors>
  11. <interceptor name="userInterceptor" class="com.hsinghsu.test.interceptor.UserInterceptor" />
  12. </interceptors>
  13. <!-- 定义全局result -->
  14. <global-results>
  15. <result name="login">/jsp/login.jsp</result>
  16. </global-results>
  17. <action name="loginPro" class="com.hsinghsu.test.action.LoginAction">
  18. <result name="success" type="redirectAction">${prePage}</result>
  19. <result name="input">/jsp/login.jsp</result>
  20. <result name="usercenter">/jsp/userCenter.jsp</result>
  21. </action>
  22. <action name="productList">
  23. <result name="success">/jsp/productList.jsp</result>
  24. <interceptor-ref name="defaultStack" /> <!-- 默认拦截器 -->
  25. <interceptor-ref name="userInterceptor" /> <!-- 应用自定义拦截器 -->
  26. </action>
  27. </package>
  28. </struts>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts><constant name="struts.custom.i18n.resources" value="globalMessages" /><constant name="struts.i18n.encoding" value="UTF-8" /><package name="hsinghsu" extends="struts-default"><!-- 用户拦截器定义 --><interceptors><interceptor name="userInterceptor" class="com.hsinghsu.test.interceptor.UserInterceptor" /></interceptors><!-- 定义全局result --><global-results><result name="login">/jsp/login.jsp</result></global-results><action name="loginPro" class="com.hsinghsu.test.action.LoginAction"><result name="success" type="redirectAction">${prePage}</result><result name="input">/jsp/login.jsp</result><result name="usercenter">/jsp/userCenter.jsp</result></action><action name="productList"><result name="success">/jsp/productList.jsp</result><interceptor-ref name="defaultStack" /> <!-- 默认拦截器 --><interceptor-ref name="userInterceptor" /> <!-- 应用自定义拦截器 --></action></package>
</struts>

登录页面login.jsp代码如下:

[html] view plaincopyprint?
  1. <%@ page contentType="text/html; charset=utf-8" language="java"
  2. errorPage=""%>
  3. <%@ taglib prefix="s" uri="/struts-tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  5. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8. <title>登录页面</title>
  9. </head>
  10. <body>
  11. <h3>用户登录</h3>
  12. ${tip}
  13. <s:form action="loginPro">
  14. <s:textfield name="username" label="用户名" />
  15. <s:password name="password" label="密码" />
  16. <s:submit value="登录" />
  17. </s:form>
  18. </body>
  19. </html>
<%@ page contentType="text/html; charset=utf-8" language="java"errorPage=""%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>登录页面</title>
</head>
<body><h3>用户登录</h3>${tip}<s:form action="loginPro"><s:textfield name="username" label="用户名" /><s:password name="password" label="密码" /><s:submit value="登录" /></s:form>
</body>
</html>

产品列表页面productList.jsp代码如下:

[html] view plaincopyprint?
  1. <%@ page contentType="text/html; charset=utf-8" language="java"
  2. errorPage=""%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  4. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <title>产品列表</title>
  8. </head>
  9. <body>
  10. <h2>水果:</h2>
  11. 苹果<br/> 橘子<br/> 香蕉<br/>
  12. </body>
  13. </html>
<%@ page contentType="text/html; charset=utf-8" language="java"errorPage=""%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>产品列表</title>
</head>
<body><h2>水果:</h2>苹果<br/> 橘子<br/> 香蕉<br/>
</body>
</html>

用户中心userCenter.jsp代码如下:

[html] view plaincopyprint?
  1. <%@ page contentType="text/html; charset=utf-8" language="java"
  2. errorPage=""%>
  3. <%@ taglib prefix="s" uri="/struts-tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  5. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8. <title>成功页面</title>
  9. </head>
  10. <body>个人用户中心,您已经登录!
  11. </body>
  12. </html>
<%@ page contentType="text/html; charset=utf-8" language="java"errorPage=""%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>成功页面</title>
</head>
<body>个人用户中心,您已经登录!
</body>
</html>

验证:
情形一:若用户未登录,输入http://localhost:8686/testInterceptor/productList.action
则系统会自动跳转到login.jsp页面,进行用户登录,登录后系统会自动跳到productList.jsp前台展现页面。
情形二:若用户已登录,输入http://localhost:8686/testInterceptor/productList.action
则系统直接跳转到productList.jsp前台展现页面。
情形三:若用户未登录,输入http://localhost:8686/testInterceptor/testInterceptor/loginPro.action
则系统会自动跳转到login.jsp页面,进行用户登录,登录后系统会自动跳到userCenter.jsp前台展现页面。

Struts 拦截器权限控制【通过拦截器实现登录后跳转到登录前页面】相关推荐

  1. php拦截登录页面跳转页面,PHP未登录自动跳转到登录页面

    PHP未登录自动跳转到登录页面 下面一段代码给大家分享php未登录自动跳转到登录页面,具体代码如下所示: namespace Home\Controller; use Think\Controller ...

  2. 案例分享:Qt西门子机床人机界面以及数据看板定制(西门子通讯,mysql数据库,生产信息,参数信息,信息化看板,权限控制,播放器,二维图表,参数调试界面)

    若该文为原创文章,转载请注明原文出处 本文章博客地址:https://blog.csdn.net/qq21497936/article/details/118685521 长期持续带来更多项目与技术分 ...

  3. python描述器做权限控制_Python装饰器14-描述器

    描述器 这是Python一个重要的概念,英文名:Descriptor descriptor是对象的一个属性,只不过它存在于类的dict中并且有特殊方法get(可能还有set和__delete)而具有一 ...

  4. 项目一:第十二天 1、常见权限控制方式 2、基于shiro提供url拦截方式验证权限 3、在realm中授权 5、总结验证权限方式(四种) 6、用户注销7、基于treegrid实现菜单展示...

    1 课程计划 1. 常见权限控制方式 2. 基于shiro提供url拦截方式验证权限 3. 在realm中授权 4. 基于shiro提供注解方式验证权限 5. 总结验证权限方式(四种) 6. 用户注销 ...

  5. 案例分享:Qt激光加工焊接设备信息化软件研发(西门子PLC,mysql数据库,用户权限控制,界面设计,参数定制,播放器,二维图,期限控制,参数调试等)

    若该文为原创文章,转载请注明原文出处 本文章博客地址:https://hpzwl.blog.csdn.net/article/details/127669684 长期持续项目技术分享,有专业Qt需求请 ...

  6. 案例分享:Qt工程机械真空激光焊接系统软件产品定制(西门子PLC,mysql数据库,用户权限控制,界面配置,参数定制,播放器,二维图标,rgv小车,期限控制,参数调试等)

    若该文为原创文章,转载请注明原文出处 本文章博客地址:https://hpzwl.blog.csdn.net/article/details/127214512 长期持续项目技术分享,有需求请回博客首 ...

  7. vue---axios拦截器处理登录失效跳转登录页

    axios拦截器(Interceptors)主要分为: (1)请求拦截器:在发送请求前进行拦截,可以根据发送的请求参数做一些发送参数的调整,例如设置headers (2)响应拦截器:在后台返回响应时进 ...

  8. axios拦截器 config_vue中axios拦截器的使用

    1.拦截器分为request请求拦截器和response响应拦截器 PS:request请求拦截器:发送请求前统一处理,如:设置请求头headers.应用的版本号.终端类型等. response响应拦 ...

  9. struts 权限控制

    今天结合Java的Annotation和Struts2进行注解拦截器权限控制. 功能需求:添加.查找.删除三个功能,添加.查找功能需进行权限拦截判断,删除功能则不需进行权限拦截判断. 操作流程如下:客 ...

最新文章

  1. Linux下新增硬盘处理过程
  2. mysql error 10048_mysql 连接异常(10048)
  3. c#操作xml实例 2009-03-13 20:00
  4. swift中的@objc
  5. Linux内核源码行数,Linux源代码已超过1000万行 价值达5亿美金
  6. linux usb 升级脚本,linux – 使用bash脚本更新CRON
  7. key map 模糊查找_lua脚本语言批量删除模糊查询的key
  8. 可视化管理_RFID技术实施智能仓储管理可视化
  9. ChaiNext:比特币打穿4.2万美元,市场多空胶着
  10. Crackeme021
  11. HTML+CSS+JavaScript仿京东购物商城网站 web前端制作服装购物商城 html电商购物网站...
  12. 在线抖音去水印,下载抖音背景音乐
  13. 计算机c盘突然少了几个G,做系统时c盘显示0容量-关于Windows系统c盘突然没了十几个g...
  14. 啃书:《利用Python进行数据分析》第一章
  15. pytorch中dataloader的num_workers参数
  16. mac 终端查看端口命令
  17. list中移除元素:用remove(i)方法遇到的坑以及替代方法
  18. JavaScript (上篇)
  19. 《青山翠影》叁 爱情的模样 | 似懂非懂
  20. 如何解决Win11系统崩溃的问题?

热门文章

  1. python六角形的绘制
  2. Java8 EnumMap 源码分析
  3. VTK:BSP树时序用法实战
  4. wxWidgets:启动默认浏览器
  5. boost::ratio_greater_equal相关的测试程序
  6. boost::mp11::mp_iterate相关用法的测试程序
  7. 基于Boost::beast模块的异步HTTP客户端
  8. Boost:不受约束的集合bimap的测试程序
  9. Boost:与容器相关的BOOST_TEST_EQ测试
  10. DCMTK:OFStandard类的测试程序