springBoot配置https请求

第一步:生成服务端配置jks文件
keytool -genkeypair -alias weblogic -keyalg RSA -keypass spdb1234 -storepass spdb1234 -keystore weblogic.jks -validity 3650 -ext SAN=IP:124.74.239.46 -dname "CN=124.74.239.46,OU=Developer,O=Digital China,L=Shanghai,ST=Shanghai,C=home"
第二步:生成客户端请求认证文件
keytool -export -trustcacerts -alias weblogic -file weblogic.cer -keystore weblogic.jks -storepass spdb1234

springBoot服务端配置:
第一步:将jks文件放入resource目录下
第二步:配置yml文件

server:
  port: 443
  custom:
    httpPort: 80
  ssl:
    key-store: classpath:weblogic.jks
    key-store-password: spdb1234
    keyStoreType: JKS
    keyAlias: weblogic

第三步:添加https请求配置类

package com.example.demo.Util;

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.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ProjectName: uublog
 * @Package: com.lhc.uublog.utils
 * @ClassName: SSLUtils
 * @Author: mvp_lee
 * @Description: Http重定向到Https
 */
@Configuration
public class SSLUtils {

    @Bean
    public Connector connector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setSecure(false);
        connector.setPort(80);
        connector.setRedirectPort(443);
        return connector;
    }

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

你可能感兴趣的:(安卓开发,https)