SSH进阶(2)——用Struts拦截器实现登陆限制
攔截器從字面意思來看就是限制,限制用戶訪問某些網(wǎng)頁。在Action提出請(qǐng)求之前用攔截器來做權(quán)限設(shè)置,讓符合的用戶跳入相應(yīng)的界面中。最近做的一個(gè)商城項(xiàng)目中就用到了自定義的攔截器,實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的session判斷,成功就登陸,不成功就跳轉(zhuǎn)到門戶。
【攔截器工作原理】
? ? ? ?Struts2攔截器是在訪問某個(gè)Action或Action的某個(gè)方法,字段之前或之后實(shí)施攔截,并且Struts2攔截器是可插拔的,攔截器是AOP的一種實(shí)現(xiàn)。AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預(yù)編譯方式和運(yùn)行期動(dòng)態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。利用AOP可以對(duì)業(yè)務(wù)邏輯的各個(gè)部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了開發(fā)的效率。
? ?我的理解是每一個(gè)action請(qǐng)求都會(huì)在攔截器的內(nèi)部,當(dāng)發(fā)生請(qǐng)求的時(shí)候,Struts會(huì)找對(duì)應(yīng)的配置文件,實(shí)例化相應(yīng)的攔截器對(duì)象,如設(shè)置攔截器屬性excludeMethods。很類似于過濾器的,但是過濾器的范圍要大于攔截器。
【創(chuàng)建攔截器】
第一種方法就是直接實(shí)現(xiàn)Interceptor接口,這樣的話,有個(gè)弊端,這里面的init() ?destroy() ?intercept() ?方法都需要實(shí)現(xiàn)了。
第二種方法是繼承自AbstractInterceptor抽象類,實(shí)現(xiàn)了Interceptor接口,并且對(duì)里面的init()和destroy()方法進(jìn)行空實(shí)現(xiàn),而把intercept()方法設(shè)置為抽象方法,讓繼承它的子類去實(shí)現(xiàn),這樣的話,子類只要實(shí)現(xiàn)這個(gè)intercept()方法就可以了,比上面的簡(jiǎn)單。
第三種方法是繼承自MethodFilterInterceptor(我下面就是用這個(gè)方法實(shí)現(xiàn)的),這個(gè)類叫做方法過濾攔截器,MethodFilterInterceptor繼承自AbstractInterceptor,并且提供了一種機(jī)制,即可以指定對(duì)Action中某些方法進(jìn)行攔截或者是不攔截,所謂攔截不攔截,指的就是攔截器中的intercept()方法是否被執(zhí)行了,若沒有執(zhí)行,就是沒有攔截,若執(zhí)行了,就是攔截了。
總的來說,相比之前的接口和抽象類,最后一個(gè)只需要指定對(duì)Action中某些方法進(jìn)行攔截是最簡(jiǎn)單的。是通過includeMethods和excludeMethods這兩個(gè)參數(shù)來設(shè)置那個(gè)方法需要攔截的,在Struts.xml中進(jìn)行配置。
【攔截器實(shí)現(xiàn)登陸】
1>首先我們要自定義一個(gè)攔截器類:
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>然后在配置文件中去注冊(cè)這個(gè)攔截器,并在后臺(tái)登陸的action中利用excludeMethods這個(gè)成員變量來綁定login這個(gè)方法被攔截。
3>然后是在login 這個(gè)方法被調(diào)用的時(shí)候,就去驗(yàn)證session是不是空的,來判斷是否有這個(gè)用戶名和密碼的匹配,然后放入了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";}}
這樣一來就輕松實(shí)現(xiàn)了用戶登陸時(shí)候,session是否為空的判斷了。
【總結(jié)】
1.攔截器定義了一種機(jī)制,可以讓開發(fā)定義一些代碼在action執(zhí)行之前&后執(zhí)行。
2.提高了代碼的復(fù)用,想想我們開發(fā)asp.net的時(shí)候總是寫上if(session["username"]==null)之類的話在if(!Page。ispostback)里面。每個(gè)頁面都有去判斷session。在我們有了這個(gè)攔截器之后呢,我們只需要在每個(gè)action配置里面寫上:
<interceptor-ref name="攔截器名稱"/><interceptor-ref name="defaultStack"/>
這樣就解決了所有頁面的權(quán)限驗(yàn)證問題了。
3.其中includeMethods和excludeMethods是來決定這個(gè)action里面的方法是否需要被攔截。
總結(jié)
以上是生活随笔為你收集整理的SSH进阶(2)——用Struts拦截器实现登陆限制的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 第十三课 Shared
- 下一篇: Git(2):安装和使用