Spring AOP实现拦截转发控制
2024-10-20 15:56:38
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class demoInterceptor { private demoInterceptor() { }; @Pointcut("execution(* com.demo.controller.login(..))")
private void interceptor() {
}; @Around("interceptor()")
public Object aroundLogin(ProceedingJoinPoint point) throws Throwable {
HttpServletRequest request = null;
HttpServletResponse response = null;
Object[] args = point.getArgs();
for (int i = ; i < args.length; i++) {
if (args[i] instanceof HttpServletRequest) {
request = (HttpServletRequest) args[i];
}
if (args[i] instanceof HttpServletResponse) {
response = (HttpServletResponse) args[i];
} }
if (request.getParameter("j_username").equals("demo")) { return point.proceed();
} else {
response.sendRedirect("/login.do");
return null;
}
}
}
aop-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy/>
<bean class="demoInterceptor"/>
</beans>
转自:http://www.iprotoss.com/?p=130
最新文章
- Python之路 day3 全局变量、局部变量
- Swift完整项目打包Framework,嵌入OC项目使用
- android之inflater用法
- Emmet插件使用方法总结
- 【windows核心编程】使用远程线程注入DLL
- 【转】stdin, stdout, stderr 以及重定向
- Reids详解-抄本
- [bzoj4872]分手是祝愿
- class对象详解
- String方法
- leetcode 639 Decode Ways II
- 支持pc和移动端的手写签批功能
- 解决wxParse空格不解析的问题
- awk 相关的复习
- Navicat备份、还原mysql数据库
- java 日期工具类DateUtils
- C# 将 Stream 写入文件
- Java-Runoob-高级教程-实例-数组:06. Java 实例 – 数组获取最大和最小值
- maven - 配置强制从指定仓库拉取jar包
- maven将镜像站点改为中国开源镜像点