Struts2实现访问控制

今天在项目中实现了访问控制,较之其他功能简单很多,总结一下:
在SSH框架之下,最基本的是要实现访问控制。
主要思想就是使用拦截器控制action的访问,使用jsp代码控制jsp页面的访问。
对Action的访问控制:
拦截器需要制定拦截除了LoginAction和LogoutAction之外的所有Action
一般是声明一些拦截器拦截器,然后将某些拦截器组合成拦截器栈:


   
   
          
   
   
       
       
   


然后在Action配置中制定需要使用的拦截器:

    yndw_1.jsp
    error.jsp
   

但是不可能在所有的Action配置中都加上

 
可以这样做:
新建1+1+n个包
第一个包声明登入登出等不需要拦截的Action:
    

   
       
           yndwAction
           /
       

        返回input结果,若找不到input类型,就会尝试用action的class去创建一个
action,导致action未知类型的错误 -->
       /login.jsp
   
   
   
       login.jsp
   


第二个包声明拦截器,然后定义为默认拦截器:

   
   
       
        class="com.jsjl.interceptor.LoginAuthenInterceptor" />
       
       
           
           
       

   
    
   
        
   
   
       /login.jsp
   


然后其他N和包,都继承自第二个包,这样就默认继承了第二个包的默认的拦截器:

就这样,访问除了第一个包中的所有Action都会通过拦截器判断用户是否登入,否则转到登陆页面。
然后对于jsp页面的访问控制,只需要在页面开始加上以下几句即可:
<%
    Object isLogin = session.getAttribute("isLogin");
    if(isLogin == null){    
       response.sendRedirect("login.jsp");
    }
%>
拦截器是这样的:
public class LoginAuthenInterceptor extends AbstractInterceptor {
    public String intercept(ActionInvocation invocation) throws Exception {
       ActionContext context = ActionContext.getContext();
       Map session = context.getSession();
       Object user = session.get("isLogin");
       if(user == null){
           return "login";
       }
       return invocation.invoke();
    }
}
 
 
对JSP页面的访问控制:
在页面开头加入如下jsp代码即可:
<%!
   String greeting = "";
%>
<%
   UserBean user = (UserBean)session.getAttribute("user");
   if(user == null){
       response.sendRedirect("login.jsp");
   }
   else greeting = user.getNiname();
%>

---------------------->>转载于:http://blog.sina.com.cn/s/blog_6a1928130100ughg.html



配置文件部分 
(struts.xml)
 
                "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"     
                "http://struts.apache.org/dtds/struts-2.0.dtd">         
         
        "struts-default.xml"
/>        
                
        <package name="non-authority" extends="struts-default" >                    
                "login"
 class="com.aumy.struts.example.LoginAction">        
                        "input">/login.jsp        
                        "error">/error.jsp        
                        "success">/welcome.jsp        
                                 
                "qurey" class="com.aumy.struts.example.LoginAction" method="qurey">        
                        "success">/qurey.jsp        
                
        
     
        package>        
                
                
        <package name="authority" extends="struts-default">        
                        
                                
                                                         class="com.aumy.struts.example.intercepter.AuthorityInterceptor"     
                                name="authority"/>        
                                
                        "mydefault">        
                                        
                                "defaultStack"/>        
                                        
                                "authority"/>        
                        
        
                
        
                        
                <default-interceptor-ref name="mydefault" />        
                        
                        
                        "login">/login.jsp        
                
        
                        
                "show" class="com.aumy.struts.example.LoginAction"     
                        method="show">        
                        "success">/show.jsp        
                
        
                        
                "add" class="com.aumy.struts.example.LoginAction"     
                        method="add">        
                        "success">/add.jsp        
                
        
                        
        package>        
     
 
(struts.properties)
struts.custom.i18n.resources=message.messageResouce    
 
(web.xml)
"1.0" encoding="UTF-8"?>        
"2.4"         
        xmlns="http://java.sun.com/xml/ns/j2ee"         
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee         
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">        
        Struts test        
     
                
                struts2        
                class
>org.apache.struts2.dispatcher.FilterDispatcherclass
>        
                
     
                
                struts2        
                /*        
        
        
     
     
                
                login.jsp        
        
        
     
 

请使用浏览器的分享功能分享到微信等