struts2 拦截器权限控制
工程布局:

直接上代码:
LoginAction.java
package com.aumy.struts.example;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1030294046920869257L;
private String username;
private String password;
// 处理用户请求的execute方法
public String execute() throws Exception { //执行方法
System.out.println(username) ;
System.out.println(password) ;
if (isInvalid(getUsername()))
return INPUT;
if (isInvalid(getPassword()))
return INPUT;
if ((getUsername().equals("mm") || getUsername().equals("aumy"))
&& getPassword().equals("111")) {
// 通过ActionContext对象访问Web应用的Session
ActionContext.getContext().getSession().put("user", getUsername());
ActionContext.getContext().getSession().put("pass", getPassword());
System.out.println(getUsername() + "----" + getPassword());
return SUCCESS;
} else {
System.out.println(getUsername() + "----" + getPassword());
return ERROR;
}
}
private boolean isInvalid(String value) {
return (value == null || value.length() == 0);
}
public String add() { //执行方法
System.out.println("我是add方法");
return SUCCESS;
}
public String show() { //执行方法
System.out.println("我是show方法");
return SUCCESS;
}
public String qurey() { //执行方法
System.out.println("我是query方法");
return SUCCESS;
}
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;
}
}
拦截器
AuthorityInterceptor.java
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;
public class AuthorityInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1358600090729208361L;
// 拦截Action处理的拦截方法
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("我是拦截器");
// 取得请求相关的ActionContext实例
ActionContext ctx = invocation.getInvocationContext();
Map session = ctx.getSession();
// 取出名为user的session属性
String user = (String) session.get("user");
// 如果没有登陆,或者登陆所有的用户名不是aumy,都返回重新登陆
if (user != null || !user.equals("aumy")) {
return invocation.invoke(); //拦截器通过,去action中去
}
// 没有登陆,将服务器提示设置成一个HttpServletRequest属性
ctx.put("tip", "您还没有登录,请登陆系统");
return Action.LOGIN; //拦截器没有通过,去查看struts.xml的配置文件去
}
}
struts2.xml
<?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>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<!--
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings>
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
-->
<package name="default" namespace="/" extends="struts-default">
<include file="struts-default.xml" />
<!--不受权限控制的Action请求配置-->
<package name="non-authority" extends="struts-default">
<action name="login"
class="com.aumy.struts.example.LoginAction">
<result name="input">/login.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
<action name="qurey" class="com.aumy.struts.example.LoginAction"
method="qurey">
<result name="success">/qurey.jsp</result>
</action>
</package>
<!--受权限控制的Action请求配置-->
<package name="authority" extends="struts-default">
<interceptors>
<!--定义一个名为authority的拦截器-->
<interceptor
class="com.aumy.struts.example.intercepter.AuthorityInterceptor"
name="authority" />
<!--定义一个包含权限检查的拦截器栈-->
<interceptor-stack name="mydefault">
<!--配置内建默认拦截器-->
<interceptor-ref name="defaultStack" />
<!--配置自定义的拦截器-->
<interceptor-ref name="authority" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="mydefault" />
<!--定义全局Result-->
<global-results>
<result name="login">/login.jsp</result>
</global-results>
<action name="show" class="com.aumy.struts.example.LoginAction"
method="show">
<result name="success">/show.jsp</result>
</action>
<action name="add" class="com.aumy.struts.example.LoginAction"
method="add">
<result name="success">/add.jsp</result>
</action>
</package>
<!-- Add packages here
<include file="example.xml"/>
-->
</struts>
jsp页面了
add.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title><s:text name="addPage"/></title>
</head>
<body>
<s:text name="addTip"/>
<p />
<s:a href="login.jsp">return login</s:a>
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title><s:text name="errorPage" /></title>
</head>
<body>
<s:text name="failTip" />
<p />
<s:a href="login.jsp">return</s:a>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title><s:text name="loginPage" />
</title>
</head>
<body>
<form action="login.action" method="post">
用户名:<input type="text" name="username"/> <br/>
密码: <input type="text" name="password"/> <br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
query.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title><s:text name="qureyPage"/></title>
</head>
<body>
<s:text name="qureyTip"/>
<p />
<s:a href="login.jsp">return login</s:a>
</body>
</html>
show.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title><s:text name="showPage"/></title>
</head>
<body>
<s:text name="showTip"/>
<p />
<s:a href="login.jsp">return login</s:a>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>My JSP 'success.jsp' starting 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>
This is my JSP page. <br>
</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title><s:text name="succPage" /></title>
<s:head />
</head>
<body>
<s:text name="succTip" />
<br />
<!-- 欢迎,${sessionScope.user},您已经登录!
${sessionScope.pass}-->
<p />
<s:a href="show.action">show</s:a>
<p />
<s:a href="add.action">add</s:a>
<p />
<s:a href="qurey.action">qurey</s:a>
</body>
</html>
login.jsp开始操作-----输入的时候密码是111,用户名是mm,或者aumy,自己看。
---------------------------------------------------------------------------------------------------------------------------------一点想法:
1.拦截器自定义的时候,要用到默认的主拦截器,别忘记了。
2.拦截器一般在进入action之前进行拦截的(执行完action然后再执行拦截器应该也有的吧???)
3.拦截器有全局的拦截器,或者一个action的配置拦截器,应该是先去找自己的cation的拦截器,然后
如果找不到,再去找全局的拦截器。我们的举例是用到全局的拦截器。