站長資訊網
最全最豐富的資訊網站

自定義Spring Boot內置Tomcat的404頁面

spring boot 的相關404頁面配置都是針對項目路徑下的(如果配置了 context-path)

在context-path不為空的情況下,如果訪問路徑不帶context-path,這時候會顯示空白頁面或者是tomcat默認404頁面

這時候如何自定義內置tomcat的404頁面呢?

查看tomcat錯誤頁面的實現源碼org.apache.catalina.valves.ErrorReportValue:

report方法中先查找是否注冊了錯誤頁面,默認情況未注冊任何錯誤頁面,然后通過sendErrorPage方法發送錯誤頁面

private boolean sendErrorPage(String location, Response  response) {
        File file = new File(location);
        if (!file.isAbsolute()) {
            file = new  File(getContainer().getCatalinaBase(), location);
        }
        if (!file.isFile() || !file.canRead()) {
            getContainer().getLogger().warn(
                    sm.getString(“errorReportValve.errorPageNotFound”,  location));
            return false;
        }
        // Hard coded for now. Consider making this  optional. At Valve level or
        // page level?
        response.setContentType(“text/html”);
        response.setCharacterEncoding(“UTF-8”);
        try (OutputStream os = response.getOutputStream();
                InputStream is = new  FileInputStream(file);){
            IOTools.flow(is, os);
        } catch (IOException e) {
            getContainer().getLogger().warn(
                    sm.getString(“errorReportValve.errorPageIOException”,  location), e);
            return false;
        }
        return true;
    }

由于spring boot 默認打成的jar包運行tomcat,所以必須要把404頁面放到外部,這里先將404.html放到resource目錄下,然后啟動過程中將頁面復制到tomcat臨時目錄,將404路徑指向該頁面就可以了。

這里有兩種實現辦法:

1、通過AOP修改默認注冊的ErrorReportValue

import Java.io.File;
import java.io.IOException;
import javax.servlet.Servlet;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.valves.ErrorReportValve;
import org.apache.coyote.UpgradeProtocol;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import  org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import  org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer;
import  org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import com.bc.core.util.FileUtil;
@Aspect
@ConditionalOnClass({ Servlet.class, Tomcat.class,  UpgradeProtocol.class,  TomcatWebServerFactoryCustomizer.class })
@Component
public class TomcatCustomizerAspect {
    @Pointcut(“execution(public void  org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer.customize(*))”)
    public void customize() {
    }
    @After(value = “customize()”)
    public void doAfter(JoinPoint joinPoint) throws  Throwable {
          if (!(joinPoint.getArgs()[0] instanceof  ConfigurableTomcatWebServerFactory)) {
              return;
          }
          ConfigurableTomcatWebServerFactory factory =  (ConfigurableTomcatWebServerFactory)  joinPoint.getArgs()[0];
          addTomcat404CodePage(factory);
    }
    private static void  addTomcat404CodePage(ConfigurableTomcatWebServerFactory  factory) {
          factory.addContextCustomizers((context) -> {
              String path =  context.getCatalinaBase().getPath() + “/404.html”;
              ClassPathResource cr = new  ClassPathResource(“404.html”);
              if (cr.exists()) {
                  File file404 = new File(path);
                  if (!file404.exists()) {
                        try {
                            FileCopyUtils.copy(cr.getInputStream(),  FileUtil.openOutputStream(file404));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                  }
              }
              ErrorReportValve valve = new  ErrorReportValve();
              valve.setProperty(“errorCode.404”, path);
              valve.setShowServerInfo(false);
              valve.setShowReport(false);
              valve.setAsyncSupported(true);
              context.getParent().getPipeline().addValve(valve);
          });
    }
}

2、通過自定義BeanPostProcessor添加自定義的ErrorReportValve

import java.io.File;
import java.io.IOException;
import javax.servlet.Servlet;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.valves.ErrorReportValve;
import org.apache.coyote.UpgradeProtocol;
import org.springframework.beans.BeansException;
import  org.springframework.beans.factory.config.BeanPostProcessor;
import  org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import  org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer;
import  org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import com.bc.core.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
@ConditionalOnClass({ Servlet.class, Tomcat.class,  UpgradeProtocol.class,  TomcatWebServerFactoryCustomizer.class })
@Component
@Slf4j
public class TomcatCustomizerBeanPostProcessor implements  BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object  bean, String beanName) throws BeansException {
          if (bean instanceof  ConfigurableTomcatWebServerFactory) {
              ConfigurableTomcatWebServerFactory  configurableTomcatWebServerFactory =  (ConfigurableTomcatWebServerFactory) bean;
             
              addTomcat404CodePage(configurableTomcatWebServerFactory);
          }
          return  BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
    private static void  addTomcat404CodePage(ConfigurableTomcatWebServerFactory  factory) {
          factory.addContextCustomizers((context) -> {
              String tomcatTempPath =  context.getCatalinaBase().getPath();
              log.info(“tomcat目錄:{}”, tomcatTempPath);
              String path = tomcatTempPath + “/404.html”;
              ClassPathResource cr = new  ClassPathResource(“404.html”);
              if (cr.exists()) {
                  File file404 = new File(path);
                  if (!file404.exists()) {
                        try {
                            FileCopyUtils.copy(cr.getInputStream(),  FileUtil.openOutputStream(file404));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                  }
              }
              ErrorReportValve valve = new  ErrorReportValve();
              valve.setProperty(“errorCode.404”, path);
              valve.setShowServerInfo(false);
              valve.setShowReport(false);
              valve.setAsyncSupported(true);
              context.getParent().getPipeline().addValve(valve);
          });
    }
}

上面兩種辦法,都就可以達到,如果項目訪問帶項目名,訪問任意錯誤路徑(非項目路徑下的路徑),指向自定義的404頁面

贊(0)
分享到: 更多 (0)
網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
亚洲国产精品久久久久| 国产精品免费久久久久久久久 | 日韩精品一区二区三区大桥未久 | 99久久人妻精品免费二区| 中文字幕无码精品三级在线电影| 国产成人精品久久| 免费国产在线精品一区| 2021免费日韩视频网| 国产免费69成人精品视频| 青草久久精品亚洲综合专区| 久久亚洲精品国产精品婷婷| 精品乱码一区二区三区四区| 精品无码人妻一区二区三区品| 久久无码专区国产精品| 国产精品露脸国语对白| 精品一区二区久久久久久久网站| 思思久久99热只有频精品66| 久久se这里只有精品| 国产精品美女网站| 国产精品免费看久久久香蕉| 亚洲AV日韩精品一区二区三区| 日韩一区二区在线免费观看| 日韩制服丝袜在线| 日韩午夜在线视频| 亚洲成人国产精品| 香蕉依依精品视频在线播放 | 国内精品伊人久久久久网站| 国产午夜精品理论片| 久久久久久久亚洲精品| 久久午夜无码鲁丝片午夜精品| 久久国产香蕉一区精品| 国产午夜精品久久久久九九| 在线观看亚洲精品福利片| 亚洲AV蜜桃永久无码精品| 欧亚精品卡一卡二卡三| 91久久精品91久久性色| 青青热久久久久综合精品| 九九热这里只有在线精品视| 久久线看观看精品香蕉国产| 久久综合久久精品| 久久精品国产亚洲av高清漫画|