【解决去除springboot-内嵌tomcat的异常信息显示】去掉版本号和异常信息

调用这个,能复现tomcat的报错
http://localhost:8182/defaultroot/DownloadServlet?modeType=2&path=html&FileName=…\login.jsp&name=123&fiewviewdownload=2&cd=inline&downloadAll=2
在这里插入图片描述springboot项目如何隐藏?

springboot内嵌了tomat,比如这个版本:tomcat-embed-core-8.5.96
找到这个tomcat-embed-core的源码,
将showReport=true,修改为showReport=false
将showServerInfo=true ,修改为showServerInfo=false

方法一:
在项目中增加ErrorReportValve.java ,实现覆盖内嵌中的类。

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package org.apache.catalina.valves;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.servlet.ServletException;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.util.ErrorPageSupport;
import org.apache.catalina.util.IOTools;
import org.apache.catalina.util.ServerInfo;
import org.apache.coyote.ActionCode;
import org.apache.tomcat.util.ExceptionUtils;
import org.apache.tomcat.util.descriptor.web.ErrorPage;
import org.apache.tomcat.util.res.StringManager;
import org.apache.tomcat.util.security.Escape;/*** 解决调试信息泄露漏洞,修改showReport=false,showServerInfo=false*/
public class ErrorReportValve extends ValveBase {private boolean showReport = false;private boolean showServerInfo = false;private final ErrorPageSupport errorPageSupport = new ErrorPageSupport();public ErrorReportValve() {super(true);}public void invoke(Request request, Response response) throws IOException, ServletException {this.getNext().invoke(request, response);if (response.isCommitted()) {if (response.setErrorReported()) {AtomicBoolean ioAllowed = new AtomicBoolean(true);response.getCoyoteResponse().action(ActionCode.IS_IO_ALLOWED, ioAllowed);if (ioAllowed.get()) {try {response.flushBuffer();} catch (Throwable var5) {ExceptionUtils.handleThrowable(var5);}response.getCoyoteResponse().action(ActionCode.CLOSE_NOW, request.getAttribute("javax.servlet.error.exception"));}}} else {Throwable throwable = (Throwable)request.getAttribute("javax.servlet.error.exception");if (!request.isAsync() || request.isAsyncCompleting()) {if (throwable != null && !response.isError()) {response.reset();response.sendError(500);}response.setSuspended(false);try {this.report(request, response, throwable);} catch (Throwable var6) {ExceptionUtils.handleThrowable(var6);}}}}protected ErrorPage findErrorPage(int statusCode, Throwable throwable) {ErrorPage errorPage = null;if (throwable != null) {errorPage = this.errorPageSupport.find(throwable);}if (errorPage == null) {errorPage = this.errorPageSupport.find(statusCode);}if (errorPage == null) {errorPage = this.errorPageSupport.find(0);}return errorPage;}protected void report(Request request, Response response, Throwable throwable) {int statusCode = response.getStatus();if (statusCode >= 400 && response.getContentWritten() <= 0L && response.setErrorReported()) {AtomicBoolean result = new AtomicBoolean(false);response.getCoyoteResponse().action(ActionCode.IS_IO_ALLOWED, result);if (result.get()) {ErrorPage errorPage = this.findErrorPage(statusCode, throwable);if (errorPage == null || !this.sendErrorPage(errorPage.getLocation(), response)) {String message = Escape.htmlElementContent(response.getMessage());String reason;if (message == null) {if (throwable != null) {reason = throwable.getMessage();if (reason != null && reason.length() > 0) {Scanner scanner = new Scanner(reason);try {message = Escape.htmlElementContent(scanner.nextLine());} catch (Throwable var17) {try {scanner.close();} catch (Throwable var15) {var17.addSuppressed(var15);}throw var17;}scanner.close();}}if (message == null) {message = "";}}reason = null;String description = null;StringManager smClient = StringManager.getManager("org.apache.catalina.valves", request.getLocales());response.setLocale(smClient.getLocale());try {reason = smClient.getString("http." + statusCode + ".reason");description = smClient.getString("http." + statusCode + ".desc");} catch (Throwable var16) {ExceptionUtils.handleThrowable(var16);}if (reason == null || description == null) {if (message.isEmpty()) {return;}reason = smClient.getString("errorReportValve.unknownReason");description = smClient.getString("errorReportValve.noDescription");}StringBuilder sb = new StringBuilder();sb.append("<!doctype html><html lang=\"");sb.append(smClient.getLocale().getLanguage()).append("\">");sb.append("<head>");sb.append("<title>");sb.append(smClient.getString("errorReportValve.statusHeader", new Object[]{String.valueOf(statusCode), reason}));sb.append("</title>");sb.append("<style type=\"text/css\">");sb.append("body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}");sb.append("</style>");sb.append("</head><body>");sb.append("<h1>");sb.append(smClient.getString("errorReportValve.statusHeader", new Object[]{String.valueOf(statusCode), reason})).append("</h1>");if (this.isShowReport()) {sb.append("<hr class=\"line\" />");sb.append("<p><b>");sb.append(smClient.getString("errorReportValve.type"));sb.append("</b> ");if (throwable != null) {sb.append(smClient.getString("errorReportValve.exceptionReport"));} else {sb.append(smClient.getString("errorReportValve.statusReport"));}sb.append("</p>");if (!message.isEmpty()) {sb.append("<p><b>");sb.append(smClient.getString("errorReportValve.message"));sb.append("</b> ");sb.append(message).append("</p>");}sb.append("<p><b>");sb.append(smClient.getString("errorReportValve.description"));sb.append("</b> ");sb.append(description);sb.append("</p>");if (throwable != null) {String stackTrace = this.getPartialServletStackTrace(throwable);sb.append("<p><b>");sb.append(smClient.getString("errorReportValve.exception"));sb.append("</b></p><pre>");sb.append(Escape.htmlElementContent(stackTrace));sb.append("</pre>");int loops = 0;for(Throwable rootCause = throwable.getCause(); rootCause != null && loops < 10; ++loops) {stackTrace = this.getPartialServletStackTrace(rootCause);sb.append("<p><b>");sb.append(smClient.getString("errorReportValve.rootCause"));sb.append("</b></p><pre>");sb.append(Escape.htmlElementContent(stackTrace));sb.append("</pre>");rootCause = rootCause.getCause();}sb.append("<p><b>");sb.append(smClient.getString("errorReportValve.note"));sb.append("</b> ");sb.append(smClient.getString("errorReportValve.rootCauseInLogs"));sb.append("</p>");}sb.append("<hr class=\"line\" />");}if (this.isShowServerInfo()) {sb.append("<h3>").append(ServerInfo.getServerInfo()).append("</h3>");}sb.append("</body></html>");try {try {response.setContentType("text/html");response.setCharacterEncoding("utf-8");} catch (Throwable var18) {ExceptionUtils.handleThrowable(var18);if (this.container.getLogger().isDebugEnabled()) {this.container.getLogger().debug("Failure to set the content-type of response", var18);}}Writer writer = response.getReporter();if (writer != null) {writer.write(sb.toString());response.finishResponse();}} catch (IllegalStateException | IOException var19) {}}}}}protected String getPartialServletStackTrace(Throwable t) {StringBuilder trace = new StringBuilder();trace.append(t.toString()).append(System.lineSeparator());StackTraceElement[] elements = t.getStackTrace();int pos = elements.length;int i;for(i = elements.length - 1; i >= 0; --i) {if (elements[i].getClassName().startsWith("org.apache.catalina.core.ApplicationFilterChain") && elements[i].getMethodName().equals("internalDoFilter")) {pos = i;break;}}for(i = 0; i < pos; ++i) {if (!elements[i].getClassName().startsWith("org.apache.catalina.core.")) {trace.append('\t').append(elements[i].toString()).append(System.lineSeparator());}}return trace.toString();}private boolean sendErrorPage(String location, Response response) {File file = new File(location);if (!file.isAbsolute()) {file = new File(this.getContainer().getCatalinaBase(), location);}if (file.isFile() && file.canRead()) {response.setContentType("text/html");response.setCharacterEncoding("UTF-8");try {OutputStream os = response.getOutputStream();InputStream is = new FileInputStream(file);IOTools.flow(is, os);return true;} catch (IOException var6) {this.getContainer().getLogger().warn(sm.getString("errorReportValve.errorPageIOException", new Object[]{location}), var6);return false;}} else {this.getContainer().getLogger().warn(sm.getString("errorReportValve.errorPageNotFound", new Object[]{location}));return false;}}public void setShowReport(boolean showReport) {this.showReport = showReport;}public boolean isShowReport() {return this.showReport;}public void setShowServerInfo(boolean showServerInfo) {this.showServerInfo = showServerInfo;}public boolean isShowServerInfo() {return this.showServerInfo;}public boolean setProperty(String name, String value) {ErrorPage ep;if (name.startsWith("errorCode.")) {int code = Integer.parseInt(name.substring(10));ep = new ErrorPage();ep.setErrorCode(code);ep.setLocation(value);this.errorPageSupport.add(ep);return true;} else if (name.startsWith("exceptionType.")) {String className = name.substring(14);ep = new ErrorPage();ep.setExceptionType(className);ep.setLocation(value);this.errorPageSupport.add(ep);return true;} else {return false;}}public String getProperty(String name) {String result;ErrorPage ep;if (name.startsWith("errorCode.")) {int code = Integer.parseInt(name.substring(10));ep = this.errorPageSupport.find(code);if (ep == null) {result = null;} else {result = ep.getLocation();}} else if (name.startsWith("exceptionType.")) {String className = name.substring(14);ep = this.errorPageSupport.find(className);if (ep == null) {result = null;} else {result = ep.getLocation();}} else {result = null;}return result;}
}

方法二:
将修改好的.class文件替换tomcat-embed-core.jar的源码
重新编译,将生成的ErrorReportValve.class替换掉原始的tomcat-embed-core的jar包。

解决!
在这里插入图片描述
在这里插入图片描述
修复完成

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/14.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2024年Q1季度冰箱行业线上市场销售数据分析

Q1季度冰箱线上市场表现不如预期。 根据鲸参谋数据显示&#xff0c;2024年1月至3月线上电商平台&#xff08;京东天猫淘宝&#xff09;冰箱累计销量约410万件&#xff0c;环比下降11%&#xff0c;同比下降21%&#xff1b;累计销售额约98亿元&#xff0c;环比下降31%&#xff0…

好用的 with as

简介 with as 相当于视图&#xff0c;是子查询的另一种写法&#xff0c;原理是将建立一个临时表。在做分析时如果需要多次使用到某些中间表可以使用该方法&#xff0c;格式如下&#xff1a; with SQL_1 as (SELECT ***),SQL_2 as (SELECT ***) select * from XXX where XXX a…

spring-数据处理及跳转

结果跳转方式 ModelAndView 设置ModelAndView对象 , 根据view的名称 , 和视图解析器跳到指定的页面 . 页面 : {视图解析器前缀} viewName {视图解析器后缀} <!-- 视图解析器 --> <bean class"org.springframework.web.servlet.view.InternalResourceViewRes…

Vue源码解读学习

Vue源码 观察者模式 & 发布订阅 观察者模式&#xff1a;中心一对多 系统单点间的灵活和拓展&#xff08;广播的方式&#xff09; 发布订阅&#xff1a;将注册列表遍历发布给订阅者 initInject initState initProvide他们挂载顺序为什么这样设计&#xff1f; initstate…

解决npm run dev跑项目,发现node版本不匹配,怎么跑起来?【已解决】

首先问题点就是我们npm run dev 运行项目的时候发现出错&#xff0c;跑不起来&#xff0c;类型下面这种 这里的出错的原因在于我们的node版本跟项目的版本不匹配 解决办法 我这里的问题是我的版本是node14的&#xff0c;然后项目需要node20的&#xff0c;执行下面的就可以正…

html接入高德地图

1.申请key key申请地址&#xff1a;https://console.amap.com/dev/key/app 官方文档 https://lbs.amap.com/api/javascript-api-v2/summary 2.html接入示例 需要将YOUR_KEY替换成自己的key <!doctype html> <html> <head><meta charset"utf-…