/** * 注册ServletRegistrationBean * @return */ @Bean public ServletRegistrationBean registrationBean() { ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); /** 初始化参数配置,initParams**/ //白名单 bean.addInitParameter("allow", "127.0.0.1"); //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page. bean.addInitParameter("deny", "192.168.1.73"); //登录查看信息的账号密码. bean.addInitParameter("loginUsername", "admin"); bean.addInitParameter("loginPassword", "admin"); //是否能够重置数据. bean.addInitParameter("resetEnable", "false"); return bean; }
/** * Druid 提供了一个 StatViewServlet 用于展示 Druid 的统计信息 * 这个 StatViewServlet 的用途包括: * 1. 提供监控信息展示的 HTML 页面 * 2. 提供监控信息的 JSON API */ @Bean public ServletRegistrationBean servletRegistrationBean(DruidDataSourceProperties druidDataSourceProperties) { log.debug("druid stat-view-servlet init..."); DruidStatViewServletProperties properties = druidDataSourceProperties.getStatViewServlet(); ServletRegistrationBean registration = new ServletRegistrationBean(); StatViewServlet statViewServlet = new StatViewServlet(); registration.setServlet(statViewServlet); registration.addUrlMappings(properties.getUrlMappings()); if (!StringUtils.isEmpty(properties.getLoginUsername())) { registration.addInitParameter("loginUsername", properties.getLoginUsername()); } if (!StringUtils.isEmpty(properties.getLoginPassword())) { registration.addInitParameter("loginPassword", properties.getLoginPassword()); } if (!StringUtils.isEmpty(properties.getAllow())) { registration.addInitParameter("allow", properties.getAllow()); } if (!StringUtils.isEmpty(properties.getDeny())) { registration.addInitParameter("deny", properties.getDeny()); } registration.addInitParameter("resetEnable", Boolean.toString(properties.isResetEnable())); return registration; }
@Bean @ConfigurationProperties(DruidServletProperties.DRUID_SERVLET_PREFIX) public ServletRegistrationBean druidServlet(DruidServletProperties properties) { ServletRegistrationBean reg = new ServletRegistrationBean(); reg.setServlet(new StatViewServlet()); reg.addUrlMappings(properties.getUrlMappings()); if(properties.getAllow() !=null){ reg.addInitParameter("allow", properties.getAllow()); // IP白名单 (没有配置或者为空,则允许所有访问) } if(properties.getDeny() !=null){ reg.addInitParameter("deny", properties.getDeny()); //IP黑名单 (存在共同时,deny优先于allow) } if(properties.getLoginUsername() !=null){ reg.addInitParameter("loginUsername", properties.getLoginUsername()); //用户名 } if(properties.getLoginPassword() !=null){ reg.addInitParameter("loginPassword", properties.getLoginPassword()); // 密码 } if(properties.getResetEnable() !=null){ reg.addInitParameter("resetEnable", properties.getResetEnable().toString());// 禁用HTML页面上的“Reset All”功能 } return reg; }
/** * 数据源监控注册一个StatViewServlet * * @return */ @Bean public ServletRegistrationBean druidStatViewServlet(){ //注册监控地址 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //白名单 servletRegistrationBean.addInitParameter("allow","127.0.0.1"); //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page. servletRegistrationBean.addInitParameter("deny","192.168.1.73"); //登录查看信息的账号密码. servletRegistrationBean.addInitParameter("loginUsername","admin"); servletRegistrationBean.addInitParameter("loginPassword","123456"); //是否能够重置数据. servletRegistrationBean.addInitParameter("resetEnable","false"); return servletRegistrationBean; }
@Bean public ServletRegistrationBean jerseyServletRegistration( JerseyProperties jerseyProperties, ResourceConfig config) { ServletRegistrationBean registration = new ServletRegistrationBean( new ServletContainer(config)); for (Map.Entry<String, String> entry : jerseyProperties.getInit().entrySet()) { registration.addInitParameter(entry.getKey(), entry.getValue()); } registration.addUrlMappings("/" + (StringUtils.isEmpty(lyreProperties.getApplicationPath()) ? "api" : lyreProperties.getApplicationPath()) + "/*"); registration.setName(APIx.class.getName()); registration.setLoadOnStartup(1); return registration; }
/** * 注册ServletRegistrationBean * @return */ @Bean public ServletRegistrationBean registrationBean() { ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); /** 初始化参数配置,initParams**/ //白名单 bean.addInitParameter("allow", "127.0.0.1"); //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page. bean.addInitParameter("deny", "192.168.1.73"); //登录查看信息的账号密码. bean.addInitParameter("loginUsername", "admin2"); bean.addInitParameter("loginPassword", "123"); //是否能够重置数据. bean.addInitParameter("resetEnable", "false"); return bean; }
/** * Configures a custom jetty http proxy servlet based on <b>oneops.proxy.enabled</b> * config property. The proxy configuration is done on the <b>application.yaml</b> file. * * @param config OneOps config * @return {@link ServletRegistrationBean} */ @Bean @ConditionalOnProperty("oneops.proxy.enabled") public ServletRegistrationBean registerProxyServlet(OneOpsConfig config) { log.info("OneOps Http Proxy is enabled."); OneOpsConfig.Proxy proxyCfg = config.getProxy(); Map<String, String> initParams = new HashMap<>(); initParams.put(proxyTo.name(), proxyCfg.getProxyTo()); initParams.put(prefix.name(), proxyCfg.getPrefix()); initParams.put(viaHost.name(), proxyCfg.getViaHost()); initParams.put(trustAll.name(), String.valueOf(proxyCfg.isTrustAll())); initParams.put(xAuthHeader.name(), config.getAuth().getHeader()); ServletRegistrationBean servletBean = new ServletRegistrationBean(new ProxyServlet(), proxyCfg.getPrefix() + "/*"); servletBean.setName("OneOps Proxy Servlet"); servletBean.setInitParameters(initParams); servletBean.setAsyncSupported(true); log.info("Configured OneOps proxy servlet with mapping: " + proxyCfg.getPrefix()); return servletBean; }
@Bean public ServletRegistrationBean messageDispatcherServlet( ApplicationContext applicationContext) { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(applicationContext); String path = this.properties.getPath(); String urlMapping = (path.endsWith("/") ? path + "*" : path + "/*"); ServletRegistrationBean registration = new ServletRegistrationBean(servlet, urlMapping); WebServicesProperties.Servlet servletProperties = this.properties.getServlet(); registration.setLoadOnStartup(servletProperties.getLoadOnStartup()); for (Map.Entry<String, String> entry : servletProperties.getInit().entrySet()) { registration.addInitParameter(entry.getKey(), entry.getValue()); } return registration; }
@Test public void sslDisabled() throws Exception { AbstractEmbeddedServletContainerFactory factory = getFactory(); Ssl ssl = getSsl(null, "password", "classpath:test.jks"); ssl.setEnabled(false); factory.setSsl(ssl); this.container = factory.getEmbeddedServletContainer( new ServletRegistrationBean(new ExampleServlet(true, false), "/hello")); this.container.start(); SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()); HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) .build(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); this.thrown.expect(SSLException.class); getResponse(getLocalUrl("https", "/hello"), requestFactory); }
@Test public void sslGetScheme() throws Exception { // gh-2232 AbstractEmbeddedServletContainerFactory factory = getFactory(); factory.setSsl(getSsl(null, "password", "src/test/resources/test.jks")); this.container = factory.getEmbeddedServletContainer( new ServletRegistrationBean(new ExampleServlet(true, false), "/hello")); this.container.start(); SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()); HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) .build(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); assertThat(getResponse(getLocalUrl("https", "/hello"), requestFactory)) .contains("scheme=https"); }
@Test public void compressionWithoutContentSizeHeader() throws Exception { AbstractEmbeddedServletContainerFactory factory = getFactory(); Compression compression = new Compression(); compression.setEnabled(true); factory.setCompression(compression); this.container = factory.getEmbeddedServletContainer( new ServletRegistrationBean(new ExampleServlet(false, true), "/hello")); this.container.start(); TestGzipInputStreamFactory inputStreamFactory = new TestGzipInputStreamFactory(); Map<String, InputStreamFactory> contentDecoderMap = Collections .singletonMap("gzip", (InputStreamFactory) inputStreamFactory); getResponse(getLocalUrl("/hello"), new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create() .setContentDecoderRegistry(contentDecoderMap).build())); assertThat(inputStreamFactory.wasCompressionUsed()).isTrue(); }
protected final ServletContextInitializer sessionServletRegistration() { ServletRegistrationBean bean = new ServletRegistrationBean(new ExampleServlet() { @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { HttpSession session = ((HttpServletRequest) request).getSession(true); long value = System.currentTimeMillis(); Object existing = session.getAttribute("boot"); session.setAttribute("boot", value); PrintWriter writer = response.getWriter(); writer.append(String.valueOf(existing) + ":" + value); } }, "/session"); bean.setName("session"); return bean; }
@Test public void accessLogCanBeEnabled() throws IOException, URISyntaxException, InterruptedException { UndertowEmbeddedServletContainerFactory factory = getFactory(); factory.setAccessLogEnabled(true); File accessLogDirectory = this.temporaryFolder.getRoot(); factory.setAccessLogDirectory(accessLogDirectory); assertThat(accessLogDirectory.listFiles()).isEmpty(); this.container = factory.getEmbeddedServletContainer( new ServletRegistrationBean(new ExampleServlet(), "/hello")); this.container.start(); assertThat(getResponse(getLocalUrl("/hello"))).isEqualTo("Hello World"); File accessLog = new File(accessLogDirectory, "access_log.log"); awaitFile(accessLog); assertThat(accessLogDirectory.listFiles()).contains(accessLog); }
@Bean ServletRegistrationBean keycloakJaxRsApplication(KeycloakServerProperties keycloakServerProperties, DataSource dataSource) throws Exception { mockJndiEnvironment(dataSource); //FIXME: hack to propagate Spring Boot Properties to Keycloak Application EmbeddedKeycloakApplication.keycloakServerProperties = keycloakServerProperties; ServletRegistrationBean servlet = new ServletRegistrationBean(new HttpServlet30Dispatcher()); servlet.addInitParameter("javax.ws.rs.Application", EmbeddedKeycloakApplication.class.getName()); servlet.addInitParameter(ResteasyContextParameters.RESTEASY_SERVLET_MAPPING_PREFIX, keycloakServerProperties.getContextPath()); servlet.addInitParameter(ResteasyContextParameters.RESTEASY_USE_CONTAINER_FORM_PARAMS, "true"); servlet.addUrlMappings(keycloakServerProperties.getContextPath() + "/*"); servlet.setLoadOnStartup(1); servlet.setAsyncSupported(true); return servlet; }
protected void testRestrictedSSLProtocolsAndCipherSuites(String[] protocols, String[] ciphers) throws Exception { AbstractEmbeddedServletContainerFactory factory = getFactory(); factory.setSsl(getSsl(null, "password", "src/test/resources/test.jks", null, protocols, ciphers)); this.container = factory.getEmbeddedServletContainer( new ServletRegistrationBean(new ExampleServlet(true, false), "/hello")); this.container.start(); SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()); HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) .build(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); assertThat(getResponse(getLocalUrl("https", "/hello"), requestFactory)) .contains("scheme=https"); }
@Test public void servletContextInitializerBeansSkipsRegisteredServletsAndFilters() throws Exception { addEmbeddedServletContainerFactoryBean(); Servlet servlet = mock(Servlet.class); Filter filter = mock(Filter.class); ServletRegistrationBean initializer = new ServletRegistrationBean(servlet, "/foo"); this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer)); this.context.registerBeanDefinition("servletBean", beanDefinition(servlet)); this.context.registerBeanDefinition("filterBean", beanDefinition(filter)); this.context.refresh(); ServletContext servletContext = getEmbeddedServletContainerFactory() .getServletContext(); verify(servletContext, atMost(1)).addServlet(anyString(), (Servlet) anyObject()); verify(servletContext, atMost(1)).addFilter(anyString(), (Filter) anyObject()); }
@Bean public ServletRegistrationBean statViewServlet(){ //创建servlet注册实体 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //设置ip白名单 // servletRegistrationBean.addInitParameter("allow","127.0.0.1"); //设置ip黑名单,如果allow与deny共同存在时,deny优先于allow // servletRegistrationBean.addInitParameter("deny","192.168.0.19"); //设置控制台管理用户 servletRegistrationBean.addInitParameter("loginUsername","lpj"); servletRegistrationBean.addInitParameter("loginPassword","123"); //是否可以重置数据 servletRegistrationBean.addInitParameter("resetEnable","false"); return servletRegistrationBean; }
@Bean ServletRegistrationBean camelServlet() { // TODO: Camel 2.19 should support this OOTB // use a @Bean to register the Camel servlet which we need to do // because we want to use the camel-servlet component for the Camel REST service ServletRegistrationBean mapping = new ServletRegistrationBean(); mapping.setName("CamelServlet"); mapping.setLoadOnStartup(1); // CamelHttpTransportServlet is the name of the Camel servlet to use mapping.setServlet(new CamelHttpTransportServlet()); mapping.addUrlMappings("/api/*"); return mapping; }
public ServletRegistrationBean routeServlet1(RouterFunction<?> routerFunction) throws Exception { HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction ); ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(servlet, "/flux" + "/*"); registrationBean.setLoadOnStartup(1); registrationBean.setAsyncSupported(true); System.out.println("starts server"); return registrationBean; }
@Bean(name = "hystrixRegistrationBean") public ServletRegistrationBean servletRegistrationBean() { ServletRegistrationBean registration = new ServletRegistrationBean( new HystrixMetricsStreamServlet(), "/hystrix.stream"); registration.setName("hystrixServlet"); registration.setLoadOnStartup(1); return registration; }
@Bean public ServletRegistrationBean cxfServlet() { final ServletRegistrationBean bean = new ServletRegistrationBean(); bean.setEnabled(true); bean.setName("cxfServletSecurityTokenService"); bean.setServlet(new CXFServlet()); bean.setUrlMappings(Collections.singleton(WSFederationConstants.ENDPOINT_STS.concat("*"))); bean.setAsyncSupported(true); return bean; }
@Bean public ServletRegistrationBean druidServlet() { ServletRegistrationBean reg = new ServletRegistrationBean(); reg.setServlet(new StatViewServlet()); reg.addUrlMappings("/druid/*"); reg.addInitParameter("allow", "127.0.0.1");//允许访问地址,不填则全允许,填了只允许 // reg.addInitParameter("deny", "127.0.0.1"); reg.addInitParameter("loginUsername", dbConfigProperties.getDruidVisitName()); reg.addInitParameter("loginPassword", dbConfigProperties.getDruidVisitPwd()); return reg; }
@Bean public ServletRegistrationBean statViewServlet(){ //创建servlet注册实体 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //设置ip白名单 servletRegistrationBean.addInitParameter("allow","127.0.0.1"); //设置ip黑名单,如果allow与deny共同存在时,deny优先于allow servletRegistrationBean.addInitParameter("deny","192.168.0.19"); //设置控制台管理用户 servletRegistrationBean.addInitParameter("loginUsername","druid"); servletRegistrationBean.addInitParameter("loginPassword","123456"); //是否可以重置数据 servletRegistrationBean.addInitParameter("resetEnable","false"); return servletRegistrationBean; }
@Bean public ServletRegistrationBean apiServletBean(WebApplicationContext webApplicationContext) { DispatcherServlet servlet = new DispatcherServlet(webApplicationContext); ServletRegistrationBean bean = new ServletRegistrationBean(servlet, "/api/*"); bean.setName("ApiServlet"); return bean; }