服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - spring aop action中验证用户登录状态的实例代码

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

2020-12-03 08:57后台菜狗子 Java教程

本篇文章主要介绍了spring aop action中验证用户登录状态的实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

想法是这样的:

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

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

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

以下是代码: 

advice代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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:

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

?
1
2
3
4
5
6
7
8
9
10
11
<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继承,这样就可以共享该异常捕获行为:

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

applicationContext.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- 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中配置了这一句:

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

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

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/caser_hdmi/article/details/75205783

延伸 · 阅读

精彩推荐