Java Code Examples for org.apache.catalina.connector.Connector#setSecure()

The following examples show how to use org.apache.catalina.connector.Connector#setSecure() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MBeanFactory.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new Connector
 *
 * @param parent MBean Name of the associated parent component
 * @param address The IP address on which to bind
 * @param port TCP port number to listen on
 * @param isAjp Create a AJP/1.3 Connector
 * @param isSSL Create a secure Connector
 *
 * @exception Exception if an MBean cannot be created or registered
 */
private String createConnector(String parent, String address, int port, boolean isAjp, boolean isSSL)
    throws Exception {
    Connector retobj = new Connector();
    if ((address!=null) && (address.length()>0)) {
        retobj.setProperty("address", address);
    }
    // Set port number
    retobj.setPort(port);
    // Set the protocol
    retobj.setProtocol(isAjp ? "AJP/1.3" : "HTTP/1.1");
    // Set SSL
    retobj.setSecure(isSSL);
    retobj.setScheme(isSSL ? "https" : "http");
    // Add the new instance to its parent component
    // FIX ME - addConnector will fail
    ObjectName pname = new ObjectName(parent);
    Service service = getService(pname);
    service.addConnector(retobj);
    
    // Return the corresponding MBean name
    ObjectName coname = retobj.getObjectName();
    
    return (coname.toString());
}
 
Example 2
Source File: TomcatConfig.java    From singleton with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * create the https additional connection for tomcat
 */
private Connector initiateHttpsConnector(ServerProperties serverProperties) {
	Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
	connector.setScheme(ConstantsTomcat.HTTPS);
	connector.setPort(serverProperties.getServerPort());
	connector.setSecure(true);
	Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
	protocol.setSSLEnabled(true);
	protocol.setKeystoreFile(serverProperties.getHttpsKeyStore());
	protocol.setKeystorePass(serverProperties.getHttpsKeyStorePassword());
	protocol.setKeystoreType(serverProperties.getHttpsKeyStoreType());
	protocol.setKeyPass(serverProperties.getHttpsKeyPassword());
	protocol.setKeyAlias(serverProperties.getHttpsKeyAlias());
	protocol.setMaxHttpHeaderSize(serverProperties.getMaxHttpHeaderSize());
	connector.setRedirectPort(ConstantsTomcat.REDIRECT_PORT);
	connector.setAllowTrace(serverProperties.isAllowTrace());
	return connector;
}
 
Example 3
Source File: TomcatConfig.java    From enhanced-pet-clinic with Apache License 2.0 6 votes vote down vote up
private Connector createSslConnector() {
	Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
	Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
	try {
		connector.setScheme("https");
		connector.setSecure(true);
		connector.setPort(tlsPort);

		File keystore = getKeyStoreFile();
		File truststore = keystore;

		protocol.setSSLEnabled(true);
		protocol.setKeystoreFile(keystore.getAbsolutePath());
		protocol.setKeystorePass(sslKeystorePassword);
		protocol.setTruststoreFile(truststore.getAbsolutePath());
		protocol.setTruststorePass(sslKeystorePassword);
		protocol.setKeyAlias(sslKeystoreAlias);
		return connector;
	} catch (IOException ex) {
		throw new IllegalStateException(
				"can't access keystore: [" + "keystore" + "] or truststore: [" + "keystore" + "]", ex);
	}
}
 
Example 4
Source File: WebsphereTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void initIdp() {
    try {
        idpServer = new Tomcat();
        idpServer.setPort(0);
        String currentDir = new File(".").getCanonicalPath();
        idpServer.setBaseDir(currentDir + File.separator + "target");

        idpServer.getHost().setAppBase("tomcat/idp/webapps");
        idpServer.getHost().setAutoDeploy(true);
        idpServer.getHost().setDeployOnStartup(true);

        Connector httpsConnector = new Connector();
        httpsConnector.setPort(Integer.parseInt(idpHttpsPort));
        httpsConnector.setSecure(true);
        httpsConnector.setScheme("https");
        // httpsConnector.setAttribute("keyAlias", keyAlias);
        httpsConnector.setAttribute("keystorePass", "tompass");
        httpsConnector.setAttribute("keystoreFile", "test-classes/server.jks");
        httpsConnector.setAttribute("truststorePass", "tompass");
        httpsConnector.setAttribute("truststoreFile", "test-classes/server.jks");
        httpsConnector.setAttribute("clientAuth", "want");
        // httpsConnector.setAttribute("clientAuth", "false");
        httpsConnector.setAttribute("sslProtocol", "TLS");
        httpsConnector.setAttribute("SSLEnabled", true);

        idpServer.getService().addConnector(httpsConnector);

        idpServer.addWebapp("/fediz-idp-sts", "fediz-idp-sts");
        idpServer.addWebapp("/fediz-idp", "fediz-idp");

        idpServer.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: SslConfig.java    From spring-boot-cookbook with Apache License 2.0 5 votes vote down vote up
@Bean
public Connector httpConnector() {
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setScheme("http");
    //Connector监听的http的端口号
    connector.setPort(80);
    connector.setSecure(false);
    //监听到http的端口号后转向到的https的端口号
    connector.setRedirectPort(8443);
    return connector;
}
 
Example 6
Source File: IdpTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static Tomcat startServer(String port)
    throws ServletException, LifecycleException, IOException {
    Tomcat server = new Tomcat();
    server.setPort(0);
    String currentDir = new File(".").getCanonicalPath();
    String baseDir = currentDir + File.separator + "target";
    server.setBaseDir(baseDir);

    server.getHost().setAppBase("tomcat/idp/webapps");
    server.getHost().setAutoDeploy(true);
    server.getHost().setDeployOnStartup(true);

    Connector httpsConnector = new Connector();
    httpsConnector.setPort(Integer.parseInt(port));
    httpsConnector.setSecure(true);
    httpsConnector.setScheme("https");
    httpsConnector.setAttribute("keyAlias", "mytomidpkey");
    httpsConnector.setAttribute("keystorePass", "tompass");
    httpsConnector.setAttribute("keystoreFile", "test-classes/server.jks");
    httpsConnector.setAttribute("truststorePass", "tompass");
    httpsConnector.setAttribute("truststoreFile", "test-classes/server.jks");
    httpsConnector.setAttribute("clientAuth", "want");
    // httpsConnector.setAttribute("clientAuth", "false");
    httpsConnector.setAttribute("sslProtocol", "TLS");
    httpsConnector.setAttribute("SSLEnabled", true);

    server.getService().addConnector(httpsConnector);

    File stsWebapp = new File(baseDir + File.separator + server.getHost().getAppBase(), "fediz-idp-sts");
    server.addWebapp("/fediz-idp-sts", stsWebapp.getAbsolutePath());

    File idpWebapp = new File(baseDir + File.separator + server.getHost().getAppBase(), "fediz-idp");
    server.addWebapp("/fediz-idp", idpWebapp.getAbsolutePath());

    server.start();

    return server;
}
 
Example 7
Source File: HttpsConfiguration.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
public Connector createSslConnector() {

        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();

        connector.setPort(properties.getPort());
        connector.setScheme("https");
        connector.setSecure(true);
        protocol.setSSLEnabled(true);
        protocol.setClientAuth("false");
        protocol.setSSLProtocol("TLSv1+TLSv1.1+TLSv1.2");
        protocol.setKeystoreFile(properties.getKeystoreFile());
        protocol.setKeystorePass(properties.getKeystorePassword());
        return connector;
    }
 
Example 8
Source File: WebConfiguration.java    From Spring-Boot-2.0-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
public void configureConnector(Connector connector) {
    if (port != null)
        connector.setPort(port);
    if (secure != null)
        connector.setSecure(secure);
    if (scheme != null)
        connector.setScheme(scheme);
    if (ssl != null)
        connector.setProperty("SSLEnabled", ssl.toString());
    if (keystore != null && keystore.exists()) {
        connector.setProperty("keystoreFile", keystore.getAbsolutePath());
        connector.setProperty("keystorePassword", keystorePassword);
    }
}
 
Example 9
Source File: HttpsServerConfig.java    From micro-service with MIT License 5 votes vote down vote up
private Connector getHttpConnector() {
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setScheme("http");
    connector.setPort(8080);
    connector.setSecure(false);
    connector.setRedirectPort(8443);
    return connector;
}
 
Example 10
Source File: WebConfiguration.java    From Spring-Boot-2.0-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
public void configureConnector(Connector connector) {
    if (port != null)
        connector.setPort(port);
    if (secure != null)
        connector.setSecure(secure);
    if (scheme != null)
        connector.setScheme(scheme);
    if (ssl != null)
        connector.setProperty("SSLEnabled", ssl.toString());
    if (keystore != null && keystore.exists()) {
        connector.setProperty("keystoreFile", keystore.getAbsolutePath());
        connector.setProperty("keystorePassword", keystorePassword);
    }
}
 
Example 11
Source File: WebConfig.java    From jcart with MIT License 5 votes vote down vote up
private Connector initiateHttpConnector() {
	Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
	connector.setScheme("http");
	connector.setPort(9090);
	connector.setSecure(false);
	connector.setRedirectPort(serverPort);

	return connector;
}
 
Example 12
Source File: WebConfiguration.java    From Spring-Boot-2.0-Cookbook-Second-Edition with MIT License 5 votes vote down vote up
public void configureConnector(Connector connector) {
    if (port != null)
        connector.setPort(port);
    if (secure != null)
        connector.setSecure(secure);
    if (scheme != null)
        connector.setScheme(scheme);
    if (ssl != null)
        connector.setProperty("SSLEnabled", ssl.toString());
    if (keystore != null && keystore.exists()) {
        connector.setProperty("keystoreFile", keystore.getAbsolutePath());
        connector.setProperty("keystorePassword", keystorePassword);
    }
}
 
Example 13
Source File: JettyTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void initIdp() throws Exception {
    idpServer = new Tomcat();
    idpServer.setPort(0);
    final Path targetDir = Paths.get("target").toAbsolutePath();
    idpServer.setBaseDir(targetDir.toString());

    idpServer.getHost().setAppBase("tomcat/idp/webapps");
    idpServer.getHost().setAutoDeploy(true);
    idpServer.getHost().setDeployOnStartup(true);

    Connector httpsConnector = new Connector();
    httpsConnector.setPort(Integer.parseInt(IDP_HTTPS_PORT));
    httpsConnector.setSecure(true);
    httpsConnector.setScheme("https");
    httpsConnector.setAttribute("keyAlias", "mytomidpkey");
    httpsConnector.setAttribute("keystorePass", "tompass");
    httpsConnector.setAttribute("keystoreFile", "test-classes/server.jks");
    httpsConnector.setAttribute("truststorePass", "tompass");
    httpsConnector.setAttribute("truststoreFile", "test-classes/server.jks");
    httpsConnector.setAttribute("clientAuth", "want");
    // httpsConnector.setAttribute("clientAuth", "false");
    httpsConnector.setAttribute("sslProtocol", "TLS");
    httpsConnector.setAttribute("SSLEnabled", true);

    idpServer.getService().addConnector(httpsConnector);

    Path stsWebapp = targetDir.resolve(idpServer.getHost().getAppBase()).resolve("fediz-idp-sts");
    idpServer.addWebapp("/fediz-idp-sts", stsWebapp.toString());

    Path idpWebapp = targetDir.resolve(idpServer.getHost().getAppBase()).resolve("fediz-idp");
    idpServer.addWebapp("/fediz-idp", idpWebapp.toString());

    idpServer.start();
}
 
Example 14
Source File: TestCustomSsl.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomSslImplementation() throws Exception {

    TesterSupport.configureClientSsl();

    Tomcat tomcat = getTomcatInstance();
    Connector connector = tomcat.getConnector();

    Assume.assumeFalse("This test is only for JSSE based SSL connectors",
            connector.getProtocolHandlerClassName().contains("Apr"));

    connector.setProperty("sslImplementationName",
            "org.apache.tomcat.util.net.jsse.TesterBug50640SslImpl");
    connector.setProperty(TesterBug50640SslImpl.PROPERTY_NAME,
            TesterBug50640SslImpl.PROPERTY_VALUE);

    connector.setProperty("sslProtocol", "tls");

    File keystoreFile =
        new File("test/org/apache/tomcat/util/net/localhost.jks");
    connector.setAttribute(
            "keystoreFile", keystoreFile.getAbsolutePath());

    connector.setSecure(true);
    connector.setProperty("SSLEnabled", "true");

    File appDir = new File(getBuildDirectory(), "webapps/examples");
    tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());

    tomcat.start();
    ByteChunk res = getUrl("https://localhost:" + getPort() +
        "/examples/servlets/servlet/HelloWorldExample");
    assertTrue(res.toString().indexOf("<a href=\"../helloworld.html\">") > 0);
}
 
Example 15
Source File: ProxyTomcatConnectorCustomizer.java    From booties with Apache License 2.0 5 votes vote down vote up
@Override
public void customize(final Connector connector) {
    if (!proxyConnectorCustomizerProperties.isEnabled()) {
        logger.warn("CUSTOMIZE CONNECTORS IS DISABLED");
        return;
    }

    for (ConnectorCustomizer cc : proxyConnectorCustomizerProperties.getCustomizers()) {

        if (cc.isEnabled()) {

            if (connector.getPort() == cc.getPort()) {
                logger.warn("CUSTOMIZE CONNECTOR ON PORT : {}", connector.getPort());

                logger.warn("SET CONNECTOR - 'secure' : {}", cc.isSecure());
                connector.setSecure(cc.isSecure());

                logger.warn("SET CONNECTOR - 'scheme' : {}", cc.getScheme());
                connector.setScheme(cc.getScheme());

                logger.warn("SET CONNECTOR - 'proxy-port' : {}", cc.getProxyPort());
                connector.setProxyPort(cc.getProxyPort());

                logger.warn("SET CONNECTOR - 'proxy-name' : {}", cc.getProxyName());
                connector.setProxyName(cc.getProxyName());
            } else {
                logger.info("No customizer found for connector on port : {}", connector.getPort());
            }

        }
    }
}
 
Example 16
Source File: SystemConfiguration.java    From NFVO with Apache License 2.0 5 votes vote down vote up
private Connector initiateHttpConnector() {

    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");

    connector.setScheme("http");
    connector.setPort(8080);
    if (https) {
      connector.setSecure(false);
      connector.setRedirectPort(8443);
    }
    return connector;
  }
 
Example 17
Source File: IdpTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static Tomcat startServer(boolean idp, String port)
    throws ServletException, LifecycleException, IOException {
    Tomcat server = new Tomcat();
    server.setPort(0);
    String currentDir = new File(".").getCanonicalPath();
    String baseDir = currentDir + File.separator + "target";
    server.setBaseDir(baseDir);

    server.getHost().setAppBase("tomcat/idp/webapps");
    server.getHost().setAutoDeploy(true);
    server.getHost().setDeployOnStartup(true);

    Connector httpsConnector = new Connector();
    httpsConnector.setPort(Integer.parseInt(port));
    httpsConnector.setSecure(true);
    httpsConnector.setScheme("https");
    httpsConnector.setAttribute("keyAlias", "mytomidpkey");
    httpsConnector.setAttribute("keystorePass", "tompass");
    httpsConnector.setAttribute("keystoreFile", "test-classes/server.jks");
    httpsConnector.setAttribute("truststorePass", "tompass");
    httpsConnector.setAttribute("truststoreFile", "test-classes/server.jks");
    httpsConnector.setAttribute("clientAuth", "want");
    // httpsConnector.setAttribute("clientAuth", "false");
    httpsConnector.setAttribute("sslProtocol", "TLS");
    httpsConnector.setAttribute("SSLEnabled", true);

    server.getService().addConnector(httpsConnector);

    File stsWebapp = new File(baseDir + File.separator + server.getHost().getAppBase(), "fediz-idp-sts");
    server.addWebapp("/fediz-idp-sts", stsWebapp.getAbsolutePath());

    File idpWebapp = new File(baseDir + File.separator + server.getHost().getAppBase(), "fediz-idp");
    server.addWebapp("/fediz-idp", idpWebapp.getAbsolutePath());

    server.start();

    return server;
}
 
Example 18
Source File: WebConfig.java    From jcart with MIT License 5 votes vote down vote up
private Connector initiateHttpConnector()
{
	Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
	connector.setScheme("http");
	connector.setPort(8080);
	connector.setSecure(false);
	connector.setRedirectPort(serverPort);

	return connector;
}
 
Example 19
Source File: SpringTest.java    From cxf-fediz with Apache License 2.0 4 votes vote down vote up
private static Tomcat startServer(boolean idp, String port)
    throws ServletException, LifecycleException, IOException {
    Tomcat server = new Tomcat();
    server.setPort(0);
    String currentDir = new File(".").getCanonicalPath();
    String baseDir = currentDir + File.separator + "target";
    server.setBaseDir(baseDir);

    if (idp) {
        server.getHost().setAppBase("tomcat/idp/webapps");
    } else {
        server.getHost().setAppBase("tomcat/rp/webapps");
    }
    server.getHost().setAutoDeploy(true);
    server.getHost().setDeployOnStartup(true);

    Connector httpsConnector = new Connector();
    httpsConnector.setPort(Integer.parseInt(port));
    httpsConnector.setSecure(true);
    httpsConnector.setScheme("https");
    httpsConnector.setAttribute("keyAlias", "mytomidpkey");
    httpsConnector.setAttribute("keystorePass", "tompass");
    httpsConnector.setAttribute("keystoreFile", "test-classes/server.jks");
    httpsConnector.setAttribute("truststorePass", "tompass");
    httpsConnector.setAttribute("truststoreFile", "test-classes/server.jks");
    httpsConnector.setAttribute("clientAuth", "want");
    // httpsConnector.setAttribute("clientAuth", "false");
    httpsConnector.setAttribute("sslProtocol", "TLS");
    httpsConnector.setAttribute("SSLEnabled", true);

    server.getService().addConnector(httpsConnector);

    if (idp) {
        File stsWebapp = new File(baseDir + File.separator + server.getHost().getAppBase(), "fediz-idp-sts");
        server.addWebapp("/fediz-idp-sts", stsWebapp.getAbsolutePath());

        File idpWebapp = new File(baseDir + File.separator + server.getHost().getAppBase(), "fediz-idp");
        server.addWebapp("/fediz-idp", idpWebapp.getAbsolutePath());
    } else {
        File rpWebapp = new File(baseDir + File.separator + server.getHost().getAppBase(),
                                 "fediz-systests-webapps-spring");
        server.addWebapp("/fedizhelloworld", rpWebapp.getAbsolutePath());
        server.addWebapp("/fedizhelloworldspringnoreqvalidation", rpWebapp.getAbsolutePath());
    }

    server.start();

    return server;
}
 
Example 20
Source File: HttpServer.java    From guardedbox with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Bean: ServletWebServerFactory.
 * Creates a dual port Tomcat, listening both in an http port and an https port. The http port simply redirects to the https one.
 *
 * @return TomcatServletWebServerFactory.
 */
@Bean
public ServletWebServerFactory servletWebServerFactory() {

    // Check if there is dual port configuration.
    if (serverProperties.getInternalHttpPort() == null
            || serverProperties.getExternalHttpPort() == null
            || serverProperties.getInternalHttpsPort() == null
            || serverProperties.getExternalHttpsPort() == null
            || serverProperties.getPort().equals(serverProperties.getInternalHttpPort())
            || !serverProperties.getPort().equals(serverProperties.getInternalHttpsPort())) {
        return new TomcatServletWebServerFactory();
    }

    // Set TLS ECDH offered curves.
    if (!StringUtils.isEmpty(sslProperties.getEcdhCurves()))
        System.setProperty(JdkProperty.TLS_ECDH_CURVES.getPropertyName(), sslProperties.getEcdhCurves());

    // Enable TLS OCSP stapling.
    if (sslProperties.getEnableOcspStapling() != null)
        System.setProperty(JdkProperty.TLS_ENABLE_OCSP_STAPLING.getPropertyName(), sslProperties.getEnableOcspStapling().toString());

    // Create the https Tomcat.
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {

        @Override
        protected void postProcessContext(
                Context context) {

            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);

        }

    };

    // Customize the https connector.
    tomcat.addConnectorCustomizers(new TomcatConnectorCustomizer() {

        @Override
        public void customize(
                Connector connector) {

            SSLHostConfig sslHostConfig = connector.findSslHostConfigs()[0];
            sslHostConfig.setHonorCipherOrder(true);

        }

    });

    // Add the http connector with a redirection to the https port.
    Connector httpConnector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
    httpConnector.setScheme("http");
    httpConnector.setPort(serverProperties.getInternalHttpPort());
    httpConnector.setSecure(false);
    httpConnector.setRedirectPort(serverProperties.getExternalHttpsPort());
    tomcat.addAdditionalTomcatConnectors(httpConnector);

    return tomcat;

}