http转成https工具类
工具类代码如下:
点击查看代码
package com.astronaut.auction.modules.oss.utils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
/**
* @Classname HttpsUtil
* @Description: http转换https工具类
* @Date: 2022/8/9 0009 9:27
* @AUTHOR: 无泪之城
* @Version 1.0
*/
public class HttpsUtil {
private static final String HTTPS = "https://";
private static final String HTTP = "http://";
/**
* https 切换 (不包含scheme,http://)
* @param request
* @param url
* @return
*/
public static String httpsSwitch(HttpServletRequest request, String url, String httpsSwitch, String appServer){
if(StringUtils.isBlank(url)){
return url;
}
if ("0".equals(httpsSwitch)) {
return url;
}
String newUrl = "";
String host = request.getHeader("host");
if (StringUtils.isBlank(host)) {
host = appServer.replace("http://","");
}
host = host.replace(":80", "");
newUrl = HTTPS + host + request.getContextPath() + url;
return newUrl;
}
/**
* http转成https (包含scheme,http://),url含有端口不替换
* @param url
* @return
*/
public static String httpsSwitch(String url){
if(StringUtils.isBlank(url)){
return url;
}
String newUrl = "";
List<String> newUrlList = new ArrayList<>();
if (url.contains("http://")) {
replaceHttp(url, newUrl, newUrlList);
if (!CollectionUtils.isEmpty(newUrlList)) {
newUrl = newUrlList.get(0);
}
} else {
newUrl = url;
}
return newUrl;
}
/**
* 替换http
* @param url
* @param newUrl
* @param newUrlList
* @return
*/
private static void replaceHttp(String url, String newUrl, List<String> newUrlList) {
String scheme = "";
String context = "";
String[] array = url.split("://", 2);
if (array.length > 0) {
scheme = array[0];
context = array[1];
}
// 是否含有端口号
boolean containsPort = containsPort(context);
// 不含端口号才替换
if (!containsPort) {
scheme = scheme.replaceFirst("http", HTTPS);
} else {
scheme = HTTP;
}
newUrl = newUrl + scheme;
// 后面url是否含有://
boolean contains = context.contains("://");
if (contains) {
replaceHttp(context, newUrl, newUrlList);
} else {
newUrl = newUrl + context;
newUrlList.add(newUrl);
}
}
/**
* 是否含有端口号
* @param url
* @return
*/
private static boolean containsPort(String url) {
if (StringUtils.isNotBlank(url)) {
int firstIndex = url.indexOf("/");
if (firstIndex != -1) {
String remoteAddr = url.substring(0, firstIndex);
if (remoteAddr.contains(":")) {
return true;
}
} else {
if (url.contains(":")) {
return true;
}
}
}
return false;
}
}
http转成https工具类的更多相关文章
- Java汉字转成汉语拼音工具类
Java汉字转成汉语拼音工具类,需要用到pinyin4j.jar包. import net.sourceforge.pinyin4j.PinyinHelper; import net.sourcefo ...
- java Https工具类
import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import ja ...
- http和https工具类 (要注意httpclient版本号和log4j的版本号)
1 工具类 package dd.com; import java.io.IOException; import java.security.cert.CertificateException; im ...
- 把实体bean对象转换成DBObject工具类
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util ...
- java安全HTTPS工具类
import java.io.FileInputStream; import java.security.KeyStore; import java.security.SecureRandom; im ...
- https工具类
import org.apache.commons.lang.StringUtils; import javax.net.ssl.*; import java.io.*; import java.ne ...
- Java-生成缩略图工具类
import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingH ...
- HttpUtil工具类
HttpUtil工具类 /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param params * 请求参数,请求参数应该是name1=val ...
- Android初级教程:对文件和字符串进行MD5加密工具类
转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/52200008 点击打开链接 之前写过一篇博文,是针对字符串进行md5加密的.今 ...
- HttpClinet工具类
一.URL调用 忽略https证书 1.调用 InputStream in = null; try { URL url = new URL( "url地址" ); IgnoreSS ...
随机推荐
- 使用python读取京东pdf发票信息导出到excel表格中
代码 #!/usr/bin/env python # -*- coding: utf-8 -*- """ pip install pdfminer3k pip insta ...
- cAdvisor容器监控规则
其他说明参考host主机监控规则:https://www.cnblogs.com/sanduzxcvbnm/p/13589848.html 在prometheus主程序目录下的rules目录下新建do ...
- typora基础和计算机五大组成部分
typora typora软件 是一款适合于IT行业文本编辑器,笔记,当下来说,非常火爆,可以使用多种语言,python java... 安装的时候路径选择可以设置一些简单便于后续查找的文件路 ...
- 用户输入学号,如果是以aabcddef开头,并且后边是4位数字,前两位大于06小于等于当前年份。判断用户输入是否合法
package seven_topic; import java.util.*; public class p_19_1 { public static void main(String[] args ...
- P1073 [NOIP2009 提高组] 最优贸易 (最短路spfa)
本题就是在一条1-n的路径上找p,q(先经过p),使得q-p最大. 考虑建正反图,正图上求出d[x],表示1-x的路径经过的节点最小值,反图上则从n开始求出f[x],x-n的最大值,最后枚举断点i,取 ...
- gradle项目对比maven项目的目录架构以及对gradle wrapper的理解
转载请注明出处: 1.使用 idea 搭建 gradle项目 注意 type 选择 gradle 以及 language 选择 grooy 搭建后的项目架构 ...
- 分布式存储系统之Ceph集群启用Dashboard及使用Prometheus监控Ceph
前文我们了解了Ceph之上的RadosGW基础使用相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/16768998.html:今天我们来聊一聊Ceph启 ...
- surging 将推出社区版微服务平台
前言 对于.NET大家并不陌生,有大批的企业选择.NET作为公司构建多种应用的开发平台,但是近几年随着微服务,大数据,移动端,物联网兴起,而后.NET社区生态没有跟上时代的步伐,已开始趋于没落,而其中 ...
- 面向对象day02,作业学生类,电脑类
学生类,电脑类,测试类 学生类:解释都写在注释里面 public class Student { public String name; public int id; public char gend ...
- Windows应急响应——敬请期待!
检查内容 进程.服务.用户.网络连接.漏洞补丁.木马查杀. 工具 火绒剑. 防护措施 杀毒软件