{"id":201337,"date":"2025-05-10T09:11:45","date_gmt":"2025-05-10T01:11:45","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201337\/"},"modified":"2025-05-10T09:11:45","modified_gmt":"2025-05-10T01:11:45","slug":"%e5%9f%ba%e4%ba%8eredis%e5%ae%9e%e7%8e%b0token%e9%aa%8c%e8%af%81%e7%94%a8%e6%88%b7%e6%98%af%e5%90%a6%e7%99%bb%e9%99%86","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201337\/","title":{"rendered":"\u57fa\u4e8eredis\u5b9e\u73b0token\u9a8c\u8bc1\u7528\u6237\u662f\u5426\u767b\u9646"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u57fa\u4e8eredis\u5b9e\u73b0token\u9a8c\u8bc1\u7528\u6237\u662f\u5426\u767b\u9646<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u57fa\u4e8eredis\u5b9e\u73b0token\u9a8c\u8bc1\u7528\u6237\u662f\u5426\u767b\u9646\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\u767b\u9646\u3001redistoken\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p>\u57fa\u4e8e\u9879\u76ee\u9700\u6c42\uff0c \u6211\u4eec\u8981\u5b9e\u73b0\u4e00\u4e2a\u57fa\u4e8eredis\u5b9e\u73b0token\u767b\u5f55\u9a8c\u8bc1\uff0c\u8be5\u5982\u4f55\u5b9e\u73b0\u5462\uff1a<\/p>\n<p><strong>\u540e\u7aef\u5b9e\u73b0\uff1a<\/strong><\/p>\n<p>1.\u5f15\u5165redis\u76f8\u5173\u7684\u4f9d\u8d56<\/p>\n<pre>\n\n   \n    \n     org.springframework.boot\n    \n    \n     spring-boot-starter-data-redis\n    \n   \n   \n    \n     org.springframework.session\n    \n    \n     spring-session-data-redis\n    \n    \n     2.0.5.RELEASE\n    \n   <\/pre>\n<p>2.Controller\u5c42\u751f\u6210token\u4fe1\u606f\u5e76\u5b58\u5165redis\u4e2d<\/p>\n<pre>\n\/\/\u82e5\u7528\u6237\u767b\u5f55\u9a8c\u8bc1\u6210\u529f\u540e\u5c06\u5bf9\u5e94\u7684\u767b\u9646\u4fe1\u606f\u548c\u767b\u9646\u51ed\u8bc1\u4e00\u8d77\u5b58\u5165redis\u4e2d\n\/\/\u751f\u6210\u767b\u9646\u51ed\u8bc1token UUID\nString uuidToken= UUID.randomUUID().toString();\nuuidToken=uuidToken.replace(\"-\",\"\");\n \n\/\/\u5c06token\u548c\u7528\u6237\u767b\u5f55\u6001\u4e4b\u95f4\u5efa\u7acb\u8054\u7cfb\nredisTemplate.opsForValue().set(uuidToken,userModel);\nredisTemplate.expire(uuidToken,1, TimeUnit.HOURS);\n \n\/\/ \u4e0b\u53d1token\nreturn CommonReturnType.create(uuidToken);\n<\/pre>\n<p>3.\u9700\u8981\u9a8c\u8bc1\u7684\u767b\u9646\u7684\u5730\u65b9\u8c03\u7528\u5373\u53ef<\/p>\n<pre>\n String token = httpServletRequest.getParameterMap().get(\"token\")[0];\n if (StringUtils.isEmpty(token)) {\n throw new BusinessException(EmBusinessError.USER_NOT_LOGIN, \"\u7528\u6237\u8fd8\u672a\u767b\u9646,\u4e0d\u80fd\u4e0b\u5355\");\n }\n \/\/\u83b7\u53d6\u7528\u6237\u7684\u767b\u9646\u4fe1\u606f\n UserModel userModel= (UserModel) redisTemplate.opsForValue().get(token);\n if(userModel==null){\n throw new BusinessException(EmBusinessError.USER_NOT_LOGIN, \"\u7528\u6237\u8fd8\u672a\u767b\u9646,\u4e0d\u80fd\u4e0b\u5355\");\n }\n \n OrderModel orderModel = orderService.creatOrder(userModel.getId(), itemId, promoId, amount);\n return CommonReturnType.create(null);<\/pre>\n<p><strong>\u524d\u7aef\u5b9e\u73b0<\/strong><\/p>\n<p>1.\u4ece\u8fd4\u56de\u503c\u4e2d\u53d6\u51fatoken\u5e76\u5b58\u5165localstorage<\/p>\n<pre>\nif(data.status == \"success\") {\n alert(\"\u767b\u9646\u6210\u529f\");\n \/\/\u53d6\u51fatoken\u653e\u5165localstorage\n var token = data.data;\n window.localStorage[\"token\"] = token;\n window.location.href = \"listitem.html\";\n}<\/pre>\n<p>2.\u9a8c\u8bc1\u7528\u6237\u662f\u5426\u767b\u9646<\/p>\n<pre>\nvar token = window.localStorage[\"token\"];\n if(token == null){\n alert(\"\u6ca1\u6709\u767b\u5f55\uff0c\u4e0d\u80fd\u4e0b\u5355\");\n window.location.href=\"login.html\" rel=\"external nofollow\" ;\n return false;\n } <\/pre>\n<p>3.\u5f53\u7136\uff0c\u9700\u8981\u628atoken\u4f20\u5165\u540e\u7aef\u518d\u6821\u9a8c\u4e00\u6b21<\/p>\n<pre>\nurl:\"http:\/\/\"+g_host+\"\/order\/createorder?token=\"+token,<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u4e8eredis\u5b9e\u73b0token\u9a8c\u8bc1&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201337","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201337"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201337\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}