繁体   English   中英

Spring Boot HTTPS和重定向

[英]Spring Boot HTTPS and redirect

我在带有Pivotal 3.1服务器(端口8080和8443)的Spring STS上使用,我在盒子上还有一个单独的tomcat 7实例,该实例可以在80和443上运行。

我使用Spring Boot 1.2.4发行版。

我希望应用程序将所有请求自动重定向到https-我没有使用嵌入式tomcat实例。

以前使用spring时,我在web.xml中添加了标签,并且工作正常。

请问如何使用Spring Boot达到同样的效果?

谢谢阿德里安

如果您使用的是Spring Security,则可以通过在Spring Boot参考中提到的将application.properties添加security.require_ssl=true来实现。 如果您自定义Spring Security配置,那么您将需要具有以下内容:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // ...
            .requiresChannel()
                .anyRequest().requiresSecure();
    }
}

由于您没有使用Spring Security并且使用的是war文件,所以最简单的方法是创建一个包含以下内容的web.xml:

src / main / webapp / WEB-INF / web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee                       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>all</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
</web-app>

使用web.xml是必需的,因为无法以编程方式设置整个应用程序的安全性约束。 您可以在如何在Servlets 3.x中以编程方式设置<security-constraint>中找到有关此内容的详细信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM