Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EmbeddedServletContainerFactory TomcatEmbeddedServletContainerFactory can't be found in spring boot version 2.0.0.M1

I have spring boot version '2.0.0.M1'. I configured https on my application and everything is good. But now I try to redirect application from http to https. I user standart configuragion class

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class ConfRedir {

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat =
                new TomcatEmbeddedServletContainerFactory() {

                    @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);
                    }
                };
        tomcat.addAdditionalTomcatConnectors(createHttpConnector());
        return tomcat;
    }

    private Connector createHttpConnector() {
        Connector connector =
                new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setSecure(false);
        connector.setPort(8080);
        connector.setRedirectPort(8443);
        return connector;
    }
}

I have compilation error. EmbeddedServletContainerFactory and TomcatEmbeddedServletContainerFactory can't be found in org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;

My graddle dependencies

dependencies {
compile("org.springframework.boot:spring-boot-starter-actuator")
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.ldap:spring-ldap-core")
compile "org.springframework.security:spring-security-core"
compile("org.springframework.security:spring-security-ldap")
compile("org.springframework:spring-tx")
compile group: 'commons-io', name: 'commons-io', version: '2.5'
compile group: 'com.jcraft', name: 'jsch', version: '0.1.54'
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.0.0.M3'

compile 'org.springframework.security.kerberos:spring-security-kerberos-web:1.0.0.RELEASE'
compile 'org.springframework.security.kerberos:spring-security-kerberos-client:1.0.0.RELEASE'
compile 'org.springframework.security.kerberos:spring-security-kerberos-core:1.0.0.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-crypto', version: '3.1.0.RELEASE'
compile("org.springframework.boot:spring-boot-devtools")
compile('org.postgresql:postgresql:9.4-1206-jdbc4')
compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity4', version: '3.0.2.RELEASE'
compileOnly('org.projectlombok:lombok:1.16.18')
compile group: 'org.modelmapper', name: 'modelmapper', version: '1.1.0'

}

like image 660
Den B Avatar asked Oct 18 '22 02:10

Den B


1 Answers

I used new version of Spring Boot so configuration above was little wrong. I solved my issue by this configuration class:

@Configuration
public class HttpsRedirectConf {
private final static String SECURITY_USER_CONSTRAINT = "CONFIDENTIAL";
private final static String REDIRECT_PATTERN = "/*";
private final static String CONNECTOR_PROTOCOL = "org.apache.coyote.http11.Http11NioProtocol";
private final static String CONNECTOR_SCHEME = "http";



@Bean
public TomcatServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat =
            new TomcatServletWebServerFactory() {

                @Override
                protected void postProcessContext(Context context) {
                    SecurityConstraint securityConstraint = new SecurityConstraint();
                    securityConstraint.setUserConstraint(SECURITY_USER_CONSTRAINT);
                    SecurityCollection collection = new SecurityCollection();
                    collection.addPattern(REDIRECT_PATTERN);
                    securityConstraint.addCollection(collection);
                    context.addConstraint(securityConstraint);
                }
            };
    tomcat.addAdditionalTomcatConnectors(createHttpConnector());
    return tomcat;
}

private Connector createHttpConnector() {
    Connector connector =
            new Connector(CONNECTOR_PROTOCOL);
    connector.setScheme(CONNECTOR_SCHEME);
    connector.setSecure(false);
    connector.setPort("${port:80}");
    connector.setRedirectPort("${port:443}");
    return connector;
}

}

like image 181
Den B Avatar answered Nov 13 '22 19:11

Den B