描述:
在ShiroConfig类的ShiroFilterFactoryBean过滤器工厂设置:
filterFactory.setLoginUrl("/user/toLogin");//设置未登录时访问未授权站点跳转
filterFactory.setUnauthorizedUrl("/user/toLogin");//设置登录后访问未授权站点跳转
实际操作中,用户登陆后访问未授权的页面(http://127.0.0.1:8089/user/toCreateUser),发现页面并没有跳转到设置好的登陆页面(http://127.0.0.1:8089/user/toLogin),而是
堆栈信息为:
org.apache.shiro.authz.UnauthorizedException: Subject does not have role [admin]
Caused by: org.apache.shiro.authz.AuthorizationException: Not authorized to invoke method: public java.lang.String com.dayuanit.dy16.atm.boatm.controller.PageController.toCreateUser()
2020-10-25 16:26:43.801 ERROR 9864 --- [nio-8089-exec-5] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8089-exec-5] Exception processing template "user/toCreateUser": Error resolving template [user/toCreateUser], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [user/toCreateUser], template might not exist or might not be accessible by any of the configured Template Resolvers
2020-10-25 16:26:43.805 ERROR 9864 --- [nio-8089-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template [user/toCreateUser], template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [user/toCreateUser], template might not exist or might not be accessible by any of the configured Template Resolvers
而在用户未登陆的情况下,访问http://127.0.0.1:8089/user/toCreateUser,会被shiro重定向到登录页。
也就是说,用户登录后访问未授权页面,会抛出被thymeleaf直接返回一个空白页。
这就奇怪了,为什么setLoginUrl生效,setUnauthorizedUrl不生效呢?不知道,估计是因为用了thymeleaf的原因,登录之后访问未授权页面,thymeleaf直接返回白标签页面了,而登录之前会直接被shiro过滤器拦截。
但是解决办法是:https://www.cnblogs.com/kingman/p/13646014.html
即重写ErrorMvcAutoConfiguration中StaticView内部类的render方法,从而替换掉默认的白标签View。
1.自定义一个View视图,把白标签替换掉,我为了简单起见,直接重定向到登录页了。
2.由于使用springboot框架,用java config配置。
经测试,ok!