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配置文件配置一处就可以了。
 
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号