struts2自定义拦截器一——模拟登陆权限验证
1、http://localhost:8083/struts2/user.jsp 表示用户已登陆,存放session对象
2、http://localhost:8083/struts2/quit.jsp 表示用户已退出,移除session对象
3、http://localhost:8083/struts2/login/addUIHelloWorld.do 如果session存在则往下执行,否则提示“你没有权限执行该操作”
4、http://localhost:8083/struts2/login/executeHelloWorld.do 如果session存在则往下执行,否则提示“你没有权限执行该操作”
代码
user.jsp页面
<%
request.getSession().setAttribute("user", "ljq");
%>
用户已经登录
quit.jsp页面
<%
request.getSession().removeAttribute("user");
%>
用户已经退出登录
Action类
public class HelloWorldAction {
private String message;
public String addUI() {
this.message = "addUI";
return "success";
}
public String execute() throws Exception {
this.message = "execute";
return "success";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
权限类
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
/**
* 限制没有登录的用户进入访问页面
*
* @author jiqinlin
*
*/
@SuppressWarnings("serial")
public class PermissionInterceptor implements Interceptor {
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation invocation) throws Exception {
Object user = ActionContext.getContext().getSession().get("user");
// 如果user不为null,代表用户已经登录,允许执行action中的方法
if (user != null){
return invocation.invoke();
}
ActionContext.getContext().put("message", "你没有权限执行该操作");
return "success";
}
}
struts.xml配置文件
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.action.extension" value="do" />
<package name="login" namespace="/login" extends="struts-default">
<interceptors>
<!-- 自定义拦截器 -->
<interceptor name="permission" class="com.ljq.interceptor.PermissionInterceptor" />
<!-- 配制默许的拦截器到拦截器栈 -->
<interceptor-stack name="permissionStack">
<!-- 此处先后顺序好像没影响哦,我尝试过是没影响的 -->
<interceptor-ref name="permission" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 配置默认的拦截器 -->
<default-interceptor-ref name="permissionStack" />
<!-- 全局变量 -->
<global-results>
<result name="success">/WEB-INF/page/message.jsp</result>
</global-results>
<action name="*HelloWorld" class="com.ljq.action.HelloWorldAction"
method="{1}">
<!-- <interceptor-ref name="permissionStack" /> -->
</action>
</package>
</struts>
转载于:https://www.cnblogs.com/linjiqin/archive/2011/03/19/1988797.html
struts2自定义拦截器一——模拟登陆权限验证相关推荐
- struts2自定义拦截器(登陆校验拦截器)
需求:有些页面需要先登录才可以访问,如果没有登陆就跳转到login.jsp去登陆 此案例为访问login方法的时候不拦截,只有把user放到session中才可以访问别的方法 web.xml 配置拦截 ...
- Struts2 自定义拦截器
所属栏目:AbstractInterceptor 如果要编写自定义拦截器,那么可以只用实现com.opensymphony.xwork2.interceptor.Interceptor这个接口即可. ...
- struts2学习笔记--拦截器(Interceptor)和登录权限验证Demo
理解 Interceptor拦截器类似于我们学过的过滤器,是可以在action执行前后执行的代码.是我们做web开发是经常使用的技术,比如权限控制,日志.我们也可以把多个interceptor连在一起 ...
- Struts2自定义拦截器
1. 需求 自定义拦截器实现,用户登录的访问控制. 2. 定义拦截器类 1 public class LoginInterceptor extends AbstractInterceptor 2 { ...
- Struts2的拦截器只允许有权限用户访问action
1.定义拦截器,继承MethodFilterInterceptor package com.life.stuts.interceptor;import java.util.Map;import com ...
- Struts2自定义类型转换器、自定义拦截器和用户输入数据的验证
一.自定义类型转换器 1.编写一个类,继承com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter 2.覆盖掉其中的public Obj ...
- struts2自定义拦截器并配置拦截器使其生效
首先编写一个struts2的拦截器,要继承 MethodFilterInterceptor 并获取其中的方法 package star.july.d_interceptor;import com.op ...
- Struts2 自定义拦截器(方法拦截器)
转自:http://05061107cm.iteye.com/blog/365504 struts2系统自带了很多拦截器,有时需要我们自己定义,一般有两种方式: 一.实现Interceptor接口 J ...
- Struts2 自定义拦截器(easy example)
要自定义拦截器需要实现com.opensymphony.xwork2.interceptor.Interceptor接口: 新建一个MyIntercept package com.action;imp ...
最新文章
- 文件魔术数字_如何使用魔术脚手架自动创建文件并节省时间
- 同一AppDomain内的SharpGL的FrameBuffer会相互影响?
- android 引用路径,android – ClassLoader引用了未知路径:/ data...
- iPad连android热点掉线,苹果终于承认,iOS 13有这个问题,网络断连的原因找到了...
- 文本对抗攻击入坑宝典
- 磁盘测试----fio
- JavaScript获取java变量值
- PCB设计中注意事项
- 单点登录系统CAS入门
- 从一个方向看一个正方体_从一个方向观察一个正方体,最多可以看到几个面
- 趋势与新高的实战研究
- 锐龙r5 3500u核显相当于什么显卡 r5 3500u相当于什么处理器
- 【游戏】任天堂Nintendo Switch超详细购买/使用指南以及注意事项(根据自己使用持续更新中...)
- 微信小程序开发中的这些坑你遇到过吗?
- Debian如何更换apt源
- 常见的网络状态检测及分析工具
- 计算机面试常见题目-网络
- Ghost高级教程——克隆备份
- Cuckoo Hash及其性质
- zendstiduo背景色设置为保护眼睛的颜色》》》
热门文章
- .net 垃圾回收学习[How To: Use CLR Profiler][翻译学习]【2】
- CSS伪类的三种写法
- 让vs2008支持jQuery的智能提示!
- nacos怎么修改服务分组_nacos服务注册如何配置分组?
- uniq 去除重复命令使用方法介绍
- 创建和应用Java包文件的两种方式(转)
- arduinoesp8266定时器_ESP8266深度睡眠与Arduino IDE(NodeMCU)
- 实现两个数的交换(异或,加减)
- AUTOSAR从入门到精通100讲(三十九)-AUTOSAR 通信服务-Com模块两部曲-概念详解
- 人工智能AI实战100讲(三)-激光雷达原理和分类