@Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { ErrorPage page404 = new ErrorPage(HttpStatus.NOT_FOUND,"/404"); container.addErrorPages(page404); } }; // return (container -> { // ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.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); // }); }
/** * Configure the Tomcat {@link Context}. * @param context the Tomcat context * @param initializers initializers to apply */ protected void configureContext(Context context, ServletContextInitializer[] initializers) { TomcatStarter starter = new TomcatStarter(initializers); if (context instanceof TomcatEmbeddedContext) { // Should be true ((TomcatEmbeddedContext) context).setStarter(starter); } context.addServletContainerInitializer(starter, NO_CLASSES); for (LifecycleListener lifecycleListener : this.contextLifecycleListeners) { context.addLifecycleListener(lifecycleListener); } for (Valve valve : this.contextValves) { context.getPipeline().addValve(valve); } for (ErrorPage errorPage : getErrorPages()) { new TomcatErrorPage(errorPage).addToContext(context); } for (MimeMappings.Mapping mapping : getMimeMappings()) { context.addMimeMapping(mapping.getExtension(), mapping.getMimeType()); } configureSession(context); for (TomcatContextCustomizer customizer : this.tomcatContextCustomizers) { customizer.customize(context); } }
public void addToContext(Context context) { Assert.state(this.nativePage != null, "Neither Tomcat 7 nor 8 detected so no native error page exists"); if (ClassUtils.isPresent("org.apache.tomcat.util.descriptor.web.ErrorPage", null)) { org.apache.tomcat.util.descriptor.web.ErrorPage errorPage = (org.apache.tomcat.util.descriptor.web.ErrorPage) this.nativePage; errorPage.setLocation(this.location); errorPage.setErrorCode(this.errorCode); errorPage.setExceptionType(this.exceptionType); context.addErrorPage(errorPage); } else { callMethod(this.nativePage, "setLocation", this.location, String.class); callMethod(this.nativePage, "setErrorCode", this.errorCode, int.class); callMethod(this.nativePage, "setExceptionType", this.exceptionType, String.class); callMethod(context, "addErrorPage", this.nativePage, this.nativePage.getClass()); } }
private void addJettyErrorPages(ErrorHandler errorHandler, Collection<ErrorPage> errorPages) { if (errorHandler instanceof ErrorPageErrorHandler) { ErrorPageErrorHandler handler = (ErrorPageErrorHandler) errorHandler; for (ErrorPage errorPage : errorPages) { if (errorPage.isGlobal()) { handler.addErrorPage(ErrorPageErrorHandler.GLOBAL_ERROR_PAGE, errorPage.getPath()); } else { if (errorPage.getExceptionName() != null) { handler.addErrorPage(errorPage.getExceptionName(), errorPage.getPath()); } else { handler.addErrorPage(errorPage.getStatusCode(), errorPage.getPath()); } } } } }
@Test public void unauthorizedWithErrorPath() throws Exception { this.filter.addErrorPages(new ErrorPage("/error")); this.chain = new MockFilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { ((HttpServletResponse) response).sendError(401, "UNAUTHORIZED"); super.doFilter(request, response); } }; this.filter.doFilter(this.request, this.response, this.chain); assertThat(this.chain.getRequest(), equalTo((ServletRequest) this.request)); HttpServletResponseWrapper wrapper = (HttpServletResponseWrapper) this.chain .getResponse(); assertThat(wrapper.getResponse(), equalTo((ServletResponse) this.response)); assertTrue(this.response.isCommitted()); assertThat(wrapper.getStatus(), equalTo(401)); // The real response has to be 401 as well... assertThat(this.response.getStatus(), equalTo(401)); assertThat(this.response.getForwardedUrl(), equalTo("/error")); }
@Test public void globalError() throws Exception { this.filter.addErrorPages(new ErrorPage("/error")); this.chain = new MockFilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { ((HttpServletResponse) response).sendError(400, "BAD"); super.doFilter(request, response); } }; this.filter.doFilter(this.request, this.response, this.chain); assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus(), equalTo(400)); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE), equalTo((Object) 400)); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE), equalTo((Object) "BAD")); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI), equalTo((Object) "/test/path")); assertTrue(this.response.isCommitted()); assertThat(this.response.getForwardedUrl(), equalTo("/error")); }
@Test public void statusError() throws Exception { this.filter.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400")); this.chain = new MockFilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { ((HttpServletResponse) response).sendError(400, "BAD"); super.doFilter(request, response); } }; this.filter.doFilter(this.request, this.response, this.chain); assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus(), equalTo(400)); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE), equalTo((Object) 400)); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE), equalTo((Object) "BAD")); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI), equalTo((Object) "/test/path")); assertTrue(this.response.isCommitted()); assertThat(this.response.getForwardedUrl(), equalTo("/400")); }
@Test public void statusErrorWithCommittedResponse() throws Exception { this.filter.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400")); this.chain = new MockFilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { ((HttpServletResponse) response).sendError(400, "BAD"); response.flushBuffer(); super.doFilter(request, response); } }; this.filter.doFilter(this.request, this.response, this.chain); assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus(), equalTo(400)); assertTrue(this.response.isCommitted()); assertThat(this.response.getForwardedUrl(), is(nullValue())); }
@Test public void exceptionError() throws Exception { this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500")); this.chain = new MockFilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { super.doFilter(request, response); throw new RuntimeException("BAD"); } }; this.filter.doFilter(this.request, this.response, this.chain); assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus(), equalTo(500)); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE), equalTo((Object) 500)); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE), equalTo((Object) "BAD")); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE), equalTo((Object) RuntimeException.class.getName())); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI), equalTo((Object) "/test/path")); assertTrue(this.response.isCommitted()); assertThat(this.response.getForwardedUrl(), equalTo("/500")); }
@Test public void subClassExceptionError() throws Exception { this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500")); this.chain = new MockFilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { super.doFilter(request, response); throw new IllegalStateException("BAD"); } }; this.filter.doFilter(this.request, this.response, this.chain); assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus(), equalTo(500)); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE), equalTo((Object) 500)); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE), equalTo((Object) "BAD")); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE), equalTo((Object) IllegalStateException.class.getName())); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI), equalTo((Object) "/test/path")); assertTrue(this.response.isCommitted()); }
@Test public void responseIsCommitedWhenExceptionIsThrownDuringAsyncDispatch() throws Exception { this.filter.addErrorPages(new ErrorPage("/error")); setUpAsyncDispatch(); this.chain = new MockFilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { super.doFilter(request, response); throw new RuntimeException("BAD"); } }; this.filter.doFilter(this.request, this.response, this.chain); assertThat(this.chain.getRequest(), equalTo((ServletRequest) this.request)); assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse(), equalTo((ServletResponse) this.response)); assertTrue(this.response.isCommitted()); }
@Test public void responseIsCommitedWhenStatusIs400PlusDuringAsyncDispatch() throws Exception { this.filter.addErrorPages(new ErrorPage("/error")); setUpAsyncDispatch(); this.chain = new MockFilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { super.doFilter(request, response); ((HttpServletResponse) response).sendError(400, "BAD"); } }; this.filter.doFilter(this.request, this.response, this.chain); assertThat(this.chain.getRequest(), equalTo((ServletRequest) this.request)); assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse(), equalTo((ServletResponse) this.response)); assertTrue(this.response.isCommitted()); }
@Test public void nestedServletExceptionIsUnwrapped() throws Exception { this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500")); this.chain = new MockFilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { super.doFilter(request, response); throw new NestedServletException("Wrapper", new RuntimeException("BAD")); } }; this.filter.doFilter(this.request, this.response, this.chain); assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus(), equalTo(500)); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE), equalTo((Object) 500)); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE), equalTo((Object) "BAD")); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE), equalTo((Object) RuntimeException.class.getName())); assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI), equalTo((Object) "/test/path")); assertTrue(this.response.isCommitted()); assertThat(this.response.getForwardedUrl(), equalTo("/500")); }
@Override public void customize(ConfigurableEmbeddedServletContainer container) { if (this.managementServerProperties == null) { this.managementServerProperties = BeanFactoryUtils .beanOfTypeIncludingAncestors(this.beanFactory, ManagementServerProperties.class); this.server = BeanFactoryUtils.beanOfTypeIncludingAncestors( this.beanFactory, ServerProperties.class); } // Customize as per the parent context first (so e.g. the access logs go to // the same place) this.server.customize(container); // Then reset the error pages container.setErrorPages(Collections.<ErrorPage>emptySet()); // and add the management-specific bits container.setPort(this.managementServerProperties.getPort()); container.setAddress(this.managementServerProperties.getAddress()); container.addErrorPages(new ErrorPage(this.server.getError().getPath())); }
@Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { ErrorPage error = new ErrorPage("/error.html"); container.addErrorPages(error); } }; }
@Override public void customize(ConfigurableEmbeddedServletContainer container) { //404错误页面 container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/notFound")); //500错误页面 container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/serverError")); //500错误页面 container.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/unauthorized")); }
@Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/pageNotFound"); container.addErrorPages(error404Page); } }; }
@Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return container -> { container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html")); container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error.html")); }; }
@Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/web/error/404"); container.addErrorPages(error404Page); } }; }
@Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.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); } }; }
@Bean public EmbeddedServletContainerCustomizer containerCustomizer(){ return new EmbeddedServletContainerCustomizer(){ @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404")); container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500")); container.addErrorPages(new ErrorPage(java.lang.Throwable.class,"/error/500")); } }; }
@Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html"); container.addErrorPages(error401Page); } }; }
@Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/WEB-INF/views/error/401.jsp"); ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/WEB-INF/views/error/404.jsp"); ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/WEB-INF/views/error/500.jsp"); container.addErrorPages(error401Page, error404Page, error500Page); } }; }
@Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return container -> { //config container paras container.setSessionTimeout(24, TimeUnit.HOURS); // config error page container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/views/400.html")); container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/views/500.html")); container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/views/400.html")); }; }
@Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); factory.setPort(8080); factory.setSessionTimeout(24, TimeUnit.HOURS); factory.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/views/400.html")); factory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/views/500.html")); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/views/400.html")); return factory; }
@Override public void addErrorPages(ErrorPage... errorPages) { for (ErrorPage errorPage : errorPages) { if (errorPage.isGlobal()) { this.global = errorPage.getPath(); } else if (errorPage.getStatus() != null) { this.statuses.put(errorPage.getStatus().value(), errorPage.getPath()); } else { this.exceptions.put(errorPage.getException(), errorPage.getPath()); } } }
private io.undertow.servlet.api.ErrorPage getUndertowErrorPage(ErrorPage errorPage) { if (errorPage.getStatus() != null) { return new io.undertow.servlet.api.ErrorPage(errorPage.getPath(), errorPage.getStatusCode()); } if (errorPage.getException() != null) { return new io.undertow.servlet.api.ErrorPage(errorPage.getPath(), errorPage.getException()); } return new io.undertow.servlet.api.ErrorPage(errorPage.getPath()); }
@Test public void exceptionErrorWithCommittedResponse() throws Exception { this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500")); this.chain = new MockFilterChain() { @Override public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { super.doFilter(request, response); response.flushBuffer(); throw new RuntimeException("BAD"); } }; this.filter.doFilter(this.request, this.response, this.chain); assertThat(this.response.getForwardedUrl(), is(nullValue())); }
@Test public void errorPage404() throws Exception { AbstractEmbeddedServletContainerFactory factory = getFactory(); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/hello")); this.container = factory.getEmbeddedServletContainer( new ServletRegistrationBean(new ExampleServlet(), "/hello")); this.container.start(); assertThat(getResponse(getLocalUrl("/hello")), equalTo("Hello World")); assertThat(getResponse(getLocalUrl("/not-found")), equalTo("Hello World")); }
@Override public void customize(ConfigurableEmbeddedServletContainer container) { container.addErrorPages( new ErrorPage(HttpStatus.BAD_REQUEST, "/error/notfound"), new ErrorPage(HttpStatus.NOT_FOUND, "/error/notfound") ); }
@Bean public ServerProperties serverProperties() { return new ServerProperties() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { super.customize(container); container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html")); } }; }
@Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return container -> { container.addErrorPages(new ErrorPage(NotFoundException.class, "/404"), new ErrorPage("/error")); MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT); mappings.add("eot", "application/vnd.ms-fontobject"); mappings.add("otf", "application/x-font-opentype"); mappings.add("ttf", "application/x-font-truetype"); mappings.add("woff", "application/font-woff"); mappings.add("woff2", "application/font-woff2"); mappings.add("svg", "image/svg+xml"); container.setMimeMappings(mappings); }; }
@Override public void customize(ConfigurableEmbeddedServletContainer container) { // container.setContextPath("/format"); container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html")); }
@Override public void customize(ConfigurableEmbeddedServletContainer container) { container.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/403")); container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404")); }
@Override public void customize(ConfigurableEmbeddedServletContainer container) { super.customize(container); container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404.html")); container.addErrorPages(new ErrorPage("/error/error.html")); }
TomcatErrorPage(ErrorPage errorPage) { this.location = errorPage.getPath(); this.exceptionType = errorPage.getExceptionName(); this.errorCode = errorPage.getStatusCode(); this.nativePage = createNativePage(errorPage); }
private void configureErrorPages(DeploymentInfo servletBuilder) { for (ErrorPage errorPage : getErrorPages()) { servletBuilder.addErrorPage(getUndertowErrorPage(errorPage)); } }
@Override public void customize(ConfigurableEmbeddedServletContainer container) { container.addErrorPages(new ErrorPage(this.properties.getServletPrefix() + this.properties.getError().getPath())); }
@Override public void customize(ConfigurableEmbeddedServletContainer container) { container.addErrorPages(new ErrorPage("/spring/error")); }
@Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return container -> { container.setServerHeader("Harvester Application"); container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"),new ErrorPage(HttpStatus.FORBIDDEN, "/error/authentication")); }; }