Java加密技术(九)——初探SSL

2023-11-04

  在 Java加密技术(八) 中,我们模拟了一个基于RSA非对称加密网络的安全通信。现在我们深度了解一下现有的安全网络通信——SSL。 
    我们需要构建一个由CA机构签发的有效证书,这里我们使用上文中生成的自签名证书 zlex.cer  
    这里,我们将证书导入到我们的密钥库。 

Shell代码   收藏代码
  1. keytool -import -alias www.zlex.org -file d:/zlex.cer -keystore d:/zlex.keystore  


其中 
-import表示 导入  
-alias指定别名,这里是 www.zlex.org  
-file指定算法,这里是 d:/zlex.cer  
-keystore指定存储位置,这里是 d:/zlex.keystore  
在这里我使用的密码为 654321  

控制台输出: 
Console代码   收藏代码
  1. 输入keystore密码:  
  2. 再次输入新密码:  
  3. 所有者:CN=www.zlex.org, OU=zlex, O=zlex, L=BJ, ST=BJ, C=CN  
  4. 签发人:CN=www.zlex.org, OU=zlex, O=zlex, L=BJ, ST=BJ, C=CN  
  5. 序列号:4a1e48df  
  6. 有效期: Thu May 28 16:18:39 CST 2009 至Wed Aug 26 16:18:39 CST 2009  
  7. 证书指纹:  
  8.          MD5:19:CA:E6:36:E2:DF:AD:96:31:97:2F:A9:AD:FC:37:6A  
  9.          SHA1:49:88:30:59:29:45:F1:69:CA:97:A9:6D:8A:CF:08:D2:C3:D5:C0:C4  
  10.          签名算法名称:SHA1withRSA  
  11.          版本: 3  
  12. 信任这个认证? [否]:  y  
  13. 认证已添加至keystore中  


OK,最复杂的准备工作已经完成。 
接下来我们将域名 www.zlex.org 定位到本机上。打开 C:\Windows\System32\drivers\etc\hosts 文件,将 www.zlex.org 绑定在本机上。在文件末尾追加 127.0.0.1       www.zlex.org 。现在通过地址栏访问 http://www.zlex.org ,或者通过 ping 命令,如果能够定位到本机,域名映射就搞定了。 
现在,配置tomcat。先将 zlex.keystore 拷贝到tomcat的conf目录下,然后配置 server.xml 。将如下内容加入配置文件
Xml代码   收藏代码
  1. <Connector  
  2.     SSLEnabled="true"  
  3.     URIEncoding="UTF-8"  
  4.     clientAuth="false"  
  5.     keystoreFile="conf/zlex.keystore"  
  6.     keystorePass="123456"  
  7.     maxThreads="150"  
  8.     port="443"  
  9.     protocol="HTTP/1.1"  
  10.     scheme="https"  
  11.     secure="true"  
  12.     sslProtocol="TLS" />  

注意 clientAuth="false" 测试阶段,置为 false ,正式使用时建议使用 true 。现在启动tomcat,访问 https://www.zlex.org/  
显然,证书未能通过认证,这个时候你可以选择安装证书(上文中的 zlex.cer 文件就是证书),作为 受信任的根证书颁发机构 导入,再次重启浏览器(IE,其他浏览器对于域名www.zlex.org不支持本地方式访问),访问 https://www.zlex.org/ ,你会看到地址栏中会有个小锁 ,就说明安装成功。所有的浏览器联网操作已经在RSA加密解密系统的保护之下了。但似乎我们感受不到。 
这个时候很多人开始怀疑,如果我们要手工做一个这样的https的访问是不是需要把浏览器的这些个功能都实现呢?不需要!  

接着上篇内容,给出如下代码实现: 
Java代码   收藏代码
  1. import java.io.FileInputStream;  
  2. import java.security.KeyStore;  
  3. import java.security.PrivateKey;  
  4. import java.security.PublicKey;  
  5. import java.security.Signature;  
  6. import java.security.cert.Certificate;  
  7. import java.security.cert.CertificateFactory;  
  8. import java.security.cert.X509Certificate;  
  9. import java.util.Date;  
  10.   
  11. import javax.crypto.Cipher;  
  12. import javax.net.ssl.HttpsURLConnection;  
  13. import javax.net.ssl.KeyManagerFactory;  
  14. import javax.net.ssl.SSLContext;  
  15. import javax.net.ssl.SSLSocketFactory;  
  16. import javax.net.ssl.TrustManagerFactory;  
  17.   
  18. /** 
  19.  * 证书组件 
  20.  *  
  21.  * @author 梁栋 
  22.  * @version 1.0 
  23.  * @since 1.0 
  24.  */  
  25. public abstract class CertificateCoder extends Coder {  
  26.   
  27.     /** 
  28.      * Java密钥库(Java Key Store,JKS)KEY_STORE 
  29.      */  
  30.     public static final String KEY_STORE = "JKS";  
  31.   
  32.     public static final String X509 = "X.509";  
  33.     public static final String SunX509 = "SunX509";  
  34.     public static final String SSL = "SSL";  
  35.   
  36.     /** 
  37.      * 由KeyStore获得私钥 
  38.      *  
  39.      * @param keyStorePath 
  40.      * @param alias 
  41.      * @param password 
  42.      * @return 
  43.      * @throws Exception 
  44.      */  
  45.     private static PrivateKey getPrivateKey(String keyStorePath, String alias,  
  46.             String password) throws Exception {  
  47.         KeyStore ks = getKeyStore(keyStorePath, password);  
  48.         PrivateKey key = (PrivateKey) ks.getKey(alias, password.toCharArray());  
  49.         return key;  
  50.     }  
  51.   
  52.     /** 
  53.      * 由Certificate获得公钥 
  54.      *  
  55.      * @param certificatePath 
  56.      * @return 
  57.      * @throws Exception 
  58.      */  
  59.     private static PublicKey getPublicKey(String certificatePath)  
  60.             throws Exception {  
  61.         Certificate certificate = getCertificate(certificatePath);  
  62.         PublicKey key = certificate.getPublicKey();  
  63.         return key;  
  64.     }  
  65.   
  66.     /** 
  67.      * 获得Certificate 
  68.      *  
  69.      * @param certificatePath 
  70.      * @return 
  71.      * @throws Exception 
  72.      */  
  73.     private static Certificate getCertificate(String certificatePath)  
  74.             throws Exception {  
  75.         CertificateFactory certificateFactory = CertificateFactory  
  76.                 .getInstance(X509);  
  77.         FileInputStream in = new FileInputStream(certificatePath);  
  78.   
  79.         Certificate certificate = certificateFactory.generateCertificate(in);  
  80.         in.close();  
  81.   
  82.         return certificate;  
  83.     }  
  84.   
  85.     /** 
  86.      * 获得Certificate 
  87.      *  
  88.      * @param keyStorePath 
  89.      * @param alias 
  90.      * @param password 
  91.      * @return 
  92.      * @throws Exception 
  93.      */  
  94.     private static Certificate getCertificate(String keyStorePath,  
  95.             String alias, String password) throws Exception {  
  96.         KeyStore ks = getKeyStore(keyStorePath, password);  
  97.         Certificate certificate = ks.getCertificate(alias);  
  98.   
  99.         return certificate;  
  100.     }  
  101.   
  102.     /** 
  103.      * 获得KeyStore 
  104.      *  
  105.      * @param keyStorePath 
  106.      * @param password 
  107.      * @return 
  108.      * @throws Exception 
  109.      */  
  110.     private static KeyStore getKeyStore(String keyStorePath, String password)  
  111.             throws Exception {  
  112.         FileInputStream is = new FileInputStream(keyStorePath);  
  113.         KeyStore ks = KeyStore.getInstance(KEY_STORE);  
  114.         ks.load(is, password.toCharArray());  
  115.         is.close();  
  116.         return ks;  
  117.     }  
  118.   
  119.     /** 
  120.      * 私钥加密 
  121.      *  
  122.      * @param data 
  123.      * @param keyStorePath 
  124.      * @param alias 
  125.      * @param password 
  126.      * @return 
  127.      * @throws Exception 
  128.      */  
  129.     public static byte[] encryptByPrivateKey(byte[] data, String keyStorePath,  
  130.             String alias, String password) throws Exception {  
  131.         // 取得私钥  
  132.         PrivateKey privateKey = getPrivateKey(keyStorePath, alias, password);  
  133.   
  134.         // 对数据加密  
  135.         Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());  
  136.         cipher.init(Cipher.ENCRYPT_MODE, privateKey);  
  137.   
  138.         return cipher.doFinal(data);  
  139.   
  140.     }  
  141.   
  142.     /** 
  143.      * 私钥解密 
  144.      *  
  145.      * @param data 
  146.      * @param keyStorePath 
  147.      * @param alias 
  148.      * @param password 
  149.      * @return 
  150.      * @throws Exception 
  151.      */  
  152.     public static byte[] decryptByPrivateKey(byte[] data, String keyStorePath,  
  153.             String alias, String password) throws Exception {  
  154.         // 取得私钥  
  155.         PrivateKey privateKey = getPrivateKey(keyStorePath, alias, password);  
  156.   
  157.         // 对数据加密  
  158.         Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());  
  159.         cipher.init(Cipher.DECRYPT_MODE, privateKey);  
  160.   
  161.         return cipher.doFinal(data);  
  162.   
  163.     }  
  164.   
  165.     /** 
  166.      * 公钥加密 
  167.      *  
  168.      * @param data 
  169.      * @param certificatePath 
  170.      * @return 
  171.      * @throws Exception 
  172.      */  
  173.     public static byte[] encryptByPublicKey(byte[] data, String certificatePath)  
  174.             throws Exception {  
  175.   
  176.         // 取得公钥  
  177.         PublicKey publicKey = getPublicKey(certificatePath);  
  178.         // 对数据加密  
  179.         Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());  
  180.         cipher.init(Cipher.ENCRYPT_MODE, publicKey);  
  181.   
  182.         return cipher.doFinal(data);  
  183.   
  184.     }  
  185.   
  186.     /** 
  187.      * 公钥解密 
  188.      *  
  189.      * @param data 
  190.      * @param certificatePath 
  191.      * @return 
  192.      * @throws Exception 
  193.      */  
  194.     public static byte[] decryptByPublicKey(byte[] data, String certificatePath)  
  195.             throws Exception {  
  196.         // 取得公钥  
  197.         PublicKey publicKey = getPublicKey(certificatePath);  
  198.   
  199.         // 对数据加密  
  200.         Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());  
  201.         cipher.init(Cipher.DECRYPT_MODE, publicKey);  
  202.   
  203.         return cipher.doFinal(data);  
  204.   
  205.     }  
  206.   
  207.     /** 
  208.      * 验证Certificate 
  209.      *  
  210.      * @param certificatePath 
  211.      * @return 
  212.      */  
  213.     public static boolean verifyCertificate(String certificatePath) {  
  214.         return verifyCertificate(new Date(), certificatePath);  
  215.     }  
  216.   
  217.     /** 
  218.      * 验证Certificate是否过期或无效 
  219.      *  
  220.      * @param date 
  221.      * @param certificatePath 
  222.      * @return 
  223.      */  
  224.     public static boolean verifyCertificate(Date date, String certificatePath) {  
  225.         boolean status = true;  
  226.         try {  
  227.             // 取得证书  
  228.             Certificate certificate = getCertificate(certificatePath);  
  229.             // 验证证书是否过期或无效  
  230.             status = verifyCertificate(date, certificate);  
  231.         } catch (Exception e) {  
  232.             status = false;  
  233.         }  
  234.         return status;  
  235.     }  
  236.   
  237.     /** 
  238.      * 验证证书是否过期或无效 
  239.      *  
  240.      * @param date 
  241.      * @param certificate 
  242.      * @return 
  243.      */  
  244.     private static boolean verifyCertificate(Date date, Certificate certificate) {  
  245.         boolean status = true;  
  246.         try {  
  247.             X509Certificate x509Certificate = (X509Certificate) certificate;  
  248.             x509Certificate.checkValidity(date);  
  249.         } catch (Exception e) {  
  250.             status = false;  
  251.         }  
  252.         return status;  
  253.     }  
  254.   
  255.     /** 
  256.      * 签名 
  257.      *  
  258.      * @param keyStorePath 
  259.      * @param alias 
  260.      * @param password 
  261.      *  
  262.      * @return 
  263.      * @throws Exception 
  264.      */  
  265.     public static String sign(byte[] sign, String keyStorePath, String alias,  
  266.             String password) throws Exception {  
  267.         // 获得证书  
  268.         X509Certificate x509Certificate = (X509Certificate) getCertificate(  
  269.                 keyStorePath, alias, password);  
  270.         // 获取私钥  
  271.         KeyStore ks = getKeyStore(keyStorePath, password);  
  272.         // 取得私钥  
  273.         PrivateKey privateKey = (PrivateKey) ks.getKey(alias, password  
  274.                 .toCharArray());  
  275.   
  276.         // 构建签名  
  277.         Signature signature = Signature.getInstance(x509Certificate  
  278.                 .getSigAlgName());  
  279.         signature.initSign(privateKey);  
  280.         signature.update(sign);  
  281.         return encryptBASE64(signature.sign());  
  282.     }  
  283.   
  284.     /** 
  285.      * 验证签名 
  286.      *  
  287.      * @param data 
  288.      * @param sign 
  289.      * @param certificatePath 
  290.      * @return 
  291.      * @throws Exception 
  292.      */  
  293.     public static boolean verify(byte[] data, String sign,  
  294.             String certificatePath) throws Exception {  
  295.         // 获得证书  
  296.         X509Certificate x509Certificate = (X509Certificate) getCertificate(certificatePath);  
  297.         // 获得公钥  
  298.         PublicKey publicKey = x509Certificate.getPublicKey();  
  299.         // 构建签名  
  300.         Signature signature = Signature.getInstance(x509Certificate  
  301.                 .getSigAlgName());  
  302.         signature.initVerify(publicKey);  
  303.         signature.update(data);  
  304.   
  305.         return signature.verify(decryptBASE64(sign));  
  306.   
  307.     }  
  308.   
  309.     /** 
  310.      * 验证Certificate 
  311.      *  
  312.      * @param keyStorePath 
  313.      * @param alias 
  314.      * @param password 
  315.      * @return 
  316.      */  
  317.     public static boolean verifyCertificate(Date date, String keyStorePath,  
  318.             String alias, String password) {  
  319.         boolean status = true;  
  320.         try {  
  321.             Certificate certificate = getCertificate(keyStorePath, alias,  
  322.                     password);  
  323.             status = verifyCertificate(date, certificate);  
  324.         } catch (Exception e) {  
  325.             status = false;  
  326.         }  
  327.         return status;  
  328.     }  
  329.   
  330.     /** 
  331.      * 验证Certificate 
  332.      *  
  333.      * @param keyStorePath 
  334.      * @param alias 
  335.      * @param password 
  336.      * @return 
  337.      */  
  338.     public static boolean verifyCertificate(String keyStorePath, String alias,  
  339.             String password) {  
  340.         return verifyCertificate(new Date(), keyStorePath, alias, password);  
  341.     }  
  342.   
  343.     /** 
  344.      * 获得SSLSocektFactory 
  345.      *  
  346.      * @param password 
  347.      *            密码 
  348.      * @param keyStorePath 
  349.      *            密钥库路径 
  350.      *  
  351.      * @param trustKeyStorePath 
  352.      *            信任库路径 
  353.      * @return 
  354.      * @throws Exception 
  355.      */  
  356.     private static SSLSocketFactory getSSLSocketFactory(String password,  
  357.             String keyStorePath, String trustKeyStorePath) throws Exception {  
  358.         // 初始化密钥库  
  359.         KeyManagerFactory keyManagerFactory = KeyManagerFactory  
  360.                 .getInstance(SunX509);  
  361.         KeyStore keyStore = getKeyStore(keyStorePath, password);  
  362.         keyManagerFactory.init(keyStore, password.toCharArray());  
  363.   
  364.         // 初始化信任库  
  365.         TrustManagerFactory trustManagerFactory = TrustManagerFactory  
  366.                 .getInstance(SunX509);  
  367.         KeyStore trustkeyStore = getKeyStore(trustKeyStorePath, password);  
  368.         trustManagerFactory.init(trustkeyStore);  
  369.   
  370.         // 初始化SSL上下文  
  371.         SSLContext ctx = SSLContext.getInstance(SSL);  
  372.         ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory  
  373.                 .getTrustManagers(), null);  
  374.         SSLSocketFactory sf = ctx.getSocketFactory();  
  375.   
  376.         return sf;  
  377.     }  
  378.   
  379.     /** 
  380.      * 为HttpsURLConnection配置SSLSocketFactory 
  381.      *  
  382.      * @param conn 
  383.      *            HttpsURLConnection 
  384.      * @param password 
  385.      *            密码 
  386.      * @param keyStorePath 
  387.      *            密钥库路径 
  388.      *  
  389.      * @param trustKeyStorePath 
  390.      *            信任库路径 
  391.      * @throws Exception 
  392.      */  
  393.     public static void configSSLSocketFactory(HttpsURLConnection conn,  
  394.             String password, String keyStorePath, String trustKeyStorePath)  
  395.             throws Exception {  
  396.         conn.setSSLSocketFactory(getSSLSocketFactory(password, keyStorePath,  
  397.                 trustKeyStorePath));  
  398.     }  
  399. }  


增加了 configSSLSocketFactory 方法供外界调用,该方法为HttpsURLConnection配置了SSLSocketFactory。当HttpsURLConnection配置了SSLSocketFactory后,我们就可以通过HttpsURLConnection的getInputStream、getOutputStream,像往常使用HttpURLConnection做操作了。尤其要说明一点,未配置SSLSocketFactory前,HttpsURLConnection的getContentLength()获得值永远都是 -1 。 

给出相应测试类: 
Java代码   收藏代码
  1. import static org.junit.Assert.*;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.io.InputStream;  
  5. import java.net.URL;  
  6.   
  7. import javax.net.ssl.HttpsURLConnection;  
  8.   
  9. import org.junit.Test;  
  10.   
  11. /** 
  12.  *  
  13.  * @author 梁栋 
  14.  * @version 1.0 
  15.  * @since 1.0 
  16.  */  
  17. public class CertificateCoderTest {  
  18.     private String password = "123456";  
  19.     private String alias = "www.zlex.org";  
  20.     private String certificatePath = "d:/zlex.cer";  
  21.     private String keyStorePath = "d:/zlex.keystore";  
  22.     private String clientKeyStorePath = "d:/zlex-client.keystore";  
  23.     private String clientPassword = "654321";  
  24.   
  25.     @Test  
  26.     public void test() throws Exception {  
  27.         System.err.println("公钥加密——私钥解密");  
  28.         String inputStr = "Ceritifcate";  
  29.         byte[] data = inputStr.getBytes();  
  30.   
  31.         byte[] encrypt = CertificateCoder.encryptByPublicKey(data,  
  32.                 certificatePath);  
  33.   
  34.         byte[] decrypt = CertificateCoder.decryptByPrivateKey(encrypt,  
  35.                 keyStorePath, alias, password);  
  36.         String outputStr = new String(decrypt);  
  37.   
  38.         System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);  
  39.   
  40.         // 验证数据一致  
  41.         assertArrayEquals(data, decrypt);  
  42.   
  43.         // 验证证书有效  
  44.         assertTrue(CertificateCoder.verifyCertificate(certificatePath));  
  45.   
  46.     }  
  47.   
  48.     @Test  
  49.     public void testSign() throws Exception {  
  50.         System.err.println("私钥加密——公钥解密");  
  51.   
  52.         String inputStr = "sign";  
  53.         byte[] data = inputStr.getBytes();  
  54.   
  55.         byte[] encodedData = CertificateCoder.encryptByPrivateKey(data,  
  56.                 keyStorePath, alias, password);  
  57.   
  58.         byte[] decodedData = CertificateCoder.decryptByPublicKey(encodedData,  
  59.                 certificatePath);  
  60.   
  61.         String outputStr = new String(decodedData);  
  62.         System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);  
  63.         assertEquals(inputStr, outputStr);  
  64.   
  65.         System.err.println("私钥签名——公钥验证签名");  
  66.         // 产生签名  
  67.         String sign = CertificateCoder.sign(encodedData, keyStorePath, alias,  
  68.                 password);  
  69.         System.err.println("签名:\r" + sign);  
  70.   
  71.         // 验证签名  
  72.         boolean status = CertificateCoder.verify(encodedData, sign,  
  73.                 certificatePath);  
  74.         System.err.println("状态:\r" + status);  
  75.         assertTrue(status);  
  76.   
  77.     }  
  78.   
  79.     @Test  
  80.     public void testHttps() throws Exception {  
  81.         URL url = new URL("https://www.zlex.org/examples/");  
  82.         HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();  
  83.   
  84.         conn.setDoInput(true);  
  85.         conn.setDoOutput(true);  
  86.   
  87.         CertificateCoder.configSSLSocketFactory(conn, clientPassword,  
  88.                 clientKeyStorePath, clientKeyStorePath);  
  89.   
  90.         InputStream is = conn.getInputStream();  
  91.   
  92.         int length = conn.getContentLength();  
  93.   
  94.         DataInputStream dis = new DataInputStream(is);  
  95.         byte[] data = new byte[length];  
  96.         dis.readFully(data);  
  97.   
  98.         dis.close();  
  99.         System.err.println(new String(data));  
  100.         conn.disconnect();  
  101.     }  
  102. }  

注意 testHttps 方法,几乎和我们往常做HTTP访问没有差别,我们来看控制台输出: 
Console代码   收藏代码
  1. <!--  
  2.   Licensed to the Apache Software Foundation (ASF) under one or more  
  3.   contributor license agreements.  See the NOTICE file distributed with  
  4.   this work for additional information regarding copyright ownership.  
  5.   The ASF licenses this file to You under the Apache License, Version 2.0  
  6.   (the "License"); you may not use this file except in compliance with  
  7.   the License.  You may obtain a copy of the License at  
  8.   
  9.       http://www.apache.org/licenses/LICENSE-2.0  
  10.   
  11.   Unless required by applicable law or agreed to in writing, software  
  12.   distributed under the License is distributed on an "AS IS" BASIS,  
  13.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  14.   See the License for the specific language governing permissions and  
  15.   limitations under the License.  
  16. -->  
  17. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
  18. <HTML><HEAD><TITLE>Apache Tomcat Examples</TITLE>  
  19. <META http-equiv=Content-Type content="text/html">  
  20. </HEAD>  
  21. <BODY>  
  22. <P>  
  23. <H3>Apache Tomcat Examples</H3>  
  24. <P></P>  
  25. <ul>  
  26. <li><a href="servlets">Servlets examples</a></li>  
  27. <li><a href="jsp">JSP Examples</a></li>  
  28. </ul>  
  29. </BODY></HTML>  

通过浏览器直接访问https://www.zlex.org/examples/你也会获得上述内容。也就是说应用甲方作为服务器构建tomcat服务,乙方可以通过上述方式访问甲方受保护的SSL应用,并且不需要考虑具体的加密解密问题。甲乙双方可以经过相应配置,通过双方的tomcat配置有效的SSL服务,简化上述代码实现,完全通过证书配置完成SSL双向认证! 

转载自:http://snowolf.iteye.com/blog/397693

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java加密技术(九)——初探SSL 的相关文章

  • 2.2.1 数据通信系统的模型

    一个数据通信系统分为三大部分 1 源系统 或发送端 发送方 2 传输系统 或传输网络 3 目的系统 或接收端 接收方 数据通信系统模型如下 上图中调制解调器有2个功能 1 调制 将计算机发出的低频 数字信号 转换成传输媒介可以传输的 模拟信
  • fetch中断请求, 和再次恢复使用

    业务场景 当时用fetch 建立长连接请求 在不使用时需要将其断掉 以缓解带宽压力和浏览器运行压力 等再次需要建立长链接时 再次启用 1 外层定义controller 一旦中止 AbortController就会被消耗 每次调用都必须创建新
  • Python基础学习-简要记录

    目录 快捷键 基础 1 字符串 2 变量 3 序列 4 列表 5 元组 6 字典 7 集合 8 time 模块 9 datetime 模块 date time datetime 类 10 calendar 模块 Calendar 类 Tex
  • 在1行上输入5个数字,数字之间用英文半角逗号分隔。输出其中最小的数字。 结果保留2位小数。

    题目描述 在1行上输入5个数字 数字之间用英文半角逗号分隔 输出其中最小的数字 结果保留2位小数 输入 6 4 5 2 3 输出 2 00 样例输入 Copy 12 22 2 32 42 样例输出 Copy 2 00 a map eval
  • undo表空间故障恢复

    time 2008 04 15author skate 参考文档 http blog chinaunix net u 7667 showart 163271 html undo表空间故障恢复 ORA 00376 file 2 cannot
  • mysql查询排名前5的语句_MySQL语句实现排名

    首先我们创建一张city popularity表 CREATE TABLEcity popularity regionint 10 NOT NULL COMMENT 1 国内 2 海外 city nameVARCHAR 64 NOT NUL
  • Vue.js全家桶仿哔哩哔哩动画 (移动端APP)

    项目地址 由于项目是移动端 电脑访问时可以切换成手机端 播放页面其实没有根据B站移动端来 比较粗糙 源码地址 欢迎Star 在线预览 项目描述 前端部分 实现的Swiper Toast Indicator组件 来自Mint ui 使用了Vu
  • 【HDFS】EditLogTailer功能及原理(二)-- selectInputStreams细节详解

    HDFS EditLogTailer功能及原理 一 整体流程 HDFS EditLogTailer功能及原理 二 selectInputStreams细节详解 HDFS EditLogTailer功能及原理 三 loadEdits方法细节详
  • Javascript变量提升预解析的理解

    预解析 JavaScript代码的执行是由浏览器中的JavaScript解析器来执行的 JavaScript解析器执行JavaScript代码的时候 分为两个过程 预解析过程和代码执行过程 预解析过程 把变量的声明提升到当前作用域的最前面
  • 使用python的pandas库把.data文件转化为csv文件

    1 问题引入 在数据分析 机器学习 深度学习中 我们经常会处理各种各样格式的数据 今天 博主在做房价预测时 采用波士顿房价数据集 从网上下载的数据集格式为 data 并不是我们喜闻乐见的csv格式 所以想采用pandas库将其转为为csv格
  • 【Redis】Redis 的学习教程(十)之使用 Redis 实现消息队列

    消息队列需要满足的要求 顺序一致 要保证消息发送的顺序和消费的顺序是一致的 不一致的话可能会导致业务上的错误 消息确认机制 对于一个已经被消费的消息 已经收到ACK 不能再次被消费 消息持久化 要具有持久化的能力 避免消息丢失 这样当消费者

随机推荐

  • linux怎么将磁盘剩余空间给分区,利用fdisk将硬盘剩余空间进行分区

    1 首先查看分区 发现300多G的硬盘 dev sdc1只使用了200多G而已 root local103 dbbackup df h Filesystem Size Used Avail Use Mounted on dev sda2 1
  • [黑科技] 使用Word和Excel自制题库自判断答题系统

    这篇文章是LZY老师告诉我的一个方法 如果你需要做题库 并且喜欢电子答题的方法 这篇文章或许会对你有所帮助 反正李老师班级的平均成绩高出其他班级的14分 这就是它的好处 希望这篇文章对我今后的学生有所帮助吧 注意 这篇文章涉及到Word特殊
  • 详解分布式共识(一致性)算法Raft

    分布式共识及Raft简介 所谓分布式共识 consensus 与CAP理论中的一致性 consistency 其实是异曲同工 就是在分布式系统中 所有节点对同一份数据的认知能够达成一致 保证集群共识的算法就叫共识算法 它与一致性协议这个词也
  • PyTorch 更改训练好的模型 继续训练

    目录 直接加载预训练模型 加载部分预训练模型 冻结部分参数 训练另一部分参数 微改基础模型预训练 微改基础模型 简单预训练 直接加载预训练模型 如果我们使用的模型和原模型完全一样 那么我们可以直接加载别人训练好的模型 my resnet M
  • SHADER学习笔记(一):Surface Shader

    Surface Shader是Unity为了方便shader编写提供的特殊功能 它对底层的vertex fragment shader做了封装 省去了一些重复代码编写的工作量 我的理解是它同时具有vertex fragment shader
  • [CISCN 2022 初赛]login_normal

    叠甲 菜 很菜 就是懂一点但是不多 可能涉及的错误会很多 有错误欢迎指出 同时对于这个疑问有解答的也欢迎留言 总之就是很菜 QAQ 这一道题 首先要考代码审计 来猜它这个 login 的格式 然后在通过它的 login 之后 通过传入可见字
  • 【Android】ViewModel原理分析

    概述 本文主要通过分析ViewModel源码解决以下两个疑问 1 ViewModel如何保证的唯一性 2 ViewModel如何保证数据不丢失的 为了解决这些问题 从ViewModel的构造开始 一般创建ViewModel的方法如下 Vie
  • 《消息队列高手课》内存管理:如何避免内存溢出和频繁的垃圾回收?

    不知道你有没有发现 在高并发 高吞吐量的极限情况下 简单的事情就会变得没有那么简单了 一个业务逻辑非常简单的微服务 日常情况下都能稳定运行 为什么一到大促就卡死甚至进程挂掉 再比如 一个做数据汇总的应用 按照小时 天这样的粒度进行数据汇总都
  • SQL Server用户登录失败

    SQL Server数据库中 如果我们忘记了 sa密码 又删除了jhyf kj administrators帐号 我们可以用下面的方法来修复 1 首先停止所有与SQLServer相关的服务 net stop SQL Server Integ
  • Spring Boot全面总结(超详细,建议收藏)

    前言 本文非常长 建议先mark后看 也许是最后一次写这么长的文章 说明 前面有4个小节关于Spring的基础知识 分别是 IOC容器 JavaConfig 事件监听 SpringFactoriesLoader详解 它们占据了本文的大部分内
  • 2021极客大挑战web部分wp

    Dark 看到url http c6h35nlkeoew5vzcpsacsidbip2ezotsnj6sywn7znkdtrbsqkexa7yd onion 发现后缀为 onion 为洋葱 下载后使用洋葱游览器访问 Welcome2021
  • git学习:github上传自己的代码到别人的仓库

    转载 原博客链接 总结 向别人贡献自己的代码 和传到自己仓库的区别 要先fork转化 clone仓库文件到电脑本地 然后进入文件夹 若想提交到非默认分支 要先git checkout到分支 pull分支下的最新代码 若还想创建新分支 用gi
  • 入門篇-耦合Coupling AC/DC/GND差別在哪

    摘自 https www strongpilab com p 156 示波器操作 入門篇 耦合Coupling AC DC GND差別在哪 2016 06 26 儀器 Instrument 示波器 Scope 0 示波器的Vertical選
  • Crested Ibis vs Monster——AT动态规划思想

    题目描述 Ibis is fighting with a monster The health of the monster is H Ibis can cast N kinds of spells Casting the i th spe
  • 对caffe2的一些初步体会(草稿)

    Caffe2的一些关键设计思想 所有运算都抽象为Operator Blob和Tensor的概念 Blob和Net都存放在Workspace中 一个Workspace中可以有多个Net 这些Net中使用到的相同名称的Blob实际对应于这个Wo
  • 图数据库nebula

    目录 1 查询方式 按需不需要基于索引查询 可以分为两类 为什么有的需要索引 go 依据路径查询属性 fetch 获取指定边 点的属性值 lookup match 1 查询方式 nebula可以用来查询的语句关键字主要有 GO FETCH
  • virt与virsh常用命令

    前提 客户机虚拟机上配置qemu guest agent 并对guest的xml配置文件做一些修改 那么就可以使用很多特有的命令 对虚拟机进行配置 例如 修改虚拟机密码 root localhost virsh set user passw
  • Excel工具类

    目录 1导入导出 2测试 2 1导入测试 2 1 1JSON导入 2 1 2对象导入 2 2导出测试 2 2 1导出模版 2 2 2导出用户表 3依赖 4工具包 1导入导出 UserImport package com excel enti
  • 关于嵌入式系统的学习路线图

    来源 本文乃同济大学软件学院王院长 JacksonWan 在同济网论坛发表的帖子 谈谈软件学院高年级同学的学习方向 的第二部分 三部分依次为 一 关于企业计算方向 二 关于嵌入式系统方向 三 关于游戏软件方向 嵌入式系统方向 嵌入式系统无疑
  • Java加密技术(九)——初探SSL

    在 Java加密技术 八 中 我们模拟了一个基于RSA非对称加密网络的安全通信 现在我们深度了解一下现有的安全网络通信 SSL 我们需要构建一个由CA机构签发的有效证书 这里我们使用上文中生成的自签名证书 zlex cer 这里 我们将证书