aspectj 处理异常问题

romever 2014-08-29 10:31:56
本人目前在做一个权限控制的功能,使用的是AOP的aspectj,我采取的是拦截到方法时,如果用户没有权限,那么就抛出异常,然后跳转到相应的页面,我在spring mvc中配置了异常处理的页面,但是不知道什么原因 ,就是无法跳转到指定页面。我这种处理方式有问题吗?希望大神帮帮忙。
切面类中before方法代码:

@Before("controllerMethod()")
public void interceptor(JoinPoint jp) throws PermissionDeniedException {
System.out.println("----------拦截方法开始----------");
Object[] args = jp.getArgs();
HttpServletRequest request = null;
for (int i = 0; i < args.length; i++) {
System.out.println("method:" + args[i].toString());
// 获取当前拦截方法的request对象
if (args[i] instanceof HttpServletRequest) {
request = (HttpServletRequest) args[i];
}
}
User user = getUser(request);
String privilegeUrl = getURL(request);
System.out.println("target:" + jp.getTarget());
System.out.println(jp.getSignature().getName());
System.out.println(getURL(request));
//判断用户是否有权限
if(!hasPrivilegeByUrl(privilegeUrl, request, user)){
System.out.println("判断权限");
throw new PermissionDeniedException("您没有该权限");
}
System.out.println("----------拦截方法结束----------");
}

spring mvc的web-servlet.xml配置:
<!--定义异常处理页面-->
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="cn.huizhupay.interceptor.PermissionDeniedException">permissionException</prop>
</props>
</property>
</bean>
...全文
115 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
romever 2014-08-29
  • 打赏
  • 举报
回复
自己解决了,spring只能拦截到RuntimeException,将自定义异常继承RuntimeException就可以实现页面跳转了

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧