https://blog.csdn.net/a_squirrel/article/details/79729690

一、Https 简介(百度百科)

HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。 它是一个URI scheme(抽象标识符体系),句法类同http:体系。用于安全的HTTP数据传输。https:URL表明它使用了HTTP,但HTTPS存在不同于HTTP的默认端口及一个加密/身份验证层(在HTTP与TCP之间)。这个系统的最初研发由网景公司(Netscape)进行,并内置于其浏览器Netscape Navigator中,提供了身份验证与加密通讯方法。现在它被广泛用于万维网上安全敏感的通讯,例如交易支付方面。

二、Springcloud添加 https

直接配置(无Nginx):

1. 首先需要SSL证书,可以自己生成(浏览器不认可),也可购买。

2. 自己生成方法(不推荐):https://www.cnblogs.com/zhangzb/p/5200418.html

3. 免费一年证书(推荐):https://buy.cloud.tencent.com/ssl?fromSource=ssl

4. 下载完成后解压目录如下:

springcloud(springboot)一般默认嵌入tomcat中间件(服务器),如果有其他中间件选择对应文件夹下就好。

tomcat打开如下:.jks证书文件, keystorePass.txt 为证书秘钥

5. 将证书文件放入要添加https服务的resources下,一般为web层服务,然后打开springcloud(springboot)配置文件application/bootstarp.yml(properties)

server.port  注册服务端口也是项目访问端口   server.ssl.key-store 证书路径   server.ssl.key-store-password 秘钥(.txt中内容)  http.port 做转发的端口,如果不做端口转发可以不用配置,名字可以随便起 ,端口号也可自定义(不要冲突就好)。接下来直接启动服务即可,然后即可用https://+项目路径,进行访问。

6. https配置类编写(不做端口转发可以不写)

注意:如果项目配置文件中同时有management.port,需要将其注释掉,不然无法启动服务。

/**
* @Title: HttpsPort.java
* @Description: 添加https
* @author zxj
* @version V1.0
*/
package com.ewp.data.config; import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component; /**
* @ClassName: HttpsPort
* @Description: 添加https
* @date 2018年3月14日
*
*/
@Configuration 
public class HttpsPort { @Value("${server.port}")
private int sPort; @Value("${http.port}")
private int hPort; private static class Tomcat extends TomcatEmbeddedServletContainerFactory{// 静态内部类
@Override
protected void postProcessContext(Context context) {
SecurityConstraint constraint = new SecurityConstraint();
constraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
constraint.addCollection(collection);
context.addConstraint(constraint);
}
} @Bean
public EmbeddedServletContainerFactory servletContainer() {// 创建新的tomcat示例,指向定义的http连接
Tomcat tomcat = new Tomcat();
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
} @Bean
public Connector httpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(hPort);
connector.setSecure(false);
connector.setRedirectPort(sPort);
return connector;
} }
有Nginx配置:

1.打开linux下Nginx安装目录找到配置文件nginx.conf(一般路径为:/usr/local/nginx/conf)进行如下配置

# HTTPS server
server {
listen 443 ssl;#网页浏览端口,主要是用于HTTPS服务
server_name www.asquirrel.cn asquirrel.cn;#顶级域名,二级域名
ssl on;
ssl_certificate 1_asquirrel.cn_bundle.crt;#证书路径
ssl_certificate_key 2_asquirrel.cn.key;#证书秘钥路径
ssl_session_timeout 5m;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
location / {
proxy_pass https://ewp_web_contract; #代理转发的路径
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
error_page 502 = /500.html;
} }

2.将下载好的证书放在与nginx.conf同级目录下,可新建文件夹,也可不建

证书用Nignx文件夹下的,不要用其他文件夹下的

3.监听80端口,转发请求

server {
listen 80;
server_name asquirrel.cn;#访问的路径
if ($host != 'www.asquirrel.cn') {
rewrite ^/(.*)$ https://www.asquirrel.cn/$1 permanent;#转发的路径
} }

4.项目web模块需做如下配置(主要作用于项目内部进行https交互)

第一:添加tomcat文件夹下的证书

第二:配置application.properties

五:重启项目web层服务和Nignx,访问域名就可以看到浏览器显示安全标志

GitHub:https://github.com/acutesquirrel/spring-cloud/tree/master/add-https

Springcloud/Springboot项目绑定域名,使用Nginx配置Https的更多相关文章

  1. ******可用 SpringBoot 项目打包分开lib,配置和资源文件

    spring-boot多模块打包后,无法找到其他模块中的类https://blog.csdn.net/Can96/article/details/96172172 关于SpringBoot项目打包没有 ...

  2. Nginx 配置 HTTPS(多域名)

    平常开发要求比较低, 依然在用 HTTP, 但到了微信小程序就不行了, 腾讯和苹果都对 API 提出了 HTTPS 的要求. 尤其是苹果, 不仅要求 HTTPS, 还要求 TLS 协议版本要在 1.2 ...

  3. windwos下nginx 配置https并http强制跳转https

    windwos下nginx  配置https并http强制跳转https 一.首先配置证书文件 申请证书文件,这里就不做详细过程了,直接看证书文件结果. 这是两个证书的关键文件 打开ngxin下con ...

  4. Nginx配置Https(详细、完整)

    Nginx配置Https(详细.完整) 原文链接:请支持原创 前置条件: 在配置https之前请确保下面的步骤已经完成 服务器已经安装nginx并且通过http可以正常访问 不会安装nginx的可以参 ...

  5. 【Nginx(五)】Nginx配置Https证书

    大致的流程如下 1.申请Https证书,绑定域名信息; 由于自己的服务器是腾讯云服务器, 这里就在腾讯云上申请SSL证书, 申请地址: https://console.cloud.tencent.co ...

  6. Nginx配置Https

    1.申请证书: https://console.qcloud.com/ssl?utm_source=yingyongbao&utm_medium=ssl&utm_campaign=qc ...

  7. 【转】Linux下nginx配置https协议访问的方法

    一.配置nginx支持https协议访问,需要在编译安装nginx的时候添加相应的模块--with-http_ssl_module 查看nginx编译参数:/usr/local/nginx/sbin/ ...

  8. nginx配置https双向验证(ca机构证书+自签证书)

    nginx配置https双向验证 服务端验证(ca机构证书) 客户端验证(服务器自签证书) 本文用的阿里云签发的免费证书实验,下载nginx安装ssl,文件夹有两个文件 这两个文件用于做服务器http ...

  9. Nginx 配置https 服务

    一.HTTPS 服务 为什么需要HTTPS? 原因:HTTP不安全 1.传输数据被中间人盗用.信息泄露 2.数据内容劫持.篡改 HTTPS协议的实现 对传输内容进行加密以及身份验证 HTTPS加密校验 ...

随机推荐

  1. Android在使用WebView时,通过Javascript调用JAVA函数

    webView = (WebView) findViewById(R.id.article_webview); //WebView启用Javascript脚本运行 webView.getSetting ...

  2. go-008-循环语句

    一.循环语句[只有for] 1.基础结构: Go语言的For循环有3中形式,只有其中的一种使用分号. 和 C 语言的 for 一样: for init; condition; post { } 和 C ...

  3. python学习笔记(十一)redis的介绍及安装

    一.redis简介 1.redis是一个开源的.使用C语言编写的.支持网络交互的.可基于内存也可持久化的Key-Value数据库.       2.redis的官网地址,非常好记,是redis.io. ...

  4. 从原型链看DOM--Document类型

    JavaScript通过Document类型表示文档,原型链的继承关系为:document.__proto__->HTMLDocument.prototype->Document.prot ...

  5. C#生成电子印章源码

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  6. django 使用form验证用户名和密码

    form验证可以减少查询数据库,所以代码先预先验证,有问题可以返回给前端显示 1.在users文件夹下新建forms.py文件,用来验证用户名和密码是否为空,密码长度是否大于6 # -*- codin ...

  7. [golang note] 匿名组合

    匿名组合 golang也提供了继承机制,但采用组合的文法,因此称为匿名组合.与其他语言不同, golang很清晰地展示出类的内存布局是怎样的. • 非指针方式组合 ▶  基本语法 // 基类 type ...

  8. 支持向量机SVM、优化问题、核函数

    1.介绍 它是一种二类分类模型,其基本模型定义为特征空间上的间隔最大的线性分类器,即支持向量机的学习策略便是间隔最大化,最终可转化为一个凸二次规划问题的求解. 2.求解过程 1.数据分类—SVM引入 ...

  9. SQLSERVER2016 无域控AlwaysOn 实施步骤

    SQLSERVER2016 无域控AlwaysOn 实施步骤 步骤: 一.安装3个windows server 2016系统 1)在3台机器建立具有administrators权限的相同账号密码,本例 ...

  10. Restful概念

    文章节选自: http://www.ruanyifeng.com/blog/2011/09/restful https://www.zhihu.com/question/28557115/answer ...