gpt4 book ai didi

java - EmbeddedServletContainerFactory TomcatEmbeddedServletContainerFactory在spring boot版本2.0.0.M1找不到

转载 作者:搜寻专家 更新时间:2023-11-01 03:16:45 25 4
gpt4 key购买 nike

我有 spring boot 版本“2.0.0.M1”。我在我的应用程序上配置了 https,一切都很好。但现在我尝试将应用程序从 http 重定向到 https。我使用标准配置类

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;

我的 gradle 依赖项

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'

最佳答案

我使用的是新版本的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;
}

关于java - EmbeddedServletContainerFactory TomcatEmbeddedServletContainerFactory在spring boot版本2.0.0.M1找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47551951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com