Struts2使用Interceptor实现权限控制的应用实例详解

拦截器:是Struts2框架的核心,重点之重。因此,对于我们要向彻底学好Struts2.0.读源码和使用拦截器是必不可少的。少说了。下面就Interceptor在Struts2中的一个非常常用的例子进行解析。网上也找了很多的例子,感觉都是讲的不太详细,自己从网上找了许多资料,下面就自己对其理解进行分析。
     首先,权限控制,就是,当我们使用不同的用户对某个模块或是系统进行操作的时候可以根据其不同的权限进行不同的设置。本博文就其简单的分析一下,好让自己理解。我是对于一个登陆的用户,若是其没有登录成功到该系统,则当其在浏览器中直接输入地址进行访问的时候我自定义的拦截器AuthorityInterceptor.java类将会进行判断,从而达到进行权限的判断。禁止其直接进行访问,只有当我们成功登录之后,session中存在username=clark的Map设置的时候才可以访问相应的页面。从而达到了权限控制。代码如下:
自定义拦截器:
package com.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
 * 权限控制拦截器
 * @author Administrator
 *
 */
public class AuthorityInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
//取得请求相关的ActionContext实例
ActionContext cxt = invocation.getInvocationContext();
Map session = cxt.getSession();
String username = (String)session.get("username");
//如果用户没有登录,或者是登录的用户名不是clark,都不准其登录
if(username != null && username.equals("clark")){
return invocation.invoke();//会自动调用下一个拦截器或者放行到Action的execute方法
}
//没有登录,将服务器提示设置成一个HttpServletRequest
cxt.put("tip", "您还没有登录,请登录系统");
return Action.LOGIN;
}
}
     Struts2 的Action
package com.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 20130924L;
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;
}
public static long getSerialversionuid() {
return serialVersionUID;
}

@Override
public String execute() throws Exception {
if(isInvalid(getUsername())){
return INPUT;
}
if(isInvalid(getPassword())){
return INPUT;
}
if((getUsername().equals("clark"))&&(getPassword().equals("123456"))){
//通过ActionContext对象访问Web应用的Session
ActionContext.getContext().getSession().put("username", getUsername());
ActionContext.getContext().getSession().put("password", getPassword());
System.out.println(getUsername()+"---------"+getPassword());
return SUCCESS;
}else{
return ERROR;
}
}
public boolean isInvalid(String value) {
return (value == null || value.length() == 0);
}
public String add(){
return SUCCESS;
}
public String show(){
return SUCCESS;
}
public String query(){
return SUCCESS;
}
}

  struts.xml配置文件如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
  <struts>
  <include file="struts-default.xml"></include>
  <!-- 不受权限控制的Action请求配置 -->
  <package name="non-authority" extends="struts-default">
  <action name="login" class="com.action.LoginAction">
  <result name="input">/login.jsp</result>
  <result name="error">/fail.jsp</result>
  <result name="success">/welcome.jsp</result>
  </action>
  <action name="query" class="com.action.LoginAction" method="query">
  <result name="success">/query.jsp</result>
  </action>
  </package>
  <!-- 受权限控制的Action请求配置 -->
  <package name="authority" extends="struts-default">
  <!-- 定义一个拦截器,用于权限控制 -->
  <interceptors>
  <interceptor name="authority" class="com.interceptor.AuthorityInterceptor">
  </interceptor>
  <interceptor-stack name="mydefault">
  <interceptor-ref name="defaultStack"></interceptor-ref>
  <interceptor-ref name="authority"></interceptor-ref>
  </interceptor-stack>
  </interceptors>
  <!-- 配置默认的interceptor -->
  <default-interceptor-ref name="mydefault"></default-interceptor-ref>
  <!-- 配置全局Result -->
  <global-results>
  <result name="login">/login.jsp</result>
  </global-results>
  <action name="show" class="com.action.LoginAction" method="show">
  <result name="success">/show.jsp</result>
  <!-- <interceptor-ref name="mydefault"></interceptor-ref>  -->
  </action>
  <action name="add" class="com.action.LoginAction" method="add">
  <result name="success">/add.jsp</result>
  <!-- <interceptor-ref name="mydefault"></interceptor-ref>-->
  </action>
  </package>
  </struts>
相应的jsp页面如下:
login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags"  prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>login page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
  <h1>欢迎来到登录页面</h1>
    <s:form action="login">
    <s:textfield name="username" label="用户名"/><br/>
    <s:textfield name="password" label="密码"/><br/>
    <s:submit value="登录"/><br/>
    </s:form>
  </body>
</html>
welcome.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags"  prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>login success</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    <s:text name="succTip" />
    <br>
    <p />
    <s:a href="show.action">show</s:a>
    <p />
    <s:a href="add.action">add</s:a>
    <p />
    <s:a href="query.action">query</s:a>
    <p />
  </body>
</html>

fail.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags"  prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>login fail</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    <s:text name="failTip" />
    <br>
    <p />
    <s:a href="login.jsp">return</s:a>
  </body>
</html>

add.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags"  prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>add page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    <s:text name="addTip" />
    <p/>
    <s:a href="login.jsp">return login</s:a>
  </body>
</html>


show.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags"  prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>show page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    <s:text name="showTip" />
    <p/>
    <s:a href="login.jsp">return login</s:a>
  </body>
</html>

query,jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags"  prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>query page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    <s:text name="queryTip" />
    <p/>
    <s:a href="login.jsp">return login</s:a>
  </body>
</html>


Struts2使用Interceptor实现权限控制的应用实例详解的更多相关文章

  1. 转:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法、shiro认证与shiro授权

    原文地址:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法.shiro认证与shiro授权 以下是部分内容,具体见原文. shiro介绍 什么是shiro shiro是Apache ...

  2. mysql用户授权、数据库权限管理、sql语法详解

    mysql用户授权.数据库权限管理.sql语法详解 —— NiceCui 某个数据库所有的权限 ALL 后面+ PRIVILEGES SQL 某个数据库 特定的权限SQL mysql 授权语法 SQL ...

  3. C#依赖注入控制反转IOC实现详解

    原文:C#依赖注入控制反转IOC实现详解 IOC的基本概念是:不创建对象,但是描述创建它们的方式.在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务.容器负责将这些联系在一起. ...

  4. 6、Struts2拦截器实现权限控制

    1.创建如下项目结果 2.在com.entity包下创建 package com.entity; public class User { private String name; private St ...

  5. struts2内置拦截器和自定义拦截器详解(附源码)

    一.Struts2内置拦截器 Struts2中内置类许多的拦截器,它们提供了许多Struts2的核心功能和可选的高级特 性.这些内置的拦截器在struts-default.xml中配置.只有配置了拦截 ...

  6. Struts2实例详解(转载)

    Struts2(上) 一.        经典的MVC模式 二.        Struts1.x对MVC的实现 三.        Struts1.x的主要组件和作用 组件 作用 ActionSer ...

  7. Smarty section、foreach控制循环次数的实现详解

    <!--{ section name='i' loop=$a }--><!--{ if $smarty.section.i.index < 3 }--><!--{  ...

  8. DOM对象控制HTML无素——详解3

    创建元素节点createElement createElement()方法可创建元素节点.此方法可返回一个 Element 对象. 语法: document.createElement(tagName ...

  9. DOM对象控制HTML无素——详解2

    节点属性 在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType ...

随机推荐

  1. c++之 数组

    数组的定义 数组用于表示一组数值,例如: char arr[5]; 其中,arr称为"数组变量",简称"数组".它表示5个char型数据,我们把每一个数据称为一 ...

  2. Android-JNI编程-图文解析

    要想阅读并调试下文源码,首先要确保你的NDK环境是ok的:        编译环境:win7+Eclipse+ADT+SDK+NDK:基本用最新的就ok.        说明下,下文代码就是一个简单的 ...

  3. IoC容器Autofac正篇之类型注册(四)

    Autofac类型注册 类型注册简单的从字面去理解就可以了,不必复杂化,只是注册的手段比较丰富. (一)类型/泛型注册 builder.RegisterType<Class1>(); 这种 ...

  4. javascript的本地存储 cookies、localStorage

    一.cookies 本地存储 首先是常用的cookies方法,网上有很多相关的代码以及w3cSchool cookies. // 存储cookies function setCookie(name,v ...

  5. overflow清楚浮动 + 去掉li标签的小圆点

    原文链接:http://blog.163.com/qqabc20082006@126/blog/static/22928525201031211212955/ 测试用例: <!DOCTYPE h ...

  6. Ext tpl 造成 store不能正确加载

    最近维护别人写的代码的时候,遇到了这么个情况 找原因找到了这行代码的身上 tpl: '<tpl for="."><div ext:qtip="{name ...

  7. hdu2037 经典贪心入门

    今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  8. mysql函数全解析

    本文摘自:http://www.cnblogs.com/cocos/archive/2011/05/06/2039469.html mysql函数大全 对于针对字符串位置的操作,第一个位置被标记为1. ...

  9. USE_DB_RECOVERY_FILE_DEST的使用详解(转载)

    实施时间2009-9-22晚10点 实施工作 1.  数据库归档模式修改: eashost:root:[/] smitty hacmp orahost:root:[/oracle/arch_log]# ...

  10. 视差滚动(Parallax Scrolling)效果的原理和实现

    视差滚动(Parallax Scrolling)是指让多层背景以不同的速度移动,形成立体的运动效果,带来非常出色的视觉体验.作为今年网页设计的热点趋势,越来越多的网站应用了这项技术. 一.什么是视差滚 ...