zoukankan      html  css  js  c++  java
  • http 切换成 https

    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;
        }
    
        /**
         * https 切换(包含scheme,http://),url含有端口不替换
         * @param url
         * @return
         */
        public static String httpsSwitch(String url, String httpsSwitch){
            if(StringUtils.isBlank(url)){
                return url;
            }
            if ("0".equals(httpsSwitch)) {
                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;
        }
    
    }
    随笔看心情
  • 相关阅读:
    Firefly 3288又一次制作android和lubuntu双系统固件
    想做一个完美的健身训练计划,你须要知道什么?
    【LeetCode-面试算法经典-Java实现】【075-Sort Colors (颜色排序)】
    每天进步一点点——Ganglia的Python扩展模块开发
    Unity3D-rigidBody.velocity
    泛型初识
    HDOJ 5418 Victor and World 状压DP
    UIPopoverController具体解释
    怎样提升站点的性能?
    PHP操作MongoDB数据库具体样例介绍(增、删、改、查) (六)
  • 原文地址:https://www.cnblogs.com/stromgao/p/13408540.html
Copyright © 2011-2022 走看看