{"id":3843,"date":"2024-02-23T18:01:01","date_gmt":"2024-02-23T10:01:01","guid":{"rendered":""},"modified":"2024-02-23T18:01:01","modified_gmt":"2024-02-23T10:01:01","slug":"\u5f00\u53d1\u7f51\u9875\u5fae\u4fe1\u767b\u5165\u83b7\u53d6\u5fae\u4fe1\u7528\u6237\u4fe1\u606f","status":"publish","type":"post","link":"https:\/\/mushiming.com\/3843.html","title":{"rendered":"\u5f00\u53d1\u7f51\u9875\u5fae\u4fe1\u767b\u5165\u83b7\u53d6\u5fae\u4fe1\u7528\u6237\u4fe1\u606f"},"content":{"rendered":"
\u5fae\u4fe1\u516c\u4f17\u5e73\u53f0<\/p>\n
\u7528\u81ea\u5df1\u5fae\u4fe1\u767b\u5165\u53ef\u4ee5\u83b7\u53d6\u81ea\u5df1\u7684appid\u548cappsecret<\/p>\n
\nimport org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RequestParam;\nimport org.springframework.web.bind.annotation.RestController;\nimport java.security.MessageDigest;\nimport java.security.NoSuchAlgorithmException;\nimport java.util.Arrays;\n\n@RestController\n@RequestMapping(\"\/wx\")\npublic class WxTestController {\n\n private Logger log = LoggerFactory.getLogger(WxTestController.class);\n\n private String TOKEN = \"*****\";\n\n @RequestMapping(\"\/login\")\n public String login(@RequestParam(\"signature\") String signature,\n @RequestParam(\"timestamp\") String timestamp,\n @RequestParam(\"nonce\") String nonce,\n @RequestParam(\"echostr\") String echostr) {\n log.info(\"timestamp:\"+timestamp);\n log.info(\"nonce:\"+nonce);\n \/\/\u6392\u5e8f\n String sortString = sort(TOKEN, timestamp, nonce);\n \/\/\u52a0\u5bc6\n String myString = sha1(sortString);\n \/\/\u6821\u9a8c\n if (myString != null && myString != \"\" && myString.equals(signature)) {\n System.out.println(\"\u7b7e\u540d\u6821\u9a8c\u901a\u8fc7\");\n \/\/\u5982\u679c\u68c0\u9a8c\u6210\u529f\u539f\u6837\u8fd4\u56deechostr\uff0c\u5fae\u4fe1\u670d\u52a1\u5668\u63a5\u6536\u5230\u6b64\u8f93\u51fa\uff0c\u624d\u4f1a\u786e\u8ba4\u68c0\u9a8c\u5b8c\u6210\u3002\n return echostr;\n } else {\n System.out.println(\"\u7b7e\u540d\u6821\u9a8c\u5931\u8d25\");\n return \"\";\n }\n }\n\n public String sort(String token, String timestamp, String nonce) {\n String[] strArray = {token, timestamp, nonce};\n Arrays.sort(strArray);\n StringBuilder sb = new StringBuilder();\n for (String str : strArray) {\n sb.append(str);\n }\n\n return sb.toString();\n }\n\n public String sha1(String str) {\n try {\n MessageDigest digest = MessageDigest.getInstance(\"SHA-1\");\n digest.update(str.getBytes());\n byte messageDigest[] = digest.digest();\n \/\/ Create Hex String\n StringBuffer hexString = new StringBuffer();\n \/\/ \u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u4e3a \u5341\u516d\u8fdb\u5236 \u6570\n for (int i = 0; i < messageDigest.length; i++) {\n String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);\n if (shaHex.length() < 2) {\n hexString.append(0);\n }\n hexString.append(shaHex);\n }\n return hexString.toString();\n\n } catch (NoSuchAlgorithmException e) {\n e.printStackTrace();\n }\n return \"\";\n }\n\n}\n<\/code><\/pre>\n3\u3001\u4e8c\u7ea7\u57df\u540d\u5728\u90a3\u4e2ajs\u6846\u4e2d\uff0c-----\u4e0d\u5e26http\uff1a\/\/\uff01\uff01\uff01\uff01<\/strong><\/h4>\n4\u3001\u4ee3\u7801\u4e0a\u4f20\u670d\u52a1\u5668\u542f\u52a8\u9879\u76ee\uff0c\u7136\u540e\u70b9\u51fb\u63d0\u4ea4\uff0c\u63d0\u793a\u8fde\u63a5\u6210\u529f\u4fbf\u5b8c\u6210\u3002<\/strong><\/h4>\n\u63a5\u4e0b\u6765\u5c31\u662f\u9700\u8981\u767b\u5165\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u4e86<\/strong><\/span><\/h4>\n\u4e00\u5171\u5c31\u56db\u6b65\uff0c\u9644\u4e0a \u5fae\u4fe1\u5f00\u53d1\u6587\u6863\u8fde\u63a5\uff1a\u7f51\u9875\u6388\u6743 | \u5fae\u4fe1\u5f00\u653e\u6587\u6863<\/p>\n
1 \u7b2c\u4e00\u6b65\uff1a\u7528\u6237\u540c\u610f\u6388\u6743\uff0c\u83b7\u53d6code<\/p>\n
2 \u7b2c\u4e8c\u6b65\uff1a\u901a\u8fc7code\u6362\u53d6\u7f51\u9875\u6388\u6743access_token<\/p>\n
3 \u7b2c\u4e09\u6b65\uff1a\u5237\u65b0access_token\uff08\u5982\u679c\u9700\u8981\uff09<\/p>\n
4 \u7b2c\u56db\u6b65\uff1a\u62c9\u53d6\u7528\u6237\u4fe1\u606f(\u9700scope\u4e3a snsapi_userinfo)<\/p>\n
5 \u9644\uff1a\u68c0\u9a8c\u6388\u6743\u51ed\u8bc1\uff08access_token\uff09\u662f\u5426\u6709\u6548<\/p>\n
\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n
mport com.naruto.huo.util.AuthUtil;\nimport org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;\nimport org.springframework.stereotype.Controller;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\nimport javax.servlet.ServletException;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\nimport java.io.IOException;\nimport java.net.URLEncoder;\n\n@Controller\npublic class WxLoginController{\n\n private Logger log = LoggerFactory.getLogger(WxLoginController.class);\n\n \/**\n *\n *\/\n\n private static final long serialVersionUID = 1L;\n\n @RequestMapping(\"\/wxLogin\")\n protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\n\n log.info(\"\u8fdb\u5165WxLoginController\");\n \/\/\u7b2c\u4e00\u6b65\uff1a\u5f15\u5bfc\u7528\u6237\u8fdb\u5165\u6388\u6743\u9875\u9762\u540c\u610f\u6388\u6743\uff0c\u83b7\u53d6code\n \/\/\u56de\u8c03\u5730\u5740\n String backUrl = \"*****\"; \/\/\u7b2c1\u79cd\u60c5\u51b5\u4f7f\u7528\n\n \/\/ String backUrl = \"*****k\";\/\/\u7b2c2\u79cd\u60c5\u51b5\u4f7f\u7528\uff0c\u8fd9\u91cc\u662fweb.xml\u4e2d\u7684\u8def\u5f84\n\n\n \/\/\u6388\u6743\u9875\u9762\u5730\u5740\n\n String url = \"https:\/\/open.weixin.qq.com\/connect\/oauth2\/authorize?appid=\" + AuthUtil.APPID\n\n + \"&redirect_uri=\" + URLEncoder.encode(backUrl)\n\n + \"&response_type=code\"\n\n + \"&scope=snsapi_userinfo\"\n\n + \"&state=STATE#wechat_redirect\";\n\n\n \/\/\u91cd\u5b9a\u5411\u5230\u6388\u6743\u9875\u9762\n\n response.sendRedirect(url);\n\n }\n\n}\n<\/code><\/pre>\n\nimport com.naruto.huo.model.UserVx;\nimport com.naruto.huo.util.AuthUtil;\nimport net.sf.json.JSONObject;\nimport org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.jdbc.core.JdbcTemplate;\nimport org.springframework.stereotype.Controller;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.ResponseBody;\nimport org.springframework.web.servlet.ModelAndView;\n\nimport javax.servlet.ServletConfig;\nimport javax.servlet.ServletException;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\nimport java.io.IOException;\nimport java.sql.*;\nimport java.util.Date;\n\n@Controller\npublic class WxIndexController {\n\n \/**\n *\n *\/\n private Logger log = LoggerFactory.getLogger(WxIndexController.class);\n\n private static final long serialVersionUID = 1L;\n\n \/\/1. \u83b7\u53d6JDBCTemplate\u5bf9\u8c61\n @Autowired\n private JdbcTemplate jdbcTemplate;\n\n\n private String dbUrl;\n\n private String driverClassName;\n\n private String userName;\n\n private String passWord;\n\n\n private Connection conn = null;\n\n private PreparedStatement ps = null;\n\n private ResultSet rs = null;\n\n\n \/\/\u521d\u59cb\u5316\u6570\u636e\u5e93\n public void init(ServletConfig config) throws ServletException {\n\n\n \/\/\u52a0\u8f7d\u9a71\u52a8\n\n try {\n\n this.dbUrl = config.getInitParameter(\"dbUrl\");\n\n this.driverClassName = config.getInitParameter(\"driverClassName\");\n\n this.userName = config.getInitParameter(\"userName\");\n\n this.passWord = config.getInitParameter(\"passWord\");\n\n Class.forName(driverClassName);\n\n } catch (ClassNotFoundException e) {\n\n \/\/ TODO Auto-generated catch block\n\n e.printStackTrace();\n\n }\n\n }\n\n @RequestMapping(\"\/callBack\")\n protected ModelAndView callBack(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\n\n log.info(\"\u8fdb\u5165\u83b7\u53d6code\u540e\u53f0\");\n \/\/\u7b2c\u4e8c\u6b65\uff1a\u901a\u8fc7code\u6362\u53d6\u7f51\u9875\u6388\u6743access_token\n\n\n \/\/\u4ecerequest\u91cc\u9762\u83b7\u53d6code\u53c2\u6570(\u5f53\u5fae\u4fe1\u670d\u52a1\u5668\u8bbf\u95ee\u56de\u8c03\u5730\u5740\u7684\u65f6\u5019\uff0c\u4f1a\u628acode\u53c2\u6570\u4f20\u9012\u8fc7\u6765)\n\n String code = request.getParameter(\"code\");\n\n\n System.out.println(\"code:\" + code);\n\n\n \/\/\u83b7\u53d6code\u540e\uff0c\u8bf7\u6c42\u4ee5\u4e0b\u94fe\u63a5\u83b7\u53d6access_token\n\n String url = \"https:\/\/api.weixin.qq.com\/sns\/oauth2\/access_token?appid=\" + AuthUtil.APPID\n\n + \"&secret=\" + AuthUtil.APPSECRET\n\n + \"&code=\" + code\n\n + \"&grant_type=authorization_code\";\n\n\n \/\/\u901a\u8fc7\u7f51\u7edc\u8bf7\u6c42\u65b9\u6cd5\u6765\u8bf7\u6c42\u4e0a\u9762\u8fd9\u4e2a\u63a5\u53e3\n\n JSONObject jsonObject = AuthUtil.doGetJson(url);\n\n\n System.out.println(\"==========================jsonObject\" + jsonObject);\n\n\n \/\/\u4ece\u8fd4\u56de\u7684JSON\u6570\u636e\u4e2d\u53d6\u51faaccess_token\u548copenid\uff0c\u62c9\u53d6\u7528\u6237\u4fe1\u606f\u65f6\u7528\n\n String token = jsonObject.getString(\"access_token\");\n System.out.println(\"\u7b2c\u4e00\u6b21\u7684token:\"+token);\n String openid = jsonObject.getString(\"openid\");\n String refresh_token = jsonObject.getString(\"refresh_token\");\n\n \/\/ \u7b2c\u4e09\u6b65\uff1a\u5237\u65b0access_token\uff08\u5982\u679c\u9700\u8981\uff09\n JSONObject jsonObjectRefresh = AuthUtil.getRefreshToken(refresh_token);\n \/\/ \u7b2c\u56db\u6b65\uff1a\u62c9\u53d6\u7528\u6237\u4fe1\u606f(\u9700scope\u4e3a snsapi_userinfo)\n System.out.println(\"==========================jsonObjectRefresh\" + jsonObjectRefresh);\n String tokenR = jsonObject.getString(\"access_token\");\n String openidR = jsonObject.getString(\"openid\");\n String infoUrl = \"https:\/\/api.weixin.qq.com\/sns\/userinfo?access_token=\" + tokenR\n + \"&openid=\" + openidR\n + \"&lang=zh_CN\";\n\n \/\/\u901a\u8fc7\u7f51\u7edc\u8bf7\u6c42\u65b9\u6cd5\u6765\u8bf7\u6c42\u4e0a\u9762\u8fd9\u4e2a\u63a5\u53e3\n\n JSONObject userInfo = AuthUtil.doGetJson(infoUrl);\n\n \/\/\u5b58\u50a8\u6b64\u7528\u6237\n UserVx userVx = new UserVx();\n userVx.setOpenId((String)userInfo.get(\"openid\"));\n userVx.setPassword(\"123456\");\n userVx.setUpdateTime(new Date());\n userVx.setCreateTime(new Date());\n log.info(\"\u83b7\u53d6\u5230openid:\"+(String)userInfo.get(\"openid\"));\n Integer count = jdbcTemplate.queryForObject(\"select count(1) from user_wx where openid = ?\",Integer.class,new Object[]{(String)userInfo.get(\"openid\")});\n log.info(\"\u6b64\u7528\u6237\u5b58\u5728\u51e0\u4e2a\uff1a\"+count);\n if(count == 0){\n String sql = \"insert into user_wx (openid,password,create_time,update_time) values (?,?,?,?)\";\n jdbcTemplate.update(sql,(String)userInfo.get(\"openid\"),\"123456\",new Date(),new Date());\n log.info(\"\u63d2\u5165\u4e00\u6761\u6570\u636e\u6210\u529f\");\n }else{\n String sql = \"update user_wx update_time = ? where openid = ?\";\n jdbcTemplate.update(sql,new Date(),(String)userInfo.get(\"openid\"));\n }\n\n \/\/\u7b2c1\u79cd\u60c5\u51b5\uff1a\u4f7f\u7528\u5fae\u4fe1\u7528\u6237\u4fe1\u606f\u76f4\u63a5\u767b\u5f55\uff0c\u65e0\u9700\u6ce8\u518c\u548c\u7ed1\u5b9a\n\n request.setAttribute(\"info\", userInfo);\n \/\/\u76f4\u63a5\u8df3\u8f6c\n\/\/ request.getRequestDispatcher(\"\/index1Wx.html\").forward(request, response);\n ModelAndView mv = new ModelAndView();\n mv.setViewName(\"index1Wx\");\n return mv;\n \/\/\u7b2c2\u79cd\u60c5\u51b5\uff1a \u5c06\u5fae\u4fe1\u4e0e\u5f53\u524d\u7cfb\u7edf\u7684\u8d26\u53f7\u8fdb\u884c\u7ed1\u5b9a(\u9700\u5c06\u7b2c1\u79cd\u60c5\u51b5\u548c@WebServlet(\"\/callBack\")\u6ce8\u91ca\u6389)\n\n \/\/\u7b2c\u4e00\u6b65\uff0c\u6839\u636e\u5f53\u524dopenid\u67e5\u8be2\u6570\u636e\u5e93\uff0c\u770b\u662f\u5426\u8be5\u8d26\u53f7\u5df2\u7ecf\u8fdb\u884c\u7ed1\u5b9a\n\n \/* try {\n\n String nickname = getNickName(openid);\n\n if (!\"\".equals(nickname)) {\n\n \/\/\u5df2\u7ed1\u5b9a\n\n request.setAttribute(\"nickname\", nickname);\n\n request.getRequestDispatcher(\"\/indexWx2.html\").forward(request, response);\n\n } else {\n\n \/\/\u672a\u7ed1\u5b9a\n\n request.setAttribute(\"openid\", openid);\n\n request.getRequestDispatcher(\"\/loginWx.html\").forward(request, response);\n\n }\n\n } catch (SQLException e) {\n\n \/\/ TODO Auto-generated catch block\n\n e.printStackTrace();\n\n }*\/\n\n\n }\n\n\n \/\/\u6570\u636e\u5e93\u7684\u67e5\u8be2\n\n public String getNickName(String openid) throws SQLException {\n\n String nickName = \"\";\n\n \/\/\u521b\u5efa\u6570\u636e\u5e93\u94fe\u63a5\n\n conn = DriverManager.getConnection(dbUrl, userName, passWord);\n\n String sql = \"select nickname from user where openid = ?\";\n\n ps = conn.prepareStatement(sql);\n\n ps.setString(1, openid);\n\n rs = ps.executeQuery();\n\n while (rs.next()) {\n\n nickName = rs.getString(\"nickname\");\n\n }\n\n\n \/\/\u5173\u95ed\u94fe\u63a5\n\n rs.close();\n\n ps.close();\n\n conn.close();\n\n\n return nickName;\n\n }\n\n\n \/\/\u6570\u636e\u5e93\u7684\u4fee\u6539(openid\u7684\u7d81\u5b9a)\n\n public int updateUser(String account, String password, String openid) throws SQLException {\n\n\n \/\/\u521b\u5efa\u6570\u636e\u5e93\u94fe\u63a5\n\n conn = DriverManager.getConnection(dbUrl, userName, passWord);\n\n String sql = \"update user set openid = ? where account = ? and password = ?\";\n\n ps = conn.prepareStatement(sql);\n\n ps.setString(1, openid);\n\n ps.setString(2, account);\n\n ps.setString(3, password);\n\n int temp = ps.executeUpdate();\n\n\n \/\/\u5173\u95ed\u94fe\u63a5\n\n rs.close();\n\n ps.close();\n\n conn.close();\n\n\n return temp;\n\n }\n\/*\n\n \/\/post\u65b9\u6cd5\uff0c\u7528\u6765\u63a5\u53d7\u767b\u5f55\u8bf7\u6c42\n protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\n\n\n String account = request.getParameter(\"account\");\n\n String password = request.getParameter(\"password\");\n\n String openid = request.getParameter(\"openid\");\n\n\n try {\n\n int temp = updateUser(account, password, openid);\n\n\n if (temp > 0) {\n\n String nickname = getNickName(openid);\n\n request.setAttribute(\"nickname\", nickname);\n\n request.getRequestDispatcher(\"\/indexWx2.html\").forward(request, response);\n\n System.out.println(\"\u8d26\u53f7\u7ed1\u5b9a\u6210\u529f\");\n\n } else {\n\n System.out.println(\"\u8d26\u53f7\u7ed1\u5b9a\u5931\u8d25\");\n\n }\n\n\n } catch (SQLException e) {\n\n \/\/ TODO Auto-generated catch block\n\n e.printStackTrace();\n\n }\n\n\n }*\/\n\n\n}\n<\/code><\/pre>\n\u6210\u529f\u540e\uff1a\u626b\u7801\u5373\u53ef\u83b7\u53d6\u4fe1\u606f<\/h2>\n","protected":false},"excerpt":{"rendered":"\u5f00\u53d1\u7f51\u9875\u5fae\u4fe1\u767b\u5165\u83b7\u53d6\u5fae\u4fe1\u7528\u6237\u4fe1\u606f1\u3001\u5148\u8fdb\u5165\u5fae\u4fe1\u516c\u4f17\u53f7\u6d4b\u8bd5\u5e73\u53f0\uff08\u586b\u5199\u8fd9\u4e9b\u7684\u65f6\u5019\u9700\u8981\u4e70\u57df\u540d\uff0c\u6211\u4e70\u7684\u662f\u963f\u91cc\u4e91\u57df\u540d\uff09\uff1ahttps:\/\/...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/3843"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=3843"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/3843\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=3843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=3843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=3843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}