Struts2使用Interceptor实现权限控制的应用实例详解
Struts2使用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;
}
}
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;
}
}
<!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>
<%@ 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>
<%@ 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>
<%@ 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>
<%@ 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>
<%@ 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>
<%@ 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实现权限控制的应用实例详解的更多相关文章
- 转:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法、shiro认证与shiro授权
原文地址:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法.shiro认证与shiro授权 以下是部分内容,具体见原文. shiro介绍 什么是shiro shiro是Apache ...
- mysql用户授权、数据库权限管理、sql语法详解
mysql用户授权.数据库权限管理.sql语法详解 —— NiceCui 某个数据库所有的权限 ALL 后面+ PRIVILEGES SQL 某个数据库 特定的权限SQL mysql 授权语法 SQL ...
- C#依赖注入控制反转IOC实现详解
原文:C#依赖注入控制反转IOC实现详解 IOC的基本概念是:不创建对象,但是描述创建它们的方式.在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务.容器负责将这些联系在一起. ...
- 6、Struts2拦截器实现权限控制
1.创建如下项目结果 2.在com.entity包下创建 package com.entity; public class User { private String name; private St ...
- struts2内置拦截器和自定义拦截器详解(附源码)
一.Struts2内置拦截器 Struts2中内置类许多的拦截器,它们提供了许多Struts2的核心功能和可选的高级特 性.这些内置的拦截器在struts-default.xml中配置.只有配置了拦截 ...
- Struts2实例详解(转载)
Struts2(上) 一. 经典的MVC模式 二. Struts1.x对MVC的实现 三. Struts1.x的主要组件和作用 组件 作用 ActionSer ...
- Smarty section、foreach控制循环次数的实现详解
<!--{ section name='i' loop=$a }--><!--{ if $smarty.section.i.index < 3 }--><!--{ ...
- DOM对象控制HTML无素——详解3
创建元素节点createElement createElement()方法可创建元素节点.此方法可返回一个 Element 对象. 语法: document.createElement(tagName ...
- DOM对象控制HTML无素——详解2
节点属性 在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType ...
随机推荐
- 如何实现 iOS 自定义状态栏
给大家介绍如何实现 iOS 自定义状态栏 Sample Code: 01 UIWindow * statusWindow = [[UIWindow alloc] initWithFrame:[UIAp ...
- hdu1016Prime Ring Problem
就是说,给你一个数n, 要你把1到n都连在一起成环. 每一个数不可反复, 且相连的两个数的和要是素数. 把全部情况输出来. 我是用dfs暴力出来的. 首先把素数打表, 然后每次顺时针预測下一个数 ...
- 机器学习实战笔记5(logistic回归)
1:简单概念描写叙述 如果如今有一些数据点,我们用一条直线对这些点进行拟合(改线称为最佳拟合直线),这个拟合过程就称为回归.训练分类器就是为了寻找最佳拟合參数,使用的是最优化算法. 基于sigmoid ...
- Rss 的作用 及使用方法
也可以参考http://jingyan.baidu.com/article/e73e26c0c73e1f24adb6a70f.html 什么是RSS RSS是站点用来和其他站点之间共享内容的一种简易方 ...
- Linux UGO和ACL权限管理
自主访问控制(Discretionary Access Control, DAC)是指对象(比如程序.文件.进程)的拥有者可以任意修改或者授予此对象相应的权限.Linux的UGO(User, Grou ...
- css基本选择器
CSS:层叠样式表 (Cascading Style Sheets) 结构层:HTML表现层: CSS行为层: DOM,JavaScript CSS语法结构:div{background:#f00;} ...
- SQL Server事务的存储过程
在酒店管理系统开发中,我们会创建房间表和房间类型表(房型表)这两个表,如下图所示: 房型表:RoomType 房间表:Room 首先这两个表的关系:Room是从表,RoomType是主表,两表有主外键 ...
- Ubuntu安装samba服务器
一.安装软件 sudo apt-get install samba 二.配置samba服务器/etc/samba/smb.conf 把默认的配置文件备份,按如下修改配置文件 [global] work ...
- shell中的数学运算
shell中要进行数学运算通常有3中方法: expr命令 比如 expr 1 + 6就会返回7,使用expr的缺点就是碰到乘法运算,或者加括号(因为它们在shell中有其他意义),需要使用转义,比如: ...
- mysql存储过程实践总结
一:参数类型 1.IN 只能读取参数并在函数内部更改有效,不能持久化到外部变量 2.OUT 不能读取参数,可以在函数内部修改并保存到外部变量 3.INOUT 既能读取又能持久化 二:基本格式 mys ...