shiro unauthorizedUrl页面不跳转问题

来源:互联网 发布:淘宝客服真的好忙好累 编辑:程序博客网 时间:2024/05/24 00:35

  最近使用shiro框架控制用户权限,用户权限认证未通过时,无法跳转到unauthorizedUrl对应的页面,直接抛出了异常

解决方法:

1、mvc

<!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常页名作为值 -->
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">none_authority</prop>
</props>
</property>

2、web.xml

  1. <error-page>  
  2.       <error-code>500</error-code>  
  3.       <location>/error.jsp</location>  
  4. </error-page>  

原因:

shiro的源代码ShiroFilterFactoryBean.java

[java] view plaincopy
  1. private void applyUnauthorizedUrlIfNecessary(Filter filter) {  
  2.     String unauthorizedUrl = getUnauthorizedUrl();  
  3.     if (StringUtils.hasText(unauthorizedUrl) && (filter instanceof AuthorizationFilter)) {  
  4.         AuthorizationFilter authzFilter = (AuthorizationFilter) filter;  
  5.         //only apply the unauthorizedUrl if they haven't explicitly configured one already:  
  6.         String existingUnauthorizedUrl = authzFilter.getUnauthorizedUrl();  
  7.         if (existingUnauthorizedUrl == null) {  
  8.             authzFilter.setUnauthorizedUrl(unauthorizedUrl);  
  9.         }  
  10.     }  
  11. }  

定义的filter必须满足filter instanceof AuthorizationFilter,只有perms,roles,ssl,rest,port才是属于AuthorizationFilter,而anon,authcBasic,auchc,user是AuthenticationFilter,所以unauthorizedUrl设置后页面不跳转