springboot自定义异常页面


本文以springboot+thmyleaf+shiro为列。

    1.官方默认的视图路径如下

    2.而默认异常页面路径即为:src/main/resources/templates/error;结构如下:

      

src/
 +- main/
     +- java/
     |   + <source code>
     +- resources/
         +- public/
             +- error/
             |   +- 404.html
        |   +- 403.html
        |   +- 500.html
  
      +- <other public assets>
    

    

    3.自定义异常视图处理器:

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *异常视图处理器
 *
 * @author 
 * @date 2019/4/17
 */
public class ExcetionViewResolver implements HandlerExceptionResolver {

    private static final Logger log= LoggerFactory.getLogger(ExcetionViewResolver.class);
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView = new ModelAndView();

        if ((ex instanceof AuthenticationException) ) {
            modelAndView.setViewName("/error/403");
            modelAndView.getModel().put("msg","登录失败!");
        } else if(ex instanceof  AuthorizationException) {
            modelAndView.setViewName("/error/403");
            modelAndView.getModel().put("msg","没有权限!");
        }else{
            modelAndView.setViewName("/error/500");
            modelAndView.getModel().put("msg","发生未知异常!");
        }
        return modelAndView;
    }
}
                    

    4.注入自定义处理器; 类似于注册filter、servlet等组件,参考 https://blog.csdn.net/king_is_everyone/article/details/53116744

 

/**
 * @author 
 * @date 2018/9/23
 */
@Controller
@SpringBootApplication
public class BootAllRun {
    public static void main(String[] args) {
        SpringApplication.run(BootAllRun.class,args);
  
    }

    
    /**
     * 添加自定义异常-视图 处理器
     *
     * @author 
     * @date 2019/4/17 15:56
     * @param 
     * @return  [返回值说明]
    */
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers){
                exceptionResolvers.add(new ExcetionViewResolver());
            }
        };
    }
}

 

    5.权限方法

    

 

 

    6.运行结果:

      处理前:

     

 

     

 

      处理后:

    

    

    最后:官网也提供了,自定义错误页面解决样例。但处理关键以HttpStatus status值做划分,异常时则是统一的处理结果(500页面)。本文细化到具体异常匹配处理结果 (官网也提供了依据@ExceptionHandler 处理的思路)。

 

    总结:SpringMVC框架是围绕着处理所有的HTTP请求和响应的DispatcherServlet的设计。DispatcherServlet控制整个处理流程(包括本文的异常处理DispatcherServlet的handlerExceptionResolvers属性关联),再通过springboot的相关组件注入即可达到相关流程控制。

     

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM