struts自己定义拦截器--登录权限控制
说明:该自己定义的拦截器实现用户登录的权限控制。
login.jsp--->LoginAction--重定向-->MainAction--->main.jsp
一.1.整体的步骤:
(1).定义拦截器类.LoginInterceptor(完毕登录拦截)
方式1:实现com.opensymphony.xwork2.interceptor.Interceptor接口并覆写方法.
方式2:继承com.opensymphony.xwork2.interceptor.AbstractInterceptor类,覆写intercept方法.
(2).在struts.xml中注冊该拦截器
(3).告诉<action>使用该拦截器
<action name="login" class="cn.wwh.www.web.interceptor.LoginAction">
<!-- 该Action引用哪一个拦截器 -->
<interceptor-ref name="loginInterceptor" />
<result type="redirectAction">
<param name="actionName">main.action</param>
</result>
</action>
可是第三步操作完,Action中再也获取不到请求參数.
<default-interceptor-ref name="defaultStack"/> 在struts-default.xml配置
<action>都有默认的拦截器,一旦显示的引用了其它的拦截器,默认的就被取消了.
解决方式:先引用defaultStack,再引用loginInterceptor.
<action name="login" class="cn.wwh.www.web.interceptor.LoginAction">
<!-- 引用默认拦截器(接受请求參数等功能), -->
<interceptor-ref name="defaultStack"/>
<!-- 该Action引用loginInterceptor拦截器 -->
<interceptor-ref name="loginInterceptor" />
<result type="redirectAction">
<param name="actionName">main.action</param>
</result>
</action>
上述引用的两个拦截器都定义在某一个<action>元素中,若此时同<package>中多个<action>也要引用这两个拦截器.
那么的都要拷贝这两行代码.
解决方式:我们也定义拦截器栈
<!--告诉当前<package>默认使用的拦截器栈 -->
<default-interceptor-ref name="myStack" />
二.代码实战:
1.LoginInterceptor.java:
<strong>
</strong>
<strong>package cn.wwh.www.web.interceptor; import java.util.Arrays;
import java.util.List; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; /**
*类的作用:自己定义拦截器类,继承AbstractInterceptor。
*
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014-8-16 下午05:10:23
*/
public class LoginInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
private List<String> actionNames; // 在系统启动自己主动为actionNames属性设置值
public void setActionNames(String actionNames) {
System.out.println("--->" + actionNames);
// 将配置文件里的參数,利用Arrays工具拆一个集合
<span style="color:#ff0000;">this.actionNames = Arrays.asList(actionNames.split(","));</span>
} // 拦截方法
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// 若Session中有key为USER_IN_SESSION的对象,就应该放行
ActionContext ctx = invocation.getInvocationContext();
Object user = ctx.getSession().get("USER_IN_SESSION");// 从Session中去获取
// 若当前Action的名字等于login也应该放行
String actionName = invocation.getProxy().getActionName();// 获取当前Action的名字
System.out.println(actionName);
System.out.println("LoginInterceptor:" + user);
// 当前的action是login,或者有正确的用户登录时。程序放行。然而是main时。并不包括在actionNames中。
// 则不能放其通过,所以if的推断条件中的应该是||而不是&&
if <span style="color:#3333ff;">(user != null</span><span style="color:#ff0000;"> ||</span><span style="color:#3333ff;"> this.actionNames.contains(actionName))</span> {// 登陆
System.out.println("通过。进入程序的主界面!");
return invocation.invoke();// 放行
}
System.out.println("还没登陆,请登录!");
// 否则到登陆界面
return Action.LOGIN;// "login"
}
}</strong>
2.登录界面:login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head> <body>
<form action="/StrutsExercise5/interceptor/login" method="post">
名字:<input type="text" name="username"/><br/>
密码:<input type="text" name="password"/><br/>
<input type="submit" value="登陆"/><br/>
</form> </body>
</html>
3.action类:
package cn.wwh.www.web.interceptor; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; /**
*类的作用:
*
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014-8-16 下午05:17:12
*/ public class LoginAction extends ActionSupport implements ModelDriven<User> {
private static final long serialVersionUID = 1L;
private User user = new User(); public String execute() throws Exception {
System.out.println(user); //两种方式将数据存储到session中,把User对象放入Session
//ServletActionContext.getRequest().getSession().setAttribute(name, value)
<span style="color:#ff0000;">ActionContext.getContext().getSession().put("USER_IN_SESSION", user);</span> return SUCCESS;
} public User getModel() {
return user;
}
}
4.实体类:User.java
package cn.wwh.www.web.interceptor; import java.io.Serializable; /**
*类的作用:
*
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014-8-16 下午05:03:09
*/ public class User implements Serializable {
private static final long serialVersionUID = 1L; private String username;
private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Override
public String toString() {
return "User --->\n username=" + username + "\n password=" + password;
} }
5.用户验证成功后的页面:main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
已经成功登录,进入程序的main界面! </body>
</html>
6.main的action类:
package cn.wwh.www.web.interceptor; import com.opensymphony.xwork2.ActionSupport; /**
*类的作用:
*
*
*@author 一叶扁舟
*@version 1.0
*@创建时间: 2014-8-16 下午05:18:16
*/
public class MainAction extends ActionSupport {
private static final long serialVersionUID = 1L; public String execute() throws Exception {
return SUCCESS;
}
}
7.interceptor.xml配置文件(这个要在struts.xml文件里用include包括进去)
<? xml version="1.0" encoding="UTF-8" ? >
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="interceptor" extends="struts-default" namespace="/interceptor"> <span style="color:#3333ff;"><interceptors>
<!-- 注冊一个拦截器,而且该拦截器必须在action之前 -->
<interceptor name="loginInterceptor"
class="cn.wwh.www.web.interceptor.LoginInterceptor">
<!-- 设置 LoginInterceptor,不须要拦截的action的name-->
<param name="actionNames">login,userLogin,xxAction,ooAction</param>
</interceptor>
<!-- 定义一个拦截器栈 -->
<interceptor-stack name="myStack">
<!-- 引用默认拦截器(接受请求參数等功能), -->
<interceptor-ref name="defaultStack" />
<!-- 该Action引用loginInterceptor拦截器 -->
<interceptor-ref name="loginInterceptor" />
</interceptor-stack>
</interceptors></span> <span style="color:#ff0000;"><!--告诉当前<package>默认使用的拦截器栈 -->
<default-interceptor-ref name="myStack" />
</span>
<!-- 配置全局的结果视图 -->
<global-results>
<result name="login">/views/interceptor/login.jsp</result>
</global-results> <action name="login" class="cn.wwh.www.web.interceptor.LoginAction">
<result type="redirectAction">
<!--
由于是同一个命名空间的Action跳转,所以不是必需配置namespace: <param
name="namespace">/interceptor</param>
-->
<param name="actionName">main.action</param>
</result>
</action> <action name="main" class="cn.wwh.www.web.interceptor.MainAction">
<result>/views/interceptor/main.jsp</result>
</action>
</package>
</struts>
注意:每一个包仅仅能指定一个默认拦截器。
另外,一旦我们为该包中的某个action显式指定了某个拦截器。
则默认拦截器不会起作用。
struts自己定义拦截器--登录权限控制的更多相关文章
- Struts2使用拦截器完成权限控制示例
http://aumy2008.iteye.com/blog/146952 Struts2使用拦截器完成权限控制示例 示例需求: 要求用户登录,且必须为指定用户名才可以查看系统中某个视图资源:否 ...
- Struts2基础-4-2 -struts拦截器实现权限控制案例+ 模型驱动处理请求参数 + Action方法动态调用
1.新建项目,添加jar包到WEB-INF目录下的lib文件夹,并添加到builde path里面 整体目录结构如下 2.新建web.xml,添加struts2核心过滤器,和默认首页 <?xml ...
- 6、Struts2拦截器实现权限控制
1.创建如下项目结果 2.在com.entity包下创建 package com.entity; public class User { private String name; private St ...
- 使用Struts 拦截namespace进行权限控制
有时候我需要在几个包下都需要进行同一个权限控制.如在购物网站中,我们需要进入个人中心.下订单.评价商品等等都需要进行登录权限控制,但是这几个模块并不是位于同一个package下.Struts提供的拦截 ...
- Struts2通过自己定义拦截器实现登录之后跳转到原页面
这个功能对用户体验来说是非常重要的.实现起来事实上非常easy. 拦截器的代码例如以下: package go.derek.advice; import go.derek.entity.User; i ...
- Struct2_定义拦截器并使用注解方式作用在Action的方法中
一.目的:通过在方法上加注解控制哪些方法需要登陆后才能访问 二.方式:利用拦截器判断用户是否登陆 三.实现步骤 定义配置文件struts.xml添加节点 1 2 3 4 5 6 7 8 9 1 ...
- Struts2(十四)拦截器实现权限管理
一.认识拦截器 拦截器也是一个类 拦截器可以在Action被调用之前和之后执行代码 框架很多核心功能是拦截器实现的 拦截器的特点: 拦截器自由组合,增强了灵活性.扩展性.有利于系统解耦 拦截器可以拦截 ...
- 附:Struts2-CRM,拦截器实现权限访问
拦截器代码: package mycrm.interceptor; import org.apache.struts2.ServletActionContext; import com.opensym ...
- Struts 2的拦截器(Interceptor)总结
什么是Struts 2拦截器? 从软件构架上来说,拦截器是实现了面向方面编程的组件.它将影响了多个业务对象的公共行为封装到一个个可重用的模块,减少了系统的重复代码,实现功能的高度内聚,确保了业务对象 ...
随机推荐
- soapui icon以及resource的理解
https://www.soapui.org/getting-started/soapui-interface/main-window.html 以调用博客园的api为例 第一个是域名 第二个是res ...
- devenv.exe 编译Solution
Build https://docs.microsoft.com/en-us/visualstudio/ide/reference/build-devenv-exe Builds a solution ...
- js 获取现在时间一个月(N天)后的日期
欢迎加入前端交流群交流知识&&获取视频资料:749539640 let today = new Date().getTime() let lastDay = getTimeByDay( ...
- [Pulgin] 前端上传组件Plupload使用指南
我之前写过一篇文章<文件上传利器SWFUpload使用指南>,里面介绍了上传组件SWFUpload的使用方法,但现在随着html5技术的逐渐推广和普及,再去使用以flash为上传手段的SW ...
- yii依赖注入
为了降低代码耦合程度,提高项目的可维护性,Yii采用多许多当下最流行又相对成熟的设计模式,包括了依赖注入(Denpdency Injection, DI)和服务定位器(Service Locator) ...
- Redis(三)、Redis主从复制
一.主从复制 主从复制:主节点负责写数据,从节点负责读数据,从而实现读写分离,提高redis的高可用性. 让一个服务器去复制(replicate)另一个服务器,我们称呼被复制的服务器为主节点(mast ...
- BZOJ 3991 set维护dfs序
思路: set按照dfn排序 两点之间的距离可以O(logn)算出来 加一个点-> now ans+=dis(pre,now)+dis(now,next)-dis(pre-next); 删一个点 ...
- A - Dubstep
Problem description Vasya works as a DJ in the best Berland nightclub, and he often uses dubstep mus ...
- C# CultureInfo.InvariantCulture
今天在写代码的过程中发现了一个有意思的问题,我在写了一个日期格式转化的时候发现不同电脑的运行结果不一致. 代码如下 string str = this.tbTime.Text; if(string.I ...
- javascript的基础知识及面向对象和原型属性
自己总结一下javascript的基础知识,希望对大家有用,也希望大家来拍砖,毕竟是个人的理解啊 1.1 类型检查:typeof(验证数据类型是:string) var num = 123; cons ...