spring aop action中验证用户登录状态的实例代码
作者:后台菜狗子 时间:2021-12-04 19:47:01
最近在学习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
标签:spring,aop,登录验证
0
投稿
猜你喜欢
android 大图片拖拽并缩放实现原理
2022-11-10 05:59:55
C#使用webbrowser的常见用法实例
2023-06-14 13:57:45
java实现Base64加密解密算法
2023-11-25 08:07:27
android 中 SQLiteOpenHelper的封装使用详解
2022-11-09 12:36:57
MyBatis学习教程(四)-如何快速解决字段名与实体类属性名不相同的冲突问题
2023-11-25 05:43:49
java语法糖之jdk迭代的新特性汇总
2022-07-09 10:05:19
C# 文字代码页 文字编码的代码页名称速查表
2023-12-13 04:03:54
使用okhttp替换Feign默认Client的操作
2021-10-03 16:57:59
解决Android ListView数据为空及加载错误的方法
2022-11-30 06:57:41
C#中使用资源的方法分析
2022-01-16 16:27:02
Android 文件读写操作方法总结
2023-12-22 22:52:29
java面向对象之人机猜拳小游戏
2021-12-20 18:12:34
Java Stopwatch类,性能与时间计时器案例详解
2023-07-24 04:08:50
Android 之BottomsheetDialogFragment仿抖音评论底部弹出对话框效果(实例代码)
2023-08-06 01:01:56
Spring Boot 中密码加密的两种方法
2021-09-12 15:08:38
解析使用enumerator模式简化异步操作的详解
2021-10-08 01:44:54
C#中常用的正则表达式
2023-10-15 21:10:42
C#获取汉字字符串拼音首字母的方法
2022-09-06 14:01:12
Mybatis延迟加载和缓存深入讲解
2022-06-02 15:50:43
Java JVM内存区域详解
2021-05-25 18:44:43