前后端分离部署https

引用:https://blog.csdn.net/weixin_35676679/article/details/127841598

前后端部署,,一般用的是nginx和java,,,

下载SSL证书:

java配置https

将证书配置到springboot中

yml 复制代码
server:
  port: 5443  # https的端口
  http:
    port: 5050  # http的端口
 
  ssl:
    key-store: classpath:XXXX.pfx    #类路径下的CA证书
    key-store-password: 证书密码     #证书密码

配置Tomcat,,将http端口转发到https上:

java 复制代码
@Configuration
public class TomcatConfig {
    @Value("${server.http.port}")
    private int httpPort;
    @Value("${server.port}")
    private int httpsPort;




    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(connector);
        return tomcat;
    }


    @Bean
    public Connector connector(){
        Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        //http端口
        connector.setPort(httpPort);
        connector.setSecure(true);
        // https端口,即server.port
        connector.setRedirectPort(httpsPort); //重定向到htts
        return connector;
    }
}
配置nginx的证书
shell 复制代码
server{

listen 443 ssl;
server_name 域名;
ssl_certificate 证书.pem;
ssl_certificate_key  证书.key;


ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;



location /{
		# 前端位置
		root /home/wechat/dist;
          index  index.html index.htm;
            # 配置 history模式,刷新页面会404,,因为服务器没有正确的处理路由请求
                        try_files $uri $uri/ /index.html;


}


location /api/ {
  proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass https://127.0.0.1:8082/;



}


}

443 访问前端,,,转到后端,,,

遇到的问题:

  • 前端上传图片端口,,和后端不是一个端口,,====》上传文件端口,request.getServerPort() 获取端口为什么是443 ,,不是后端的8082
相关推荐
pingxiaozhao1 小时前
在Ubuntu内网环境中为Gogs配置HTTPS访问(通过Apache反向代理使用IP地址)
ubuntu·https·apache
续亮~5 小时前
ANP协议深度解析:智能体网络协议的演进与革新
网络·后端·网络协议·ai·ai编程
学习2年半7 小时前
+++++背到厌倦。持续更新
网络·网络协议·rpc
秉承初心7 小时前
HTTP 压力测试工具autocannon(AI)
网络协议·测试工具·http
Lonwayne7 小时前
为什么ChatGPT选择SSE而非WebSocket?
websocket·网络协议·chatgpt·程序那些事
凯凯爱前端9 小时前
通俗易懂的 TLS 协商过程
http
小徐Chao努力10 小时前
【安全】加密算法原理与实战
安全·https·des·ssl·加密·aes·rsa
昊昊该干饭了12 小时前
玩转代理 IP :实战爬虫案例
运维·服务器·爬虫·网络协议·tcp/ip·网络爬虫
无名之逆13 小时前
[特殊字符] 超轻高性能的 Rust HTTP 服务器 —— Hyperlane [特殊字符][特殊字符]
java·服务器·开发语言·前端·网络·http·rust
GZX墨痕13 小时前
从集线器到路由器:解码网络设备与ARP的通信密码
网络协议