SringBoot 如何使用HTTPS请求及Nginx配置Https

SringBoot 如何使用HTTPS请求

生成证书

由于业务数据在传输过程中需要保密,我们组小同学写了一个HTTPS接口。该接口用到的证书是通过JDK自带的证书生成工具keytool,使用keytool来生成证书。打开终端或者命令行输入命令,回车,然后输入信息,其中秘钥库口令和秘要口令最好输入同一个,并且记下这个口令。

//keytool -genkey -alias tomcat  -storetype PKCS12 -keyalg RSA -keysize 2048  -keystore keystore.p12 -validity 3650
keytool -genkey -alias kshttps -keyalg RSA -keysize 2048 -validity 36524 -keystore kshttps.jks
keytool -importkeystore -srckeystore kshttps.jks -destkeystore kshttps.pkcs12 -deststoretype pkcs12
openssl pkcs12 -nodes -in kshttps.pkcs12 -out kshttps.pem
openssl pkcs12 -nocerts -nodes -in kshttps.pkcs12 -out kshttps.key
  • key -genkey 生成
  • -alias 别名
  • -storetype 指定密钥库的类型,eg:JKS 、PKCS12(通常以.p12或.pfx两种文件形式出现)、PKCS11、BCFKS、DKS、Windows-MY、Windows-ROOT、PEM(不能直接用Keytool导出,但可用OpenSSL工具一起使用)
  • -keyalg RSA 代表算法.RSA是非对称加密
  • -keysize 密钥长度
  • -keystore 生成路径文件名
  • -validity有效期

导入证书及配制

证书生成完成后,可以导入到到项目中,将其复制到Springboot项目的resources目录下
配置yml配置文件即可

server:
 port: 8080
   ssl:
    #开启https
    enabled: true
    #指定存放证书的密钥库文件的位置
    key-store: classpath:springboot.keystore
    #密钥库文件的格式
    key-store-type: PKCS12
    #别名,需要与创建密钥库时的别名一致
    key-alias: springboot-https
    key-password: 123456
key-store-password: 123456
http:
  port: 8090

创建配置类

@Configuration
public class HttpsConfig {

    @Value("${http.port}")
    private Integer httpPort;


    @Bean
    public ServletWebServerFactory  servletContainer() {
        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setPort(httpPort);
        final TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();

        tomcat.addAdditionalTomcatConnectors(connector);
        return tomcat;
    }
}

将pfx转成.key和.pem

为什么转呢,因为Nginx的配置中是要求一个.key和.pem格式的,其他可否,我没有验证。大家可以自己查询。但我是直接转成两后缀的文件了。转换工具可能过openssl转,参考命令如下所示:

openssl pkcs12 -in keystore.pfx -out cert.key -nocerts
openssl pkcs12 -in keystore.pfx -out cert.pem -nodes

其他转换当了解了,也附在本文中,不想学习的可跳过这部分

1.pem转换pfx

openssl pkcs12 -export -in 'test.pem' -inkey 'test.key' -out 'test.p12' -passout pass:123456

2.pem转换jks

openssl pkcs12 -export -in 'test.pem' -inkey 'test.key' -out 'test.p12' -passout pass:123456

keytool -importkeystore -srckeystore 'test.p12' -srcstoretype PKCS12 -destkeystore 'test.jks' -srcstorepass 123456 -deststorepass 123456

3.pfx转换pem

openssl pkcs12 -in test.p12 -passin pass:123456 -out test3.pem -nodes

4.pfx转换jks

keytool -importkeystore -srckeystore 'test.p12' -srcstoretype PKCS12 -destkeystore 'test.jks' -srcstorepass 123456 -deststorepass 123456

5.jks转换pem

keytool -importkeystore -srckeystore 'test.jks' -srcstoretype jks -destkeystore 'test.p12' -deststoretype PKCS12 -srcstorepass 123456 -deststorepass 123456

openssl pkcs12 -in test.p12 -passin pass:123456 -out test3.pem -nodes

6.jks转化pfx

keytool -importkeystore -srckeystore 'test.jks' -srcstoretype jks -destkeystore 'test.p12' -deststoretype PKCS12 -srcstorepass 123456 -deststorepass 123456

Nginx 安装SSL依赖

Nginx配置HTTPS,需要先安装依赖。否用支报如下错误
在这里插入图片描述
如上所示,我们可以执行以下命令解决

./configure 安装依赖

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module

如果报以下错误提示,请顺序读完,如果不报请跳至下一章节

You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

解决这个问题,首先需要检查是否安装了OpenSSL,通过openssl version查看,未安装可以参考以下命令进行安装

yum -y install openssl openssl-devel

如果确定已安装,可找下安装位置。使用which openssl 命令。但需要注意,通过此命令如果找到的是安装后的命令则无效,仍不好使,需要找到安装源路径,如无法找到,可以通过https://www.openssl.org/source/ 下载进行安装。安装成功后,重新执行以下命令和make && install。 出现了新错误,莫慌,请看下去

 ./configure --with-openssl=/usr/openssl  --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module

find / -name  ssl.h

编译

make && make install

安装完openssl后报了新错

在这里插入图片描述
到这步后,需要修改下Nginx源路径下的 auto/lib/openssl/conf

 vi auto/lib/openssl/conf

修改内容如下所示:
在这里插入图片描述

Nginx配置

 server {
        listen       443 ssl;
        server_name  127.0.0.1;

        ssl_certificate      cert.pem;
        ssl_certificate_key  cert.key;

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

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

        location /wscc-interface {
    #        root   html;
    #       index  index.html index.htm;
            proxy_pass http://127.0.0.1:443/;
        }
    }


  • 10
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx配置Spring Boot应用可以通过proxy_pass指令实现请求转发。首先,需要在nginx配置文件中添加以下指令来定义转发规则: location / { proxy_pass http://localhost:8080; } 上述配置将会将所有请求转发到本地的8080端口,其中8080端口是Spring Boot应用的默认端口。如果Spring Boot应用运行在不同的主机或端口上,需要相应地修改proxy_pass指令中的地址。 同时,确保在Spring Boot应用的启动类中继承SpringBootServletInitializer类,并重写configure方法。这样可以将启动类交给Servlet容器进行启动。以下是一个示例的启动类代码: @SpringBootApplication public class MySpringBootApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(MySpringBootApplication.class); } } 在配置完成后,当访问nginx代理服务器时,请求将会被转发到Spring Boot应用所在的服务器上。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【SpringBoot】SpringBoot整合Nginx的全部流程](https://blog.csdn.net/qq_33591903/article/details/90715370)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值