shiro 无法跳转到unauthorizedUrl

来源:互联网 发布:淘宝卖家折800怎么报名 编辑:程序博客网 时间:2024/06/04 19:06

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


原因:

  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.     }  
定义的filter必须满足filter instanceof AuthorizationFilter,只有perms,roles,ssl,rest,port才是属于AuthorizationFilter,而anon,authcBasic,auchc,user是AuthenticationFilter,所以unauthorizedUrl设置后页面不跳转

解决方案:

方案一:

<error-page>
<exception-type>org.apache.shiro.authz.UnauthorizedException</exception-type>
<location>/unauthorized.jsp</location>
</error-page>

方案二:使用perms,roles,ssl,rest,port

方案三:使用@ExceptionHandler(UnauthorizedException.class)

0 0
原创粉丝点击