spring mvc 设置web资源不能直接访问
程序员文章站
2023-12-30 18:18:34
...
实现spring mvc 设置web资源不能直接访问,注释掉3. 中的配置即可
1. application
server.web.resources.dir=mqmweb/src/main/webapp
2. 启动类
资源加载
public class Launcher {
private static final Logger LOG = Logger.getLogger(Launcher.class);
public static WebAppServer webAppServer;
public static void main(String[] args) throws Exception {
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
LOG.error(e.getMessage(), e);
}
});
try {
LOG.info("...Now launch mqmid webserver...");
/*LOG.info("step:init app authority");
RolePermission.init();*/
LOG.info("step:init HadoopConfigManager init");
HadoopConfigManager.getInstance().init();
// ImpalaHelper.init();
PrestoHelper.init();
LOG.info("step:start jetty server");
webAppServer = new WebAppServer();
webAppServer.initService();
webAppServer.startService();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
System.exit(1);
}
}
}
public void initService() {
jettyEmbeddedServer = new Server(getThreadPool()) {
public void handle(HttpChannel connection) throws IOException, ServletException {
Request request = connection.getRequest();
if ("/".equals(request.getRequestURI())) {
Response response = connection.getResponse();
response.sendRedirect(webContext);
response.setStatus(Response.SC_OK);
response.closeOutput();
} else {
super.handle(connection);
}
}
};
this.jettyEmbeddedServer.setStopAtShutdown(true);
this.bindAddress = SiteConfig.get("server.bind.http.host", "0.0.0.0");
this.httpPort = SiteConfig.getInt("server.bind.http.port", 8111);
this.jettyEmbeddedServer.addBean(new ScheduledExecutorScheduler("mqmid.webapp.shared.scheduler", true), true);
this.jettyEmbeddedServer.addConnector(makeHttpConnector());
this.jettyEmbeddedServer.setHandler(makeWebAppContext());
}
private WebAppContext makeWebAppContext() {
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath(webContext);
String webResourcesPath = System.getenv("WEB_RESOURCES_PATH");
if (webResourcesPath == null) {
webResourcesPath = System.getProperty("WEB_RESOURCES_PATH");
if (webResourcesPath == null) {
webResourcesPath = SiteConfig.get("server.web.resources.dir", "mqmweb/src/main/webapp");
}
}
File warFile = new File(webResourcesPath);
if (!warFile.exists()) {
throw new RuntimeException(warFile.getAbsolutePath() + " is not found.");
}
this.warFileAbsolutePath = warFile.getAbsolutePath();
webAppContext.setWar(warFile.getAbsolutePath());
webAppContext.addAliasCheck(new AllowSymLinkAliasChecker());
// This webapp will use jsps and jstl. We need to enable the
// AnnotationConfiguration in order to correctly
// set up the jsp container
Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(this.jettyEmbeddedServer);
classlist.addBefore(
"org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
"org.eclipse.jetty.annotations.AnnotationConfiguration");
// Set the ContainerIncludeJarPattern so that jetty examines these
// container-path jars for tlds, web-fragments etc.
// If you omit the jar that contains the jstl .tlds, the jsp engine will
// scan for them instead.
webAppContext.setAttribute(
"org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/[^/]*taglibs.*\\.jar$");
webAppContext.setParentLoaderPriority(true);
return webAppContext;
}
3. web.xml
<web-app>
<!-- Process the static resources -->
<!-- <servlet-mapping>-->
<!-- <servlet-name>default</servlet-name>-->
<!-- <url-pattern>/view/*</url-pattern>-->
<!-- </servlet-mapping>-->
</web-app>
参考:
参考一