当前位置: 移动技术网 > IT编程>开发语言>Java > spring aop action中验证用户登录状态的实例代码

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

2019年07月22日  | 移动技术网IT编程  | 我要评论
最近在学习ssh框架时,照着网上做了一个商城系统,之前在一些需要用户存在的操作中,都是在每一个action中写重复的代码,这样做现在想起来并不好,想起了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" />

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网