{"id":555,"date":"2022-07-04T15:28:25","date_gmt":"2022-07-04T07:28:25","guid":{"rendered":"http:\/\/blog.nonot.cn\/?p=555"},"modified":"2024-07-08T12:47:26","modified_gmt":"2024-07-08T04:47:26","slug":"java%e4%b8%ad%e5%b0%86http-%e5%88%87%e6%8d%a2%e6%88%90-https","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2022\/07\/04\/java%e4%b8%ad%e5%b0%86http-%e5%88%87%e6%8d%a2%e6%88%90-https\/","title":{"rendered":"java\u4e2d\u5c06http \u5207\u6362\u6210 https"},"content":{"rendered":"<pre class=\"lang:default decode:true  \">public class HttpsUtil {\n\n    private static final String HTTPS = \"https:\/\/\";\n\n    private static final String HTTP = \"http:\/\/\";\n\n    \/**\n     * https \u5207\u6362 \uff08\u4e0d\u5305\u542bscheme\uff0chttp:\/\/\uff09\n     * @param request\n     * @param url\n     * @return\n     *\/\n    public static String httpsSwitch(HttpServletRequest request, String url, String httpsSwitch, String appServer){\n        if(StringUtils.isBlank(url)){\n            return url;\n        }\n        if (\"0\".equals(httpsSwitch)) {\n            return url;\n        }\n        String newUrl = \"\";\n        String host = request.getHeader(\"host\");\n        if (StringUtils.isBlank(host)) {\n            host = appServer.replace(\"http:\/\/\",\"\");\n        }\n        host = host.replace(\":80\", \"\");\n        newUrl = HTTPS + host + request.getContextPath() + url;\n        return newUrl;\n    }\n\n    \/**\n     * https \u5207\u6362\uff08\u5305\u542bscheme\uff0chttp:\/\/\uff09\uff0curl\u542b\u6709\u7aef\u53e3\u4e0d\u66ff\u6362\n     * @param url\n     * @return\n     *\/\n    public static String httpsSwitch(String url, String httpsSwitch){\n        if(StringUtils.isBlank(url)){\n            return url;\n        }\n        if (\"0\".equals(httpsSwitch)) {\n            return url;\n        }\n        String newUrl = \"\";\n        List&lt;String&gt; newUrlList = new ArrayList&lt;&gt;();\n        if (url.contains(\"http:\/\/\")) {\n            replaceHttp(url, newUrl, newUrlList);\n            if (!CollectionUtils.isEmpty(newUrlList)) {\n                newUrl = newUrlList.get(0);\n            }\n        } else {\n            newUrl = url;\n        }\n        return newUrl;\n    }\n\n    \/**\n     * \u66ff\u6362http\n     * @param url\n     * @param newUrl\n     * @param newUrlList\n     * @return\n     *\/\n    private static void replaceHttp(String url, String newUrl, List&lt;String&gt; newUrlList) {\n        String scheme = \"\";\n        String context = \"\";\n        String[] array = url.split(\":\/\/\", 2);\n        if (array.length &gt; 0) {\n            scheme = array[0];\n            context = array[1];\n        }\n\n        \/\/ \u662f\u5426\u542b\u6709\u7aef\u53e3\u53f7\n        boolean containsPort = containsPort(context);\n        \/\/ \u4e0d\u542b\u7aef\u53e3\u53f7\u624d\u66ff\u6362\n        if (!containsPort) {\n            scheme = scheme.replaceFirst(\"http\", HTTPS);\n        } else {\n            scheme = HTTP;\n        }\n        newUrl = newUrl + scheme;\n        \/\/ \u540e\u9762url\u662f\u5426\u542b\u6709:\/\/\n        boolean contains = context.contains(\":\/\/\");\n        if (contains) {\n            replaceHttp(context, newUrl, newUrlList);\n        } else {\n            newUrl = newUrl + context;\n            newUrlList.add(newUrl);\n        }\n    }\n\n    \/**\n     * \u662f\u5426\u542b\u6709\u7aef\u53e3\u53f7\n     * @param url\n     * @return\n     *\/\n    private static boolean containsPort(String url) {\n        if (StringUtils.isNotBlank(url)) {\n            int firstIndex = url.indexOf(\"\/\");\n            if (firstIndex != -1) {\n                String remoteAddr = url.substring(0, firstIndex);\n                if (remoteAddr.contains(\":\")) {\n                    return true;\n                }\n            } else {\n                if (url.contains(\":\")) {\n                    return true;\n                }\n            }\n        }\n        return false;\n    }\n\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>public class HttpsUtil { priva&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-555","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/comments?post=555"}],"version-history":[{"count":1,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/555\/revisions"}],"predecessor-version":[{"id":1190,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/555\/revisions\/1190"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}