软件编程
位置:首页>> 软件编程>> java编程>> spring aop action中验证用户登录状态的实例代码

spring aop action中验证用户登录状态的实例代码

作者:后台菜狗子  发布时间:2021-12-04 19:47:01 

标签:spring,aop,登录验证

最近在学习ssh框架时,照着网上做了一个商城系统,之前在一些需要用户存在的操作中,都是在每一个action中写重复的代码,这样做现在想起来并不好,想起了spring的aop,于是想通过aop来给每个需要用户操作的Action验证用户登录状态。

想法是这样的:

1. 用户登录时把userId放入session中

2. 通过spring 写一个advice来获取session中的userId,判断用户登录状态,如果userId不符合,则抛出自定义异常

3. 通过struts中配置来捕获异常,跳转界面

以下是代码: 

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 {
     //这里在一开始时userId是不存在的可能会抛出NullPointException,catch起来
     id=(int) sessionMap.get("userId");
     //在用户注销时我把session中的userId设为0
     if(id==0){
       throw new UserNotFoundException();
     }
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     throw new UserNotFoundException();
   }
 }
}

struts.xml:

这里通过全局异常映射来处理这个异常:


 <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>

全局异常有个name属性,给那些想要共享该异常捕获的package继承,这样就可以共享该异常捕获行为:


<package name="com.lsj.market.action.user" extends="struts-global">

applicationContext.xml:


 <!-- aop设置 -->
 <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可以通过or 来连接多个切入点,这里有这么多切入点是因为第一次做,没想到用aop,各个Action的命名没有考虑太多,导致现在必须配置这么多个切入点表达式- -!!!

还有一个,如果struts交由spring管理时,即struts.xml中配置了这一句:


<constant name="struts.objectFactory" value="spring" />

在生成代理类时会发生错误,无法捕捉到抛出的异常,在网上查了后发现需要在struts.xml加入这一句,struts就可以捕捉到该异常了:


 <!-- 总是确保使用spring的自动装备策略 -->
 <constant name="struts.objectFactory.spring.autoWire.alwaysRespect" value="true" />

刚刚还想删除这一句配置后把异常发上来,但是发现删除后居然还可以运行?!

算了还是写上来,以后遇到这个问题,还可以看一下博客。

来源:http://blog.csdn.net/caser_hdmi/article/details/75205783

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com