Struts2使用拦截器完成权限控制示例

http://aumy2008.iteye.com/blog/146952

Struts2使用拦截器完成权限控制示例

示例需求: 
   要求用户登录,且必须为指定用户名才可以查看系统中某个视图资源;否则,系统直接转入登陆页面。 

一、页面部分 
1、登陆页面代码(login.jsp) 

Java代码  收藏代码
  1. <%@ page language="java" contentType="text/html; charset=GBK"%>  
  2. <%@taglib prefix="s" uri="/struts-tags"%>  
  3. <html>  
  4.     <head>  
  5.         <title><s:text name="loginPage" /></title>  
  6.     </head>  
  7.     <body>  
  8.         <!-- 使用form标签生成表单元素 -->  
  9.         <s:form action="login">  
  10.             <s:textfield name="username" label="%{getText('user')}" />  
  11.             <s:textfield name="password" label="%{getText('pass')}" />  
  12.             <s:submit value="%{getText('login')}" />  
  13.         </s:form>  
  14.     </body>  
  15. </html>  



2、登陆成功页面(welcome.jsp) 

Java代码  收藏代码
  1. <%@ page language="java" contentType="text/html; charset=GBK"%>  
  2. <%@taglib prefix="s" uri="/struts-tags"%>  
  3. <html>  
  4.     <head>  
  5.         <title><s:text name="succPage" /></title>  
  6.         <s:head />  
  7.     </head>  
  8.     <body>  
  9.         <s:text name="succTip" />  
  10.         <br />  
  11.         <!-- 欢迎,${sessionScope.user},您已经登录!  
  12.         ${sessionScope.pass}-->  
  13.         <p />  
  14.         <s:a href="show.action">show</s:a>  
  15.         <p />  
  16.         <s:a href="add.action">add</s:a>  
  17.         <p />  
  18.         <s:a href="qurey.action">qurey</s:a>  
  19.     </body>  
  20. </html>  



3、登陆失败页面(error.jsp) 

Java代码  收藏代码
  1. <%@ page language="java" contentType="text/html; charset=GBK"%>  
  2. <%@taglib prefix="s" uri="/struts-tags"%>  
  3. <html>  
  4.     <head>  
  5.         <title><s:text name="errorPage" /></title>  
  6.     </head>  
  7.     <body>  
  8.         <s:text name="failTip" />  
  9.         <p />  
  10.         <s:a href="login.jsp">return</s:a>  
  11.     </body>  
  12. </html>  



4、和权限有关的几个显示页面 
(add.jsp) 

Java代码  收藏代码
  1. <%@ page language="java" contentType="text/html; charset=GBK"%>  
  2. <%@taglib prefix="s" uri="/struts-tags"%>  
  3. <html>  
  4.     <head>  
  5.         <title><s:text name="addPage"/></title>  
  6.     </head>  
  7.     <body>  
  8.         <s:text name="addTip"/>  
  9.         <p />  
  10.         <s:a href="login.jsp">return login</s:a>  
  11.     </body>  
  12. </html>  



(show.jsp) 

Java代码  收藏代码
  1. <%@ page language="java" contentType="text/html; charset=GBK"%>  
  2. <%@taglib prefix="s" uri="/struts-tags"%>  
  3. <html>  
  4.     <head>  
  5.         <title><s:text name="showPage"/></title>  
  6.     </head>  
  7.     <body>  
  8.         <s:text name="showTip"/>  
  9.         <p />  
  10.         <s:a href="login.jsp">return login</s:a>  
  11.     </body>  
  12. </html>  



(qurey.jsp) 

Java代码  收藏代码
  1. <%@ page language="java" contentType="text/html; charset=GBK"%>  
  2. <%@taglib prefix="s" uri="/struts-tags"%>  
  3. <html>  
  4.     <head>  
  5.         <title><s:text name="qureyPage"/></title>  
  6.     </head>  
  7.     <body>  
  8.         <s:text name="qureyTip"/>  
  9.         <p />  
  10.         <s:a href="login.jsp">return login</s:a>  
  11.     </body>  
  12. </html>  



二、Action部分(LoginAction.java) 

Java代码  收藏代码
  1. public class LoginAction extends ActionSupport {  
  2.     private static final long serialVersionUID = 1030294046920869257L;  
  3.     private String username;  
  4.     private String password;  
  5.   
  6.     // 处理用户请求的execute方法  
  7.     public String execute() throws Exception {  
  8.         if (isInvalid(getUsername()))  
  9.             return INPUT;  
  10.   
  11.         if (isInvalid(getPassword()))  
  12.             return INPUT;  
  13.   
  14.         if ((getUsername().equals("mm") || getUsername().equals("aumy"))  
  15.                 && getPassword().equals("111")) {  
  16.             // 通过ActionContext对象访问Web应用的Session  
  17.             ActionContext.getContext().getSession().put("user", getUsername());  
  18.             ActionContext.getContext().getSession().put("pass", getPassword());  
  19.             System.out.println(getUsername() + "----" + getPassword());  
  20.             return SUCCESS;  
  21.         } else {  
  22.             System.out.println(getUsername() + "----" + getPassword());  
  23.             return ERROR;  
  24.         }  
  25.     }  
  26.   
  27.     private boolean isInvalid(String value) {  
  28.         return (value == null || value.length() == 0);  
  29.     }  
  30.   
  31.     public String add() {  
  32.         return SUCCESS;  
  33.     }  
  34.   
  35.     public String show() {  
  36.         return SUCCESS;  
  37.     }  
  38.   
  39.     public String qurey() {  
  40.         return SUCCESS;  
  41.     }  
  42.   
  43.     public String getUsername() {  
  44.         return username;  
  45.     }  
  46.   
  47.     public void setUsername(String username) {  
  48.         this.username = username;  
  49.     }  
  50.   
  51.     public String getPassword() {  
  52.         return password;  
  53.     }  
  54.   
  55.     public void setPassword(String password) {  
  56.         this.password = password;  
  57.     }  
  58. }  



三、拦截器部分(AuthorityInterceptor.java) 

Java代码  收藏代码
  1. public class AuthorityInterceptor extends AbstractInterceptor {  
  2.     private static final long serialVersionUID = 1358600090729208361L;  
  3.   
  4.     //拦截Action处理的拦截方法  
  5.     public String intercept(ActionInvocation invocation) throws Exception {  
  6.         // 取得请求相关的ActionContext实例  
  7.         ActionContext ctx=invocation.getInvocationContext();  
  8.         Map session=ctx.getSession();  
  9.         //取出名为user的session属性  
  10.         String user=(String)session.get("user");  
  11.         //如果没有登陆,或者登陆所有的用户名不是aumy,都返回重新登陆  
  12.         if(user!=null && user.equals("aumy")){  
  13.             return invocation.invoke();  
  14.         }  
  15.         //没有登陆,将服务器提示设置成一个HttpServletRequest属性  
  16.         ctx.put("tip","您还没有登录,请登陆系统");  
  17.         return Action.LOGIN;          
  18.     }  
  19. }  



四、配置文件部分 
(struts.xml) 

Java代码  收藏代码
  1. <!DOCTYPE struts PUBLIC  
  2.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.         "http://struts.apache.org/dtds/struts-2.0.dtd">   
  4. <struts>   
  5.     <include file="struts-default.xml"/>  
  6.     <!--不受权限控制的Action请求配置-->  
  7.     <package name="non-authority" extends="struts-default" >        
  8.         <action name="login" class="com.aumy.struts.example.LoginAction">  
  9.             <result name="input">/login.jsp</result>  
  10.             <result name="error">/error.jsp</result>  
  11.             <result name="success">/welcome.jsp</result>  
  12.         </action>       
  13.         <action name="qurey" class="com.aumy.struts.example.LoginAction" method="qurey">  
  14.             <result name="success">/qurey.jsp</result>  
  15.         </action>  
  16.   
  17.     </package>  
  18.       
  19.     <!--受权限控制的Action请求配置-->  
  20.     <package name="authority" extends="struts-default">  
  21.         <interceptors>  
  22.             <!--定义一个名为authority的拦截器-->  
  23.             <interceptor  
  24.                 class="com.aumy.struts.example.intercepter.AuthorityInterceptor"  
  25.                 name="authority"/>  
  26.             <!--定义一个包含权限检查的拦截器栈-->  
  27.             <interceptor-stack name="mydefault">  
  28.                 <!--配置内建默认拦截器-->  
  29.                 <interceptor-ref name="defaultStack"/>  
  30.                 <!--配置自定义的拦截器-->  
  31.                 <interceptor-ref name="authority"/>  
  32.             </interceptor-stack>  
  33.         </interceptors>  
  34.           
  35.         <default-interceptor-ref name="mydefault" />  
  36.         <!--定义全局Result-->  
  37.         <global-results>  
  38.             <result name="login">/login.jsp</result>  
  39.         </global-results>  
  40.           
  41.         <action name="show" class="com.aumy.struts.example.LoginAction"  
  42.             method="show">  
  43.             <result name="success">/show.jsp</result>  
  44.         </action>  
  45.           
  46.         <action name="add" class="com.aumy.struts.example.LoginAction"  
  47.             method="add">  
  48.             <result name="success">/add.jsp</result>  
  49.         </action>  
  50.           
  51.     </package>  
  52. </struts>  



(struts.properties) 

Java代码  收藏代码
  1. struts.custom.i18n.resources=message.messageResouce  



(web.xml) 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.     <display-name>Struts test</display-name>  
  8.   
  9.     <filter>  
  10.         <filter-name>struts2</filter-name>  
  11.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  12.     </filter>  
  13.   
  14.     <filter-mapping>  
  15.         <filter-name>struts2</filter-name>  
  16.         <url-pattern>/*</url-pattern>  
  17.     </filter-mapping>  
  18.   
  19.   
  20.     <welcome-file-list>  
  21.         <welcome-file>login.jsp</welcome-file>  
  22.     </welcome-file-list>  
  23. </web-app>  


五、国际化资源文件(messageResouce.properties) 

Java代码  收藏代码
    1. loginPage=Login Page  
    2. errorPage=Error Page  
    3. succPage=Welcome Page  
    4. failTip=Sorry,You can't log in!  
    5. succTip=welcome,you has logged in!   
    6. user=User Name  
    7. pass=User Pass  
    8. login=Login  
    9. showPage=Show Page  
    10. showTip=show a example!  
    11. addPage=Add Page  
    12. addTip=add a example!  
    13. qureyPage=Qurey Page  
    14. qureyTip=qurey a example!  
原文地址:https://www.cnblogs.com/xing----hao/p/3581181.html