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拦截器? 从软件构架上来说,拦截器是实现了面向方面编程的组件.它将影响了多个业务对象的公共行为封装到一个个可重用的模块,减少了系统的重复代码,实现功能的高度内聚,确保了业务对象 ...
随机推荐
- Mysql数据库概述
阅读目录 引擎介绍 表介绍 创建表 查看表结构 mysql中的数据类型 表的完整性约束 修改表结构 删除表 多表结构的创建与分析 练习 返回顶部 引擎介绍 mysql中的存储引擎(https://ww ...
- UI常用网站
网站大全 国外的花瓣--Pinterest • The world’s catalog of ideas 字体海洋--求字体网提供中文和英文字体库下载.识别与预览服务,找字体的好帮手 原创设计UI-- ...
- tomcat 启动服务器日志小结
1.tomcat 启动服务配置: 目前主要有 ①把编译好war或者项目直接扔到webapps 目录下, 启动bin目录下的startup.bat 即可 ② 在conf目录下 修改 serve ...
- redis启动加载过程、数据持久化
背景 公司一年的部分业务数据放在redis服务器上,但数据量比较大,单纯的string类型数据一年就将近32G,而且是经过压缩后的. 所以我在想能否通过获取string数据的时间改为保存list数据类 ...
- cropper+pillow处理上传图片剪裁(一)
在写新博客的时候,遇到需要用户上传自定义图片的处理,查了一番资料,决定用cropper和pillow来处理需要剪裁的图片上传,大致思路是:前端收集用户上传的图片和用户剪裁的尺寸数据,后台接收图片后按数 ...
- JavaScript实现复选框的全选、不选、反选
方法一: <html> <head> <meta charset="utf-8"> <title>无标题文档</title&g ...
- apache出现You don't have permission to access / on this server提示的解决方法
在apache的配置文件httpd.conf里定义了对网站根默认的访问权限 #<Directory /> Options FollowSymLinks AllowOverrid ...
- 基于ACE的TAO开发---一个简单的入门实例-----VS2008(二)
上一节已经说了如何编译idl文件.现在就用编好的文件来写一个最小的corba小程序的.程序分为服务器程序和客户端程序. 说明下,代码是<基于C++CORBA高级编程>一书中的例子. 1.首 ...
- UWP Ad
1.对于 UWP 应用:使用 Visual Studio 2015 安装 Microsoft Store Services SDK 2.对于通用 Windows 平台 (UWP) 项目:展开通用 Wi ...
- Windows2012R2 时间同步设置
Windows2012R2里没有了internet时间,或者Internet时间无法同步成功,都可以尝试使用如下方法. 1.打开命令提示符, 输入:gpedit.msc,打开组策略管理器 2.执行上述 ...