Spring boot 使用HTTPS
来源:互联网 发布:天津中为数据 编辑:程序博客网 时间:2024/06/03 19:58
应用使用HTTPS
Spring boot 项目使用https ,本文将实现自定义密钥和将HTTP重定向到HTTPS功能,或者直接使用https访问系统,暂不支持证书下发
- 自定义密钥
- 自定义密钥
/** * https密钥 */public static void pkcKey() { String[] commands = new String[]{ "cmd", "/k", //cmd Shell命令 "start", "keytool", //genkey表示生成密钥 "-genkey", //别名 "-alias", "tomcat", //store类型 "-storetype", "PKCS12", //加密算法 "-keyalg", "RSA", //密钥大小 "-keysize", "2048", //key位置 "-keystore", "D:/account.p12", //证书有效期(单位:天) "-validity", "3650", //密钥库密码,至少为6位 "-storepass", "123456", //别名条目密码 "-keypass", "123456", //CN=名字与姓氏,OU=组织单位名称,O=组织名称,L=城市或区域名 称,ST=州或省份名称,C=单位的两字母国家代码 "-dname", "CN=(WQ),OU=(WQ),O=(WQ),L=(BJ),ST=(BJ),C=(CN)", //显示证书详情 "-v" }; try { execCommand(commands); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } } /** * 执行cmd命令 * @param commands * @throws IOException */ public static void execCommand(String... commands) throws IOException { Runtime.getRuntime().exec(commands); }
- 配置文件
http: port: 1314server: port: 520 ssl: key-store: D:/account.p12 key-store-type: PKCS12 key-password: 123456 key-store-password: 123456
- 将HTTP重定向到HTTPS
/** * @author weiQiang */@Configurationpublic class AccountConfig { @Value("${http.port}") private Integer httpPort; @Value("${server.port}") private Integer serverPort; @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() { @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(initiateHttpConnector()); return tomcat; } /** * HTTP重定向到HTTPS * * @return */ private Connector initiateHttpConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(httpPort); connector.setSecure(false); connector.setRedirectPort(serverPort); return connector; }}
访问:http://localhost:1314 重定向:https://localhost:520
阅读全文
0 0
- Spring boot 使用HTTPS
- spring boot 启用https
- Spring Boot 实现 HTTPS
- Spring Boot开启https
- Enable HTTPS in Spring Boot
- Spring Boot中启动HTTPS
- Spring Boot中启动HTTPS
- spring boot 采用https启动
- spring boot 配置 https|ssl
- Spring Boot工程支持HTTP和HTTPS
- 让spring boot支持https和http
- 【spring boot】配置ssl证书实现https
- spring boot设置http https端口
- spring boot 配置ssl证书实现https
- spring-boot 添加http自动转向https
- spring boot 配置https和SSL
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- mciSendCommand函数使用
- 二分图算法汇总
- 感叹python的简洁(判断回文)
- Redux状态管理5 使用react-redux
- 如何在VScode中配置Python的交互式环境
- Spring boot 使用HTTPS
- ElasticSearch之——Java操作ES实例(基于ES-2.3.0)
- java调用HttpClient查询手机号码归属地
- Cow Contest
- Maven项目中c3p0连接数据库及实例
- Oracal 11g client安装
- 2017 Java面试知识
- 在Ubuntu 16.04.3 LTS上运行go install出错的问题
- 【VBA自学笔记(一)】数组