在Spring引导版本2.0.0.M1

时间:2017-11-29 11:40:13

标签: java spring tomcat

我有春季启动版'2.0.0.M1'。我在我的应用程序上配置了https,一切都很好。但现在我尝试将应用程序从http重定向到https。我用户标准configuragion类

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;
    }
}

我有编译错误。在org.springframework.boot.context.embedded.EmbeddedServletContainerFactory中找不到EmbeddedServletContainerFactory和TomcatEmbeddedServletContainerFactory;

我的渐变依赖

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'

}

2 个答案:

答案 0 :(得分:2)

我使用的是新版本的Spring Boot,因此上面的配置没错。我通过这个配置类解决了我的问题:

@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;
}

}

答案 1 :(得分:0)

这样做的原因是,从Spring Boot版本2.0.0(至少到2.2.0)开始, org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory 正在替换 org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory 从1.0.0到至少1.5.x


请尝试以下代码或获得最高投票的答案中的代码:

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.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan
@Configuration
@EnableAutoConfiguration
class WebsocketSourceConfiguration {

      @Bean
      public ServletWebServerFactory servletContainer() {
        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);
          }
        };
        tomcat.addAdditionalTomcatConnectors(getHttpConnector());
        return tomcat;
      }

      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;
      }
}```