相关疑难解决方法(0)

Spring Boot将HTTP重定向到HTTPS

对于基于Spring Boot的应用程序,我在application.properties上配置了ssl属性,请参阅我的配置:

server.port=8443
server.ssl.key-alias=tomcat
server.ssl.key-password=123456
server.ssl.key-store=classpath:key.p12
server.ssl.key-store-provider=SunJSSE
server.ssl.key-store-type=pkcs12
Run Code Online (Sandbox Code Playgroud)

我在Application.class上添加了连接,就像

@Bean
public EmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
    final TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
    factory.addAdditionalTomcatConnectors(this.createConnection());
    return factory;
}

private Connector createConnection() {
    final String protocol = "org.apache.coyote.http11.Http11NioProtocol";
    final Connector connector = new Connector(protocol);

    connector.setScheme("http");
    connector.setPort(9090);
    connector.setRedirectPort(8443);
    return connector;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试以下时

http://127.0.0.1:9090/
Run Code Online (Sandbox Code Playgroud)

重定向到

https://127.0.0.1:8443/
Run Code Online (Sandbox Code Playgroud)

没有执行.谁遇到过类似的问题?

java https redirect http spring-boot

33
推荐指数
7
解决办法
5万
查看次数

Spring Boot:如何将另一个WAR文件添加到嵌入式tomcat?

Spring Boot的嵌入式tomcat非常方便,适用于开发和部署.

但是如果应该添加另一个(第三方)WAR文件(例如,GeoServer)呢?

也许以下是正常程序:

  1. 安装普通的Tomcat服务器.
  2. 将Spring Boot应用程序构建为WAR文件,并将其添加到Tomcat的webapps文件夹中.
  3. 还要将另一个(第三方)WAR文件添加到webapps文件夹.

但如果可以进行以下配置,那就太好了.

  1. 将Spring启动应用程序构建为独立的Jar,其中包括嵌入式Tomcat.
  2. 部署Spring启动应用程序Jar.
  3. 将另一个(第三方)WAR文件添加到嵌入式Tomcat识别的文件夹中.
  4. 使用嵌入式Tomcat提供Spring引导应用程序内容和另一个WAR的内容.

怎么做到呢?

UPDATE

当spring引导应用程序由胖jar(=可执行jar)构成时,答案中的代码是不够的.修订后的内容如下:

@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
    return new TomcatEmbeddedServletContainerFactory() {

        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            try {
                Context context = tomcat.addWebapp("/foo", "/path/to/foo.war");
                WebappLoader loader =
                    new WebappLoader(Thread.currentThread().getContextClassLoader());
                context.setLoader(loader);
            } catch (ServletException ex) {
                throw new IllegalStateException("Failed to add webapp", ex);
            }
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }

    };
}
Run Code Online (Sandbox Code Playgroud)

由于系统类加载器无法加载胖jar中的jar文件,因此必须指定显式父类加载器.否则,附加WAR无法将库jar加载到添加了WAR的spring boot应用程序的fat jar中.

spring tomcat spring-boot

28
推荐指数
2
解决办法
1万
查看次数

由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext

当我使用主应用程序运行应用程序时,我在consoleUnable中得到错误以启动Web服务器; 嵌套异常是org.springframework.context.ApplicationContextException:由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext.

主要应用

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

Servlet初始化程序

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

的build.gradle

    buildscript {
        ext {
            springBootVersion = '2.0.0.M4'
        }
        repositories {
            jcenter()
            mavenCentral()
            maven { url "https://repo.spring.io/snapshot" }
            maven { url "https://repo.spring.io/milestone" }
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }

    plugins {
        id "org.sonarqube" version "2.5"
    }

    apply plugin: 'java'
    apply plugin: 'idea'
    apply plugin: …
Run Code Online (Sandbox Code Playgroud)

java tomcat intellij-idea spring-boot

11
推荐指数
1
解决办法
3万
查看次数

ugrade spring boot 2.0.0.RC2 exception无ServletContext设置

经过调试,问题是mvc配置类EnableWebMvcConfiguration加载太早,servlet尚未加载.

org.springframework.beans.factory.BeanCreationException:在类路径资源中定义名为'resourceHandlerMapping'的bean时出错[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration $ EnableWebMvcConfiguration.class]:通过工厂方法进行的Bean实例化失败; 嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.web.servlet.HandlerMapping]:工厂方法'resourceHandlerMapping'抛出异常; 嵌套异常是java.lang.IllegalStateException:没有在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587)设置的ServletContext~ [spring-beans-5.0.4.RELEASE.jar:5.0.4 .RELEASE] org.springframework.beans上的org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1250)~ [spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] .factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(在Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)〜[spring]的AbstractAutowireCapableBeanFactory.java:545)~ [spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] -beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]在org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:312)〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory $$ Lambda $ 125/98506158.getObject(Unknown Source)〜[na:na] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228 )〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)〜[spring-beans-5.0. 4.RELEASE.jar:5.0.4.RELEASE]在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)〜[spring-beans-5.0.4.RELEASE.jar:5.0.4 .rELEASE] org.springframework.context上的org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)~ [spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] .support.Abs org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)中的tractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)〜[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]〜 [spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)〜[spring-boot-2.0. 0.RC2.jar:2.0.0.RC2] org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752)[spring-boot-2.0.0.RC2.jar:2.0.0.RC2] at org .springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388)[spring-boot-2.0.0.RC2.jar:2.0.0.RC2] org.springframework.boot.SpringApplication.run(SpringApplication.java:327 )[spring-boot-2.0.0.RC2.jar:2.0.0.RC2] org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)[spring-boot-2.0.0.RC2.jar: 2.0.0.RC2]在org.springframework.boot.SpringApplica 在com.manway.BccApplication.main(BccApplication.java:16)[main /:na]的.prun(SpringApplication.java:1234)[spring-boot-2.0.0.RC2.jar:2.0.0.RC2]在sun.reflect.DelegatingMethodAccessorImpl的sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8.0_40] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_40].在org.springframework.boot.devtools的java.lang.reflect.Method.invoke(Method.java:497)〜[na:1.8.0_40]中调用(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_40]. restart.RestartLauncher.run(RestartLauncher.java:49)[spring-boot-devtools-2.0.0.RC2.jar:2.0.0.RC2]引起:org.springframework.beans.BeanInstantiationException:无法实例化[org. springframework.web.servlet.HandlerMapping]:工厂方法'resourceHandlerMapping'抛出异常; 嵌套异常是java.lang.IllegalStateException:没有在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)〜[spring-beans-5.0.4.RELEASE.jar:5.0.4中设置的ServletContext .RELEASE]在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579)〜[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE] ... 24个常见框架省略:java.lang.IllegalStateException:在org.springframework.util.Assert.state(Assert.java:73)中没有设置ServletContext~ [spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE] org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:485)〜[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE] org.springframework.boot. autoconfigure.web.servlet.WebMvcAutoConfiguration $ EnableWebMvcConfiguration $$ EnhancerBySpringCGLIB $$ 5de64506.CGLIB $ resourceHandlerMapping $ 40()〜[spring-boot-autoconf 在org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration $ EnableNetMvcConfiguration $$ EnhancerBySpringCGLIB $$ 5de64506 $$ FastClassBySpringCGLIB $$ …

spring-boot

6
推荐指数
2
解决办法
5892
查看次数

Spring Boot 2 - 未找到 EmbeddedServletContainerFactory

我的 Spring Boot 2 Web 应用程序运行良好,但现在我想将所有 http 请求重定向到 https。

但是 EmbeddedServletContainerFactory 和 TomcatEmbeddedServletContainerFactory 不能被导入。它说org.springframework.boot.context.embedded不存在。

我的代码如下:

@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(redirectConnector());
    return tomcat;
}
Run Code Online (Sandbox Code Playgroud)

我使用gradle,构建文件是:

buildscript {
    ext {
        springBootVersion = '2.0.0.M6'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
} …
Run Code Online (Sandbox Code Playgroud)

java spring tomcat spring-boot spring-web

5
推荐指数
0
解决办法
1万
查看次数

标签 统计

spring-boot ×5

java ×3

tomcat ×3

spring ×2

http ×1

https ×1

intellij-idea ×1

redirect ×1

spring-web ×1