spring aop action中驗(yàn)證用戶登錄狀態(tài)的實(shí)例代碼
最近在學(xué)習(xí)ssh框架時(shí),照著網(wǎng)上做了一個(gè)商城系統(tǒng),之前在一些需要用戶存在的操作中,都是在每一個(gè)action中寫重復(fù)的代碼,這樣做現(xiàn)在想起來(lái)并不好,想起了spring的aop,于是想通過(guò)aop來(lái)給每個(gè)需要用戶操作的Action驗(yàn)證用戶登錄狀態(tài)。
想法是這樣的:
1. 用戶登錄時(shí)把userId放入session中
2. 通過(guò)spring 寫一個(gè)advice來(lái)獲取session中的userId,判斷用戶登錄狀態(tài),如果userId不符合,則拋出自定義異常
3. 通過(guò)struts中配置來(lái)捕獲異常,跳轉(zhuǎn)界面
以下是代碼:
advice代碼:
public class IsUserLoginAdvice{
public void isUserLogin() throws UserNotFoundException{
// TODO Auto-generated method stub
int id=0;
Map sessionMap=ActionContext.getContext().getSession();
System.out.println(sessionMap);
try {
//這里在一開(kāi)始時(shí)userId是不存在的可能會(huì)拋出NullPointException,catch起來(lái)
id=(int) sessionMap.get("userId");
//在用戶注銷時(shí)我把session中的userId設(shè)為0
if(id==0){
throw new UserNotFoundException();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new UserNotFoundException();
}
}
}
struts.xml:
這里通過(guò)全局異常映射來(lái)處理這個(gè)異常:
<package name="struts-global" namespace="/" extends="struts-default">
<global-results>
<result name="userNotFound">/web_resource/error_jsp/user_not_found.jsp
</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="userNotFound" exception="com.lsj.market.exception.UserNotFoundException"></exception-mapping>
</global-exception-mappings>
</package>
全局異常有個(gè)name屬性,給那些想要共享該異常捕獲的package繼承,這樣就可以共享該異常捕獲行為:
<package name="com.lsj.market.action.user" extends="struts-global">
applicationContext.xml:
<!-- aop設(shè)置 -->
<aop:config proxy-target-class="true">
<aop:aspect ref="isUserLoginAdvice">
<aop:pointcut id="isUserLoginPointcut"
expression="execution (* com.lsj.market.action..GetUser*.*(..))
or execution (* com.lsj.market.action..*Update*Action*.*(..))
or execution (* com.lsj.market.action..*Delete*Action*.*(..))
or execution (* com.lsj.market.action..GetMarketCar*.*(..))
or execution (* com.lsj.market.action..MarketCar*.*(..))
or execution (* com.lsj.market.action..ToFlower*.*(..))
or execution (* com.lsj.market.action..Flower*Add*.*(..))"/>
<aop:before method="isUserLogin" pointcut-ref="isUserLoginPointcut"/>
</aop:aspect>
</aop:config>
<!-- 聲明advice Bean -->
<bean id="isUserLoginAdvice" class="com.lsj.market.aop.IsUserLoginAdvice"></bean>
其中pointcut可以通過(guò)or 來(lái)連接多個(gè)切入點(diǎn),這里有這么多切入點(diǎn)是因?yàn)榈谝淮巫?,沒(méi)想到用aop,各個(gè)Action的命名沒(méi)有考慮太多,導(dǎo)致現(xiàn)在必須配置這么多個(gè)切入點(diǎn)表達(dá)式- -!!!
還有一個(gè),如果struts交由spring管理時(shí),即struts.xml中配置了這一句:
<constant name="struts.objectFactory" value="spring" />
在生成代理類時(shí)會(huì)發(fā)生錯(cuò)誤,無(wú)法捕捉到拋出的異常,在網(wǎng)上查了后發(fā)現(xiàn)需要在struts.xml加入這一句,struts就可以捕捉到該異常了:
<!-- 總是確保使用spring的自動(dòng)裝備策略 --> <constant name="struts.objectFactory.spring.autoWire.alwaysRespect" value="true" />
剛剛還想刪除這一句配置后把異常發(fā)上來(lái),但是發(fā)現(xiàn)刪除后居然還可以運(yùn)行?!
算了還是寫上來(lái),以后遇到這個(gè)問(wèn)題,還可以看一下博客。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springsecurity實(shí)現(xiàn)用戶登錄認(rèn)證快速使用示例代碼(前后端分離項(xiàng)目)
- Springboot+Shiro記錄用戶登錄信息并獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼
- Spring mvc 實(shí)現(xiàn)用戶登錄的方法(攔截器)
- springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(下)
- springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(上)
- SpringMvc實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- Spring實(shí)現(xiàn)加法計(jì)算器和用戶登錄功能
相關(guān)文章
java實(shí)現(xiàn)超市庫(kù)存管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)超市庫(kù)存管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
SpringBoot使用自動(dòng)配置xxxAutoConfiguration
這篇文章介紹了SpringBoot自動(dòng)配置xxxAutoConfiguration的使用方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
Java并發(fā)編程之CountDownLatch的使用
CountDownLatch是一個(gè)倒數(shù)的同步器,常用來(lái)讓一個(gè)線程等待其他N個(gè)線程執(zhí)行完成再繼續(xù)向下執(zhí)行,本文主要介紹了CountDownLatch的具體使用方法,感興趣的可以了解一下2023-05-05
淺談java中Math.random()與java.util.random()的區(qū)別
下面小編就為大家?guī)?lái)一篇淺談java中Math.random()與java.util.random()的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09
Spring?MVC中的Controller進(jìn)行單元測(cè)試的實(shí)現(xiàn)
本文主要介紹了如何對(duì)Spring?MVC中的Controller進(jìn)行單元測(cè)試的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

