Springboot项目使用Nginx 并配置 HTTPS
2017-10-23 20:57
906 查看
Springboot的配置
参考博客1参考博客2
签发证书:
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
gradle或者Maven项目:直接将生成的keystore.p12放在resources下
application.yml
server: # context-path: /myth ssl: key-store: classpath:keystore.p12 key-store-password: demo1429336 key-store-type: PKCS12 key-alias: tomcat port: 8888 session: timeout: 3000
任意的一个@Configuration注解类里添加
@Bean public TomcatEmbeddedServletContainerFactory servletContainerFactory() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory() { @Override protected void postProcessContext(Context context) { //SecurityConstraint必须存在,可以通过其为不同的URL设置不同的重定向策略。 SecurityConstraint securityConstraint = new SecurityConstraint(); securityConstraint.setUserConstraint("CONFIDENTIAL"); SecurityCollection collection = new SecurityCollection(); collection.addPattern("/*"); securityConstraint.addCollection(collection); context.addConstraint(securityConstraint); } }; factory.addAdditionalTomcatConnectors(createHttpConnector()); return factory; } // HTTP自动转发到HTTPS private Connector createHttpConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setSecure(false); connector.setPort(8887);//http端口(这是要新增加的一个端口) connector.setRedirectPort(8888);// https 端口配置文件中tomcat启动的默认端口 return connector; }
Nginx的配置
先签发证书############ 证书颁发机构 # CA机构私钥 openssl genrsa -out ca.key 2048 # CA证书 openssl req -x509 -new -key ca.key -out ca.crt ############ 服务端 # 生成服务端私钥 openssl genrsa -out server.key 2048 # 生成服务端证书请求文件 openssl req -new -key server.key -out server.csr # 使用CA证书生成服务端证书 关于sha256,默认使用的是sha1,在新版本的chrome中会被认为是不安全的,因为使用了过时的加密算法。 openssl x509 -req -sha256 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 3650 -out server.crt # 打包服务端的资料为pkcs12格式(非必要,只是换一种格式存储上一步生成的证书) 生成过程中,需要创建访问密码,请记录下来。 openssl pkcs12 -export -in server.crt -inkey server.key -out server.pkcs12
新建任意名的配置文件在/etc/nginx/conf.d/下
upstream test { server 127.0.0.1:8888; } server { listen 443; server_name wx.baidu.com; ssl on; ssl_certificate /home/er/https/server.crt; ssl_certificate_key /home/er/https/server.key; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Nginx-Proxt true; proxy_pass https://test; proxy_redirect off; } }
重启nginx即可:
sudo nginx -s reload
相关文章推荐
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- Springboot配置使用ssl,使用https
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- maven的Springboot项目的携程Apollo配置中心的配置以及使用
- Spring boot框架项目,使用maven命令将配置文件打包到jar包外,项目运行读取jar外配置文件
- SpringBoot系列三:SpringBoot基本概念(统一父 pom 管理、SpringBoot 代码测试、启动注解分析、配置访问路径、使用内置对象、项目打包发布)
- Https背景与证书在spring boot项目中的使用
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- springboot项目 打成war包 使用自定义tomcat配置
- 使用IDEA从零配置springboot项目环境
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- springboot 项目使用freemarker 配置之后,访问静态和动态页面的方法
- 使用IDEA创建springboot项目时,出现错误Cannot download 'https://start.spring.io': connect timed out
- win10下nginx部署spring boot项目的配置与启动
- 使用spring boot +Junit 测试 spring mvc 配置中心项目
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- 使用Spring Boot搭配mybatis配置项目
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- SpringBoot 项目属性的配置和使用
- 4 springboot项目集成使用disconf,配置文件更新及回调