springboot整合shiro-Whitelabel Error Page解决(三)

原文:https://blog.csdn.net/qq_34021712/article/details/80294417 ©王赛超

解决 Whitelabel Error Page

在上一篇 快速入门的博客最后,我们提到两个问题依然还没有解决,第一个就是错误页面的显示,我们最后配置好权限校验之后,访问无权限的后台服务,会报异常,说是访问没有权限的方法,异常如下:
这里写图片描述
页面显示为默认springboot展示的页面Whitelabel Error Page,具体页面如下,并没有跳转到我们之前配置的页面 。
也就是说shiroFilterFactoryBean.setUnauthorizedUrl(“/unauthorized”);无效!但是有时候我们需要向客户展示友好界面,所以,还需要如下配置:

在ShiroConfig配置类中,添加以下bean

/**
 * 解决: 无权限页面不跳转 shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized") 无效
 * shiro的源代码ShiroFilterFactoryBean.Java定义的filter必须满足filter instanceof AuthorizationFilter,
 * 只有perms,roles,ssl,rest,port才是属于AuthorizationFilter,而anon,authcBasic,auchc,user是AuthenticationFilter,
 * 所以unauthorizedUrl设置后页面不跳转 Shiro注解模式下,登录失败与没有权限都是通过抛出异常。
 * 并且默认并没有去处理或者捕获这些异常。在SpringMVC下需要配置捕获相应异常来通知用户信息
 * @return
 */
@Bean
public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
    SimpleMappingExceptionResolver simpleMappingExceptionResolver=new SimpleMappingExceptionResolver();
    Properties properties=new Properties();
    //这里的 /unauthorized 是页面,不是访问的路径
    properties.setProperty("org.apache.shiro.authz.UnauthorizedException","/unauthorized");
    properties.setProperty("org.apache.shiro.authz.UnauthenticatedException","/unauthorized");
    simpleMappingExceptionResolver.setExceptionMappings(properties);
    return simpleMappingExceptionResolver;
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在resources/templates添加unauthorized.html页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8" />
    <title>Insert title here</title>
</head>
<body>
<h1>对不起,您没有权限</h1>
</body>
</html>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

然后重新启动程序,发现访问无权限的后台服务,就会跳转到我们配置的页面,但是这个时候还有一个问题:
访问不存在的后台服务,如/userInfo/adddfdsafsafsa 这样后面随便乱打的,这种情况,依然还是返回了Whitelabel Error Page页面,我们不想看到这种效果,所以还需要在ShiroConfig配置类中,添加以下bean

/**
 * 解决spring-boot Whitelabel Error Page
 * @return
 */
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {

    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {

            ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/unauthorized.html");
            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
            ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

            container.addErrorPages(error401Page, error404Page, error500Page);
        }
    };
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

注意:然后在resource/static路径下也添加一个unauthorized.html,404.html 和500.html,页面都差不多,只是文字显示不一样。只能把页面放在resource/static下面。我也不知道为什么,如果没有这些页面,默认会是一片白,没有去进一步查看原因。然后再次重启,使用admin登录。
访问http://localhost:9090/userInfo/delfdsafadsfs 后面随便打,因为服务根本不存在,就会跳转到404页面。
如果访问http://localhost:9090/userInfo/del 就会跳转到unauthorized.html 因为没有权限。

猜你喜欢

转载自blog.csdn.net/qq_43298012/article/details/87890947