SSH进阶(2)——用Struts拦截器实现登陆限制
攔截器從字面意思來看就是限制,限制用戶訪問某些網頁。在Action提出請求之前用攔截器來做權限設置,讓符合的用戶跳入相應的界面中。最近做的一個商城項目中就用到了自定義的攔截器,實現了一個簡單的session判斷,成功就登陸,不成功就跳轉到門戶。
【攔截器工作原理】
? ? ? ?Struts2攔截器是在訪問某個Action或Action的某個方法,字段之前或之后實施攔截,并且Struts2攔截器是可插拔的,攔截器是AOP的一種實現。AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預編譯方式和運行期動態代理實現程序功能的統一維護的一種技術。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發的效率。
? ?我的理解是每一個action請求都會在攔截器的內部,當發生請求的時候,Struts會找對應的配置文件,實例化相應的攔截器對象,如設置攔截器屬性excludeMethods。很類似于過濾器的,但是過濾器的范圍要大于攔截器。
【創建攔截器】
第一種方法就是直接實現Interceptor接口,這樣的話,有個弊端,這里面的init() ?destroy() ?intercept() ?方法都需要實現了。
第二種方法是繼承自AbstractInterceptor抽象類,實現了Interceptor接口,并且對里面的init()和destroy()方法進行空實現,而把intercept()方法設置為抽象方法,讓繼承它的子類去實現,這樣的話,子類只要實現這個intercept()方法就可以了,比上面的簡單。
第三種方法是繼承自MethodFilterInterceptor(我下面就是用這個方法實現的),這個類叫做方法過濾攔截器,MethodFilterInterceptor繼承自AbstractInterceptor,并且提供了一種機制,即可以指定對Action中某些方法進行攔截或者是不攔截,所謂攔截不攔截,指的就是攔截器中的intercept()方法是否被執行了,若沒有執行,就是沒有攔截,若執行了,就是攔截了。
總的來說,相比之前的接口和抽象類,最后一個只需要指定對Action中某些方法進行攔截是最簡單的。是通過includeMethods和excludeMethods這兩個參數來設置那個方法需要攔截的,在Struts.xml中進行配置。
【攔截器實現登陸】
1>首先我們要自定義一個攔截器類:
package cn.itcast.shop.interceptor; import org.apache.struts2.ServletActionContext; import cn.itcast.shop.adminuser.vo.AdminUser; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; /*** interceptor* @author Zhouzhou* @date 2016-1-25* @time 下午09:48:21*/ /*** that class want to judge the session is null,it extends the class named : MethodFilterInterceptor,*/ public class PrivilegeInterceptor extends MethodFilterInterceptor{/*** the method doIntercept is the main method of the MethodFilterInterceptor*/@Overrideprotected String doIntercept(ActionInvocation actionInvocation) throws Exception {//judge login//AdminUser adminUser = (AdminUser) ServletActionContext.getRequest().getSession().getAttribute("existAdminUser");if(adminUser != null){// is logined ,have the userinfo in the sessionreturn actionInvocation.invoke();}else{// is not loginActionSupport support = (ActionSupport) actionInvocation.getAction();support.addActionError("You are not logged in!No permission to access!");return ActionSupport.LOGIN;}} }
2>然后在配置文件中去注冊這個攔截器,并在后臺登陸的action中利用excludeMethods這個成員變量來綁定login這個方法被攔截。
3>然后是在login 這個方法被調用的時候,就去驗證session是不是空的,來判斷是否有這個用戶名和密碼的匹配,然后放入了session中。
/*** login method--azz*/public String login() {User existUser = userService.login(user);// use the username and pwd check the user is existif (existUser == null) {//login failthis.addActionError("error,your user or pwd is error, please login again...");return LOGIN;} else {// login success// set the user information into the sessionServletActionContext.getRequest().getSession().setAttribute("existUser", existUser);// goto the struts.xml ↓//<result name="loginSuccess" type="redirect">/admin/home.jsp</result>return "loginSuccess";}}
這樣一來就輕松實現了用戶登陸時候,session是否為空的判斷了。
【總結】
1.攔截器定義了一種機制,可以讓開發定義一些代碼在action執行之前&后執行。
2.提高了代碼的復用,想想我們開發asp.net的時候總是寫上if(session["username"]==null)之類的話在if(!Page。ispostback)里面。每個頁面都有去判斷session。在我們有了這個攔截器之后呢,我們只需要在每個action配置里面寫上:
<interceptor-ref name="攔截器名稱"/><interceptor-ref name="defaultStack"/>
這樣就解決了所有頁面的權限驗證問題了。
3.其中includeMethods和excludeMethods是來決定這個action里面的方法是否需要被攔截。
總結
以上是生活随笔為你收集整理的SSH进阶(2)——用Struts拦截器实现登陆限制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 第十三课 Shared
- 下一篇: Git(2):安装和使用