springmvc shiro UnauthorizedException 异常解决方案
springMVC 整合 shiro 时,配置了当访问某个URL没有权限时的配置处理:
<!-- 通过unauthorizedUrl指定没有权限操作时跳转页面 -->
<property name="unauthorizedUrl" value="/refuse"/>
但是,上面的配置没有效果,就是当用户没有权限的时候不会运行"/refuse"这个URL路径,而是直接在页面显示出UnauthorizedException错误信息。
现提供以下几种解决方案:
1. 在控制器上添加 @ExceptionHandler()统一处理某个类异常:
/**
* 访问某个URL没有权限时抛出UnauthorizedException,
* @ExceptionHandler({UnauthorizedException.class})注解表示处理:UnauthorizedException 异常,
* 当异常出现时,返回 refuse 视图。
*/
@ExceptionHandler({UnauthorizedException.class})
public ModelAndView processUnauthenticatedException(NativeWebRequest request,
UnauthorizedException e) {
ModelAndView mv = new ModelAndView();
mv.addObject("exception", e.getMessage());
mv.setViewName("refuse");
return mv;
}
当在访问本控制器时,出现UnauthorizedException异常,则统一跳转到refuse视图。但是此方案需要在每个控制器中实现。重复代码较多。
2. 在springmvc的配置文件中来统一处理异常方案
<!-- shiro为集成springMvc 拦截异常 -->
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 这里你可以根据需要定义N多个错误异常转发 -->
<prop key="org.apache.shiro.authz.UnauthorizedException">refuse</prop>
<prop key="org.apache.shiro.authz.UnauthenticatedException">refuse</prop>
<prop key="java.lang.IllegalArgumentException">/error</prop>
<!-- 参数错误(bizError.jsp) -->
<prop key="java.lang.Exception">/error</prop>
</props>
</property>
</bean>
这样子的话,就只在SpringMVC配置文件配置一处就可以了。
springmvc shiro UnauthorizedException 异常解决方案的更多相关文章
- springmvc+shiro认证框架配置
1,在web.xml中配置fiter,如下所示 <!-- shiro的filter --> <!-- shiro过虑器,DelegatingFilterProxy通过代理模式将spr ...
- getActionBar().setTitle(); Java.lang.NullPoint异常解决方案
getActionBar().setTitle(); Java.lang.NullPoint异常解决方案,是由于低版本不支持直接获取的缘故,修改方案: try changing your theme ...
- 关于SpringMVC的全局异常处理器
近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...
- SpringMVC+Shiro权限管理(转载)
源码 http://pan.baidu.com/s/1pJzG4t1 SpringMVC+Shiro权限管理 博文目录 权限的简单描述 实例表结构及内容及POJO Shiro-pom.xml Shir ...
- (转)SpringMVC学习(八)——SpringMVC中的异常处理器
http://blog.csdn.net/yerenyuan_pku/article/details/72511891 SpringMVC在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常 ...
- springmvc+shiro+freemarker实现的安全及权限管理
本文讲述了基于springmvc+shiro实现安全管理,shiro+freemarker实现权限验证. 首先我们从web.xml开始: <?xml version="1.0" ...
- Springmvc+Shiro实战
原文链接:http://blog.csdn.net/qq_37936542/article/details/79010449 springmvc+shiro实现系统粗细粒度的权限管理步骤: 1:表格设 ...
- 基于SpringMVC的全局异常处理器介绍(转)
近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...
- SpringMVC实现全局异常处理器 (转)
出处: SpringMVC实现全局异常处理器 我们知道,系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手 ...
随机推荐
- 【leetcode】453. Minimum Moves to Equal Array Elements
problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...
- [LeetCode&Python] Problem 925. Long Pressed Name
Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might ...
- 常见模块(一) time/datetime
1 time模块 1)时间三种格式的转化 2)time模块的相关方法 time.time() 打印当前时间的时间戳 单位是秒 距离1970年1月1日到当前的时间差 time.sleep(n) ...
- 全志A33 lichee 搭建Qt App开发环境编写helloworld
开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 Step 1 在虚拟机(Ce ...
- Dynamics 365 CRM Free up storage 清理Dynamics 365 CRM的空间
Dynamics 365 CRM 的空间是要买的. 但是很多情况下用户可以去清理CRM从而达到给空间减重的方法 两大使用DB空间大的功能 1. Audit log 审计记录 审计记录是用来记录各个fi ...
- 第3章 Java数组(上): 一维数组和二维数组
3.数组及排序算法(2天) 3.1 数组的概述 2课时 3.2 一维数组的使用 3课时 3.3 多维数组的使用 3课时 3.4 数组中涉及到的常见算法 3课时 3.5 Arrays工具类的使用 3课时 ...
- vue+elementui按需引入
转载自以下网址,仅作备忘之用:https://www.cnblogs.com/lwj820876312/p/9169457.html 基于Vue的Ui框架 饿了么公司基于vue开的的vue的Ui组件库 ...
- .gitlab-ci.yml 配置文件,知识点
官方介绍:https://docs.gitlab.com/ee/ci/yaml/README.html 翻译: https://segmentfault.com/a/1190000010442764
- hadoop完全分步式搭建
实验环境介绍 4台机器,规划如下: 计算机名 IP地址 角色 master 192.168.138.200 NameNode,SecondaryNameNode,ResourceManager sla ...
- C语言小程序:除去字符串中间不需要的字符(从小引发大思考)
#include <stdio.h>#include <conio.h> void fun(char *a, char *h, char *p){ char b[81]; ch ...