一个很老的项目需要在生产中使用https,记录下!
目标使用http访问网站自动跳转到https,代码中不强制使用https以便可以在开发测试环境方便使用。
关键字 java ,springmvc,shiro, tomcat , nginx .
1,项目部署图
升级后部署图
2,需要的改动:
- nginx :
在 80端口 的 server 中 增加
rewrite ^(.*)$
修改目的:使浏览器从http 跳转到https,使用301跳转可以让浏览器缓存,减少网络连接次数。
配置中增加
proxy_set_header X-Forwarded-Proto $scheme;
修改目的:shiro,Spring Security等使用 servlet Request#isSecure() 方法判断是否是 SSL 环境,增加这个配置可以使登录跳转到https。
- java 项目 spring 配置文件
< bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--新增加-->
<property name="redirectHttp10Compatible" value="false" />
</bean>
这里使用的是 jsp ,如果是其他视图解析器 也需要做相应配置。
修改目的使spring redirect 能跳转到https 而不是http。
- tomcat
增加配置
<Valve className="org. apache .catalina.valves.RemoteIpValve"
remoteIpHeader="x-forwarded-for"
proxiesHeader="x-forwarded-by"
protocolHeader="x-forwarded-proto" />
end!