spring_reference/IX. ‘How-to’ guides/71.3. Enable HTTPS when run...

13 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 71.3. 当前端使用代理服务器时启用HTTPS
对于任何应用来说确保所有的主端点URL都只在HTTPS下可用是个重要的苦差事。如果你使用Tomcat作为servlet容器那Spring Boot如果发现一些环境设置的话它将自动添加Tomcat自己的RemoteIpValve你也可以依赖于HttpServletRequest来报告是否请求是安全的即使代理服务器的downstream处理真实的SSL终端。这个标准行为取决于某些请求头是否出现`x-forwarded-for`和`x-forwarded-proto`),这些请求头的名称都是约定好的,所以对于大多数前端和代理都是有效的。
你可以向application.properties添加以下设置里开启该功能比如
```yml
server.tomcat.remote_ip_header=x-forwarded-for
server.tomcat.protocol_header=x-forwarded-proto
```
这些属性出现一个就会开启该功能或者你可以通过添加一个TomcatEmbeddedServletContainerFactory bean自己添加RemoteIpValve
Spring Security也可以配置成针对所以或某些请求需要一个安全渠道channel。想要在一个Spring Boot应用中开启它你只需将application.properties中的`security.require_ssl`设置为`true`即可。