Struts2登录拦截器的使用
来源:互联网 发布:webapp制作软件 编辑:程序博客网 时间:2024/05/21 11:22
在没有了解拦截器之前一直有一个困惑,就是很多网站上面在没有使用账号登录的时候,一部分页面的是访问不了的(无权访问)。知道拦截器且了解了拦截器之后,恍然大悟,原来使用一个拦截器进行判断是否有用户登录,拦截设置就可实现这一功能。
下面是一个简单的示例:
我们写的是基于Struts2框架的拦截器,所以新建项目后要先搭建Struts2框架。
Struts2框架搭建步骤点击链接:Struts2框架搭建详解
1、新建一个interceptor包,创建拦截器类(示例类命名:MyInterceptor),让其实现Interceptor接口
示例代码如下:
package com.aaa.interceptor;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;/** * @Discription * @author *** * @company *** * @date 2017-12-12 **/public class MyInterceptor implements Interceptor {public void destroy() {}public void init() {}public String intercept(ActionInvocation invocation) throws Exception {ActionContext context = ActionContext.getContext();Map<String, Object> session = context.getSession();Object object = session.get("admin"); // 判断用户是否登录if(object==null){context.put("msg", "无权访问!"); // 无登录则拦截并返回error信息return "error";}String invoke = invocation.invoke(); // 登录则放行return invoke;}}2、配置struts.xml中的拦截器及拦截器栈
示例代码如下:
<!-- 拦截器 --><interceptors><!-- 注册拦截器 --><interceptor name="myInterceptor" class="com.aaa.interceptor.MyInterceptor" /><!-- 注册拦截器栈 --><interceptor-stack name="myStack"><interceptor-ref name="myInterceptor"/><interceptor-ref name="defaultStack"/></interceptor-stack></interceptors>
3、新建action包,创建登录类
示例代码如下:
package com.aaa.action;import java.util.Map;import com.opensymphony.xwork2.ActionContext;/** * @Discription * @author *** * @company *** * @date 2017-12-12 **/public class LoginAction {/** * 访问方法 * */public String execute(){ActionContext context = ActionContext.getContext();context.put("msg", "访问成功!");return "success";}/** * 登录方法 * */public String login(){ActionContext context = ActionContext.getContext();Map<String, Object> session = context.getSession();context.put("msg", "登录成功!");session.put("admin", "admin");return "login";}/** * 退出方法 * */public String exit(){ActionContext context = ActionContext.getContext();context.put("msg", "退出成功!");Map<String, Object> session = context.getSession();session.remove("admin");return "exit";}}4、对struts.xml进行action配置
示例代码如下:
<!-- 全局拦截器栈 --><default-interceptor-ref name="myStack"/><!-- 全局返回结果 --><global-results><result name="error">/error.jsp</result></global-results><!-- 访问资源 --><action name="login" class="com.aaa.action.LoginAction"><result>/success.jsp</result></action><!-- 登录action --><action name="grant_*" class="com.aaa.action.LoginAction" method="{1}"><result name="login">/login.jsp</result><result name="exit">/exit.jsp</result><interceptor-ref name="defaultStack"/></action>
示例jsp页面省略...
示例效果如下图所示:当登录成功时才能访问,当退出登录时则无权访问。
总结:
(1)新建拦截器类实现Interceptor接口,其导入包名为:import com.opensymphony.xwork2.interceptor.Interceptor;
(2)重要的是struts.xml文件中的拦截器配置,我们新建自己的拦截器后,struts2默认的20个拦截器就会被覆盖,所以我们最好的解决办法是注册拦截器栈,把我们自定义的登录拦截器和默认的20个拦截器放在一起,且定义全局拦截器栈,使得在登录的action中使用defaultStack即可;那么当用户没有登录的时候,不止只是单个的页面不能访问,而是一部分都不能访问,所以我们最好的解决办法是配置全局返回结果,即可解决代码重复且麻烦的问题。
阅读全文
0 0
- Struts2登录拦截器的使用
- struts2第十五讲学习笔记,登录拦截器的使用
- STRUTS2 登录拦截器
- Struts2拦截器,简单的用户登录拦截及静态缓存的使用
- Struts2 自动登录的拦截器
- Struts2登录拦截器的简单示例
- Struts2拦截器的应用:登录验证
- 配置Struts2的登录拦截器
- Struts2拦截器的使用
- Struts2拦截器的使用
- struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2的拦截器使用
- struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- Struts2拦截器的使用
- java8-lambda
- 详解协方差与协方差矩阵
- 学习Tango with django(1)
- Python中的Package机制
- java8-stream
- Struts2登录拦截器的使用
- Java练习--线程(13)
- 字符串反转的七种方法(java)
- mysql 第一天
- Blog28@linux NFS网络共享文件
- 定时器计时,并在数码管实时显示时间
- 多线程vs多进程
- LightOJ 1220
- 微信开发<1>——图像拍照接口实现