{"id":3240,"date":"2018-10-18T16:04:24","date_gmt":"2018-10-18T08:04:24","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3240"},"modified":"2018-10-18T16:04:24","modified_gmt":"2018-10-18T08:04:24","slug":"java%e9%9a%8f%e6%9c%ba%e6%95%b0%e7%94%9f%e6%88%90%e5%b7%a5%e5%85%b7%e7%b1%bbrandomstringutils%e4%bd%bf%e7%94%a8%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3240","title":{"rendered":"java\u968f\u673a\u6570\u751f\u6210\u5de5\u5177\u7c7bRandomStringUtils\u4f7f\u7528\u6559\u7a0b"},"content":{"rendered":"<p>\n\t\u4e00\u4e2a\u6846\u67b6\uff0c\u6211\u4eec\u4e00\u822c\u5e38\u7528\u7684 API \u53ef\u80fd\u8fde 50% \u90fd\u4e0d\u5230\uff01\u8fd9\u4e0d\u662f\u5938\u5f20\uff0c\u8fd9\u662f\u771f\u662f\u60c5\u51b5\uff01\n<\/p>\n<p>\n\t\u6700\u8fd1\u9879\u76ee\u4e2d\u9700\u8981\u7528\u5230\u968f\u673a\u6570\uff0c\u7f51\u4e0a\u641c\u4e86\u4e00\u4e0b\uff0c\u53d1\u73b0&nbsp;RandomStringUtils \u8fd9\u4e2a\u5de5\u5177\u7c7b\u975e\u5e38\u7684\u4e0d\u9519\u3002\u800c\u8fd9\u4e2a\u7c7b\u5462\uff1f\u5c31\u5728\u6211\u4eec\u7684\u6700\u5e38\u7528\u7684&nbsp;Apache Commons Lang (org.apache.commons.lang3.RandomStringUtils)\u5305\u4e2d\uff0c\u8fd9\u771f\u662f\u719f\u6089\u800c\u53c8\u964c\u751f\u7684\u77e5\u8bc6\u3002\u672c\u6587\u603b\u7ed3\u4e00\u4e0b\u5b83\u7684\u8be6\u7ec6\u7528\u6cd5\uff01\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"java\u968f\u673a\u6570\u751f\u6210\u5de5\u5177\u7c7bRandomStringUtils\u4f7f\u7528\u6559\u7a0b\" src=\"http:\/\/www.xttblog.com\/wp-content\/uploads\/2016\/02\/yh.png\" style=\"width: 512px;height: 340px\" \/>\n<\/p>\n<p>\n\tRandomStringUtils\u5de5\u5177\u7c7b\u5927\u6982\u4e00\u5171\u6709\u5341\u51e0\u4e2a\u65b9\u6cd5\uff0c\u4f46\u662f\u5e38\u7528\u7684\u5927\u6982\u4e5f\u5c31\u56db\u4e94\u4e2a\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\n\/\/\u4ea7\u751f5\u4f4d\u957f\u5ea6\u7684\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u4e2d\u6587\u73af\u5883\u4e0b\u662f\u4e71\u7801\r\nRandomStringUtils.random(5);\r\n\/\/\u4f7f\u7528\u6307\u5b9a\u7684\u5b57\u7b26\u751f\u62105\u4f4d\u957f\u5ea6\u7684\u968f\u673a\u5b57\u7b26\u4e32\r\nRandomStringUtils.random(5, new char[]{&#039;a&#039;,&#039;b&#039;,&#039;c&#039;,&#039;d&#039;,&#039;e&#039;,&#039;f&#039;, &#039;1&#039;, &#039;2&#039;, &#039;3&#039;});\r\n\/\/\u751f\u6210\u6307\u5b9a\u957f\u5ea6\u7684\u5b57\u6bcd\u548c\u6570\u5b57\u7684\u968f\u673a\u7ec4\u5408\u5b57\u7b26\u4e32\r\nRandomStringUtils.randomAlphanumeric(5);\r\n\/\/\u751f\u6210\u968f\u673a\u6570\u5b57\u5b57\u7b26\u4e32\r\nRandomStringUtils.randomNumeric(5);\r\n\/\/\u751f\u6210\u968f\u673a[a-z]\u5b57\u7b26\u4e32\uff0c\u5305\u542b\u5927\u5c0f\u5199\r\nRandomStringUtils.randomAlphabetic(5);\r\n\/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n\/\/\u751f\u6210\u4eceASCII 32\u5230126\u7ec4\u6210\u7684\u968f\u673a\u5b57\u7b26\u4e32 \r\nRandomStringUtils.randomAscii(4);<\/pre>\n<p>\n\tRandomStringUtils \u7c7b\uff0c\u975e\u5e38\u7684\u7b80\u5355\u975e\u5e38\u7684\u65b9\u4fbf\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\n\/**\r\n * count \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u5176\u957f\u5ea6\u662f\u6307\u5b9a\u7684\u5b57\u7b26\u6570,\u5b57\u7b26\u5c06\u4ece\u53c2\u6570\u7684\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u96c6\u4e2d\u9009\u62e9\uff0c\u5982\u53c2\u6570\u6240\u793a\u3002\r\n * letters true,\u751f\u6210\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5305\u62ec\u5b57\u6bcd\u5b57\u7b26\r\n * numbers true,\u751f\u6210\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5305\u542b\u6570\u5b57\u5b57\u7b26\r\n *\/\r\nString random = RandomStringUtils.random(15, true, false);\r\nSystem.out.println(random);\r\n\r\n\/**\r\n * \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u5176\u957f\u5ea6\u662f\u6307\u5b9a\u7684\u5b57\u7b26\u6570\u3002\r\n * \u5c06\u4ece\u6240\u6709\u5b57\u7b26\u96c6\u4e2d\u9009\u62e9\u5b57\u7b26\r\n *\/\r\nrandom = RandomStringUtils.random(22);\r\nSystem.out.println(random);\r\n\r\n\/**\r\n * \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u5176\u957f\u5ea6\u662f\u6307\u5b9a\u7684\u5b57\u7b26\u6570\u3002\r\n * \u5b57\u7b26\u5c06\u4ece\u5b57\u7b26\u4e32\u6307\u5b9a\u7684\u5b57\u7b26\u96c6\u4e2d\u9009\u62e9\uff0c\u4e0d\u80fd\u4e3a\u7a7a\u3002\u5982\u679cNULL\uff0c\u5219\u4f7f\u7528\u6240\u6709\u5b57\u7b26\u96c6\u3002\r\n *\/\r\nrandom = RandomStringUtils.random(15, &quot;abcdefgABCDEFG123456789&quot;);\r\nSystem.out.println(random);\r\n\r\n\/**\r\n * \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u5176\u957f\u5ea6\u662f\u6307\u5b9a\u7684\u5b57\u7b26\u6570,\u5b57\u7b26\u5c06\u4ece\u53c2\u6570\u7684\u5b57\u6bcd\u6570\u5b57\u5b57\u7b26\u96c6\u4e2d\u9009\u62e9\uff0c\u5982\u53c2\u6570\u6240\u793a\u3002\r\n * count:\u8ba1\u7b97\u521b\u5efa\u7684\u968f\u673a\u5b57\u7b26\u957f\u5ea6\r\n * start:\u5b57\u7b26\u96c6\u5728\u5f00\u59cb\u65f6\u7684\u4f4d\u7f6e\r\n * end:\u5b57\u7b26\u96c6\u5728\u7ed3\u675f\u524d\u7684\u4f4d\u7f6e\uff0c\u5fc5\u987b\u5927\u4e8e65\r\n * letters true,\u751f\u6210\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5305\u62ec\u5b57\u6bcd\u5b57\u7b26\r\n * numbers true,\u751f\u6210\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5305\u542b\u6570\u5b57\u5b57\u7b26\r\n * \r\n *\/\r\nrandom = RandomStringUtils.random(1009, 5, 129, true, true);\r\n\r\n\/**\r\n * \u4ea7\u751f\u4e00\u4e2a\u957f\u5ea6\u4e3a\u6307\u5b9a\u7684\u968f\u673a\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u6570\uff0c\u5b57\u7b26\u5c06\u4ece\u62c9\u4e01\u5b57\u6bcd\uff08a-z\u3001A-Z\u7684\u9009\u62e9\uff09\u3002\r\n * count:\u521b\u5efa\u968f\u673a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\r\n *\/\r\nrandom = RandomStringUtils.randomAlphabetic(15);\r\n\r\n\/**\r\n * \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u5176\u957f\u5ea6\u4ecb\u4e8e\u5305\u542b\u6700\u5c0f\u503c\u548c\u6700\u5927\u6700\u5927\u503c\u4e4b\u95f4,\uff0c\u5b57\u7b26\u5c06\u4ece\u62c9\u4e01\u5b57\u6bcd\uff08a-z\u3001A-Z\u7684\u9009\u62e9\uff09\u3002\r\n * minLengthInclusive \uff1a\u8981\u751f\u6210\u7684\u5b57\u7b26\u4e32\u7684\u5305\u542b\u6700\u5c0f\u957f\u5ea6\r\n * maxLengthExclusive \uff1a\u8981\u751f\u6210\u7684\u5b57\u7b26\u4e32\u7684\u5305\u542b\u6700\u5927\u957f\u5ea6\r\n *\/\r\nrandom = RandomStringUtils.randomAlphabetic(2, 15);\r\n\r\n\/**\r\n * \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u5176\u957f\u5ea6\u662f\u6307\u5b9a\u7684\u5b57\u7b26\u6570\uff0c\u5b57\u7b26\u5c06\u4ece\u62c9\u4e01\u5b57\u6bcd\uff08a-z\u3001A-Z\uff09\u548c\u6570\u5b570-9\u4e2d\u9009\u62e9\u3002\r\n * count \uff1a\u521b\u5efa\u7684\u968f\u673a\u6570\u957f\u5ea6\r\n *\/\r\nrandom = RandomStringUtils.randomAlphanumeric(15);\r\n\r\n \/**\r\n * \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u5176\u957f\u5ea6\u4ecb\u4e8e\u5305\u542b\u6700\u5c0f\u503c\u548c\u6700\u5927\u6700\u5927\u503c\u4e4b\u95f4,\u5b57\u7b26\u5c06\u4ece\u62c9\u4e01\u5b57\u6bcd\uff08a-z\u3001A-Z\uff09\u548c\u6570\u5b570-9\u4e2d\u9009\u62e9\u3002\r\n * minLengthInclusive \uff1a\u8981\u751f\u6210\u7684\u5b57\u7b26\u4e32\u7684\u5305\u542b\u6700\u5c0f\u957f\u5ea6\r\n * maxLengthExclusive \uff1a\u8981\u751f\u6210\u7684\u5b57\u7b26\u4e32\u7684\u5305\u542b\u6700\u5927\u957f\u5ea6\r\n * \r\n *\/\r\nrandom = RandomStringUtils.randomAlphanumeric(5, 68);\r\n\r\n\/**\r\n * \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u5176\u957f\u5ea6\u662f\u6307\u5b9a\u7684\u5b57\u7b26\u6570\uff0c\u5b57\u7b26\u5c06\u4eceASCII\u503c\u4ecb\u4e8e32\u5230126\u4e4b\u95f4\u7684\u5b57\u7b26\u96c6\u4e2d\u9009\u62e9\uff08\u5305\u62ec\uff09\r\n * count:\u968f\u673a\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\r\n *\/\r\nrandom = RandomStringUtils.randomAscii(15);\r\n\r\n\/**\r\n * \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u5176\u957f\u5ea6\u4ecb\u4e8e\u5305\u542b\u6700\u5c0f\u503c\u548c\u6700\u5927\u6700\u5927\u503c\u4e4b\u95f4,\u5b57\u7b26\u5c06\u4eceASCII\u503c\u4ecb\u4e8e32\u5230126\u4e4b\u95f4\u7684\u5b57\u7b26\u96c6\u4e2d\u9009\u62e9\uff08\u5305\u62ec\uff09\r\n * minLengthInclusive \uff1a\u8981\u751f\u6210\u7684\u5b57\u7b26\u4e32\u7684\u5305\u542b\u6700\u5c0f\u957f\u5ea6\r\n * maxLengthExclusive \uff1a\u8981\u751f\u6210\u7684\u5b57\u7b26\u4e32\u7684\u5305\u542b\u6700\u5927\u957f\u5ea6\r\n *\/\r\nrandom = RandomStringUtils.randomAscii(15, 30);\r\n\r\n\/**\r\n * \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u5176\u957f\u5ea6\u662f\u6307\u5b9a\u7684\u5b57\u7b26\u6570\uff0c\u5c06\u4ece\u6570\u5b57\u5b57\u7b26\u96c6\u4e2d\u9009\u62e9\u5b57\u7b26\u3002\r\n * count:\u751f\u6210\u968f\u673a\u6570\u7684\u957f\u5ea6\r\n *\/\r\nrandom = RandomStringUtils.randomNumeric(15);\r\n\r\n\/**\r\n * \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5b57\u7b26\u4e32\uff0c\u5176\u957f\u5ea6\u4ecb\u4e8e\u5305\u542b\u6700\u5c0f\u503c\u548c\u6700\u5927\u6700\u5927\u503c\u4e4b\u95f4,\u5c06\u4ece\u6570\u5b57\u5b57\u7b26\u96c6\u4e2d\u9009\u62e9\u5b57\u7b26.\r\n * minLengthInclusive, \u8981\u751f\u6210\u7684\u5b57\u7b26\u4e32\u7684\u5305\u542b\u6700\u5c0f\u957f\u5ea6\r\n * maxLengthExclusive \u8981\u751f\u6210\u7684\u5b57\u7b26\u4e32\u7684\u5305\u542b\u6700\u5927\u957f\u5ea6\r\n *\/\r\nrandom = RandomStringUtils.randomNumeric(15, 20);<\/pre>\n<p>\n\t\u4f7f\u7528&nbsp;RandomStringUtils \u6211\u4eec\u53ef\u4ee5\u7528\u6765\u751f\u6210 token\uff0c\u8ba2\u5355ID\uff0c\u9a8c\u8bc1\u7801\u968f\u673a\u6570\u7b49\uff0c\u80fd\u6781\u5927\u7684\u65b9\u4fbf\u6211\u4eec\u5f00\u53d1\u6548\u7387\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3240\">java\u968f\u673a\u6570\u751f\u6210\u5de5\u5177\u7c7bRandomStringUtils\u4f7f\u7528\u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a\u6846\u67b6\uff0c\u6211\u4eec\u4e00\u822c\u5e38\u7528\u7684 API \u53ef\u80fd\u8fde 50% \u90fd\u4e0d\u5230\uff01\u8fd9\u4e0d\u662f\u5938\u5f20\uff0c\u8fd9\u662f\u771f\u662f\u60c5\u51b5\uff01 \u6700\u8fd1\u9879\u76ee\u4e2d\u9700\u8981\u7528\u5230\u968f\u673a\u6570 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1720,216,961,43,859],"class_list":["post-3240","post","type-post","status-publish","format-standard","hentry","category-java","tag-randomstringutils","tag-216","tag-961","tag-43","tag-859"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3240"}],"version-history":[{"count":2,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3240\/revisions"}],"predecessor-version":[{"id":3242,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3240\/revisions\/3242"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}