> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/ix.-how-to-guides/84.-security/84.3-enable-https-when-running-behind-a-proxy-server.md).

# 84.3 当前端使用代理服务器时启用HTTPS

对于任何应用来说，确保所有的主端点（URL）都只在HTTPS下可用是个重要的苦差事。如果你使用Tomcat作为servlet容器，那Spring Boot如果发现一些环境设置的话，它将自动添加Tomcat自己的`RemoteIpValve`，你也可以依赖于`HttpServletRequest`来报告是否请求是安全的（即使代理服务器的downstream处理真实的SSL终端）。这个标准行为取决于某些请求头是否出现（`x-forwarded-for`和`x-forwarded-proto`），这些请求头的名称都是约定好的，所以对于大多数前端和代理都是有效的。你可以向`application.properties`添加以下设置开启该功能，比如：

```
server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.protocol-header=x-forwarded-proto
```

（这些属性出现一个就会开启该功能，或者你可以通过添加一个`TomcatServletWebServerFactory` bean自己添加`RemoteIpValve`）。

要将Spring Security配置为所有(或部分)请求都需要一个安全通道，请考虑添加你自己的`WebSecurityConfigurerAdapter`，它将添加以下`HttpSecurity`配置：

```
@Configuration
public class SslWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

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

}
```
