From a02d930f110815641b96eb9c167c05a4dac7168c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Wed, 2 Mar 2022 11:28:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E5=8A=9F=E8=83=BD=EF=BC=9BEncrpt=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=BAEncrypt=EF=BC=9B=E6=B7=BB=E5=8A=A0=E5=A1=AB=E5=85=85?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Others/sql/flashsale.sql | 8 +- README.md | 22 --- frontend/getotp.html | 20 +++ frontend/register.html | 132 ++++++++++++++++++ .../flashsale/controller/UserController.java | 24 +++- .../flashsale/dataobject/UserPasswordDO.java | 20 +-- .../service/impl/UserServiceImpl.java | 11 +- src/main/resources/application.properties | 2 +- src/main/resources/mapping/UserDOMapper.xml | 2 +- .../mapping/UserPasswordDOMapper.xml | 22 +-- 10 files changed, 204 insertions(+), 59 deletions(-) create mode 100644 frontend/register.html diff --git a/Others/sql/flashsale.sql b/Others/sql/flashsale.sql index f39117c..ac3997b 100644 --- a/Others/sql/flashsale.sql +++ b/Others/sql/flashsale.sql @@ -11,7 +11,7 @@ Target Server Version : 50726 File Encoding : 65001 - Date: 01/03/2022 17:01:05 + Date: 02/03/2022 10:30:12 */ SET NAMES utf8mb4; @@ -42,13 +42,13 @@ INSERT INTO `user_info` VALUES (1, 'admin', 2, 18, '110', 'phone', ''); -- ---------------------------- DROP TABLE IF EXISTS `user_password`; CREATE TABLE `user_password` ( - `id` int(11) NOT NULL, - `encrpt_password` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `id` int(11) NOT NULL AUTO_INCREMENT, + `encrypt_password` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', `user_id` int(11) NOT NULL DEFAULT 0, PRIMARY KEY (`id`) USING BTREE, INDEX `user_id`(`user_id`) USING BTREE, CONSTRAINT `user_password_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user_info` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT -) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; +) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of user_password diff --git a/README.md b/README.md index 21505c8..110958f 100644 --- a/README.md +++ b/README.md @@ -12,28 +12,6 @@ > 用户名密码均为 `admin` -### 本次修改通过以下方式查看👇 - -> http://localhost:8090/user/getotp?telephone=1 - -预期效果: - -```json -{"data":null,"status":"success"} -``` - -控制台: -```sql -telephone = 1 & otpCode = 90824 -``` - -> http://localhost:8090/user/get?id=2 - -预期效果: - -```json -{"data":{"errCode":10002,"errMsg":"未知错误"},"status":"fail"} -``` ### 层次结构(以User为例,自上向下) | | 目录 | Java对象类型 | 说明 | 举例 | diff --git a/frontend/getotp.html b/frontend/getotp.html index 1961327..61a1867 100644 --- a/frontend/getotp.html +++ b/frontend/getotp.html @@ -21,6 +21,12 @@ + 用户注册 + +
+ @@ -39,9 +45,11 @@ data: { "telephone": telephone, }, + xhrFields: {withCredentials: true}, success: function (data) { if (data.status == "success") { alert("OTP已经发送到了您的手机上,请注意查收"); + window.location.href = "./register.html"; } else { alert("OTP发送失败,原因为" + data.data.errMsg); } @@ -51,6 +59,18 @@ } }) }) + + $("#fillData").on("click", function () { + $("#telephone").val("18900000001"); + $("#telephone").attr("disabled", true); + // 屏蔽弹窗 + alert = function () {}; + $("#getotp").click(); + }) + + if(location.search=="?quickDebug") { + $("#fillData").click(); + } }) diff --git a/frontend/register.html b/frontend/register.html new file mode 100644 index 0000000..352d9ec --- /dev/null +++ b/frontend/register.html @@ -0,0 +1,132 @@ + + + + + Title + + + + + + +
+

用户注册

+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ + 获取验证码 +
+
+ + + + \ No newline at end of file diff --git a/src/main/java/com/cxyxiaomo/flashsale/controller/UserController.java b/src/main/java/com/cxyxiaomo/flashsale/controller/UserController.java index b9efd65..245ff05 100644 --- a/src/main/java/com/cxyxiaomo/flashsale/controller/UserController.java +++ b/src/main/java/com/cxyxiaomo/flashsale/controller/UserController.java @@ -6,19 +6,22 @@ import com.cxyxiaomo.flashsale.error.EmBusinessError; import com.cxyxiaomo.flashsale.response.CommonReturnType; import com.cxyxiaomo.flashsale.service.UserService; import com.cxyxiaomo.flashsale.service.model.UserModel; -import org.apache.tomcat.util.security.MD5Encoder; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; +import sun.misc.BASE64Encoder; import javax.servlet.http.HttpServletRequest; +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.Random; @Controller("user") // 允许被SpringBoot扫描到 @RequestMapping("/user") // 通过 "/user" 访问到 -@CrossOrigin // 允许跨域 +@CrossOrigin(allowCredentials = "true", allowedHeaders = "*") // 允许跨域 public class UserController extends BaseController { @Autowired @@ -35,26 +38,35 @@ public class UserController extends BaseController { @RequestParam(name = "name") String name, @RequestParam(name = "gender") Integer gender, @RequestParam(name = "age") Integer age, - @RequestParam(name = "password") String password) throws BusinessException { + @RequestParam(name = "password") String password) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException { // 验证手机号和对应的otpcode相符合 String inSessionOtpCode = (String) this.httpServletRequest.getSession().getAttribute(telephone); - if (com.alibaba.druid.util.StringUtils.equals(otpCode, inSessionOtpCode)) { + if (!com.alibaba.druid.util.StringUtils.equals(otpCode, inSessionOtpCode)) { throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "短信验证码不符合"); } // 用户的注册流程 UserModel userModel = new UserModel(); userModel.setName(name); - userModel.setGender(gender); + userModel.setGender(new Byte(String.valueOf(gender.intValue()))); userModel.setAge(age); userModel.setTelephone(telephone); userModel.setRegisterMode("phone"); - userModel.setEncryptPassword(MD5Encoder.encode(password.getBytes())); + userModel.setEncryptPassword(this.EncodeByMD5(password)); userService.register(userModel); return CommonReturnType.create(null); } + public String EncodeByMD5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException { + // 确定一个计算方法 + MessageDigest md5 = MessageDigest.getInstance("MD5"); + BASE64Encoder base64Encoder = new BASE64Encoder(); + // 加密字符串 + String newstr = base64Encoder.encode(md5.digest(str.getBytes("utf-8"))); + return newstr; + } + // 用户获取OTP短信接口 @RequestMapping(value = "/getotp", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED}) @ResponseBody diff --git a/src/main/java/com/cxyxiaomo/flashsale/dataobject/UserPasswordDO.java b/src/main/java/com/cxyxiaomo/flashsale/dataobject/UserPasswordDO.java index 11a1a09..ddcd05b 100644 --- a/src/main/java/com/cxyxiaomo/flashsale/dataobject/UserPasswordDO.java +++ b/src/main/java/com/cxyxiaomo/flashsale/dataobject/UserPasswordDO.java @@ -13,11 +13,11 @@ public class UserPasswordDO { /** * * This field was generated by MyBatis Generator. - * This field corresponds to the database column user_password.encrpt_password + * This field corresponds to the database column user_password.encrypt_password * * @mbg.generated Tue Mar 01 14:58:29 CST 2022 */ - private String encrptPassword; + private String encryptPassword; /** * @@ -54,26 +54,26 @@ public class UserPasswordDO { /** * This method was generated by MyBatis Generator. - * This method returns the value of the database column user_password.encrpt_password + * This method returns the value of the database column user_password.encrypt_password * - * @return the value of user_password.encrpt_password + * @return the value of user_password.encrypt_password * * @mbg.generated Tue Mar 01 14:58:29 CST 2022 */ - public String getEncrptPassword() { - return encrptPassword; + public String getEncryptPassword() { + return encryptPassword; } /** * This method was generated by MyBatis Generator. - * This method sets the value of the database column user_password.encrpt_password + * This method sets the value of the database column user_password.encrypt_password * - * @param encrptPassword the value for user_password.encrpt_password + * @param encryptPassword the value for user_password.encrypt_password * * @mbg.generated Tue Mar 01 14:58:29 CST 2022 */ - public void setEncrptPassword(String encrptPassword) { - this.encrptPassword = encrptPassword == null ? null : encrptPassword.trim(); + public void setEncryptPassword(String encryptPassword) { + this.encryptPassword = encryptPassword == null ? null : encryptPassword.trim(); } /** diff --git a/src/main/java/com/cxyxiaomo/flashsale/service/impl/UserServiceImpl.java b/src/main/java/com/cxyxiaomo/flashsale/service/impl/UserServiceImpl.java index d90f2d6..9029395 100644 --- a/src/main/java/com/cxyxiaomo/flashsale/service/impl/UserServiceImpl.java +++ b/src/main/java/com/cxyxiaomo/flashsale/service/impl/UserServiceImpl.java @@ -47,9 +47,12 @@ public class UserServiceImpl implements UserService { || StringUtils.isEmpty(userModel.getTelephone())) { throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR); } - UserDO userDO = new UserDO(); - userDOMapper.insertSelective(userDO); + // 实现 model -> dataobject 方法 + UserDO userDO = convertFormModel(userModel); + userDOMapper.insertSelective(userDO); + + userModel.setId(userDO.getId()); UserPasswordDO userPasswordDO = convertPasswordFormModel(userModel); userPasswordDOMapper.insertSelective(userPasswordDO); @@ -67,7 +70,7 @@ public class UserServiceImpl implements UserService { BeanUtils.copyProperties(userDO, userModel); if (userDO != null) { // 不可再使用copyProperties,因为里面id字段是重复的 - userModel.setEncryptPassword(userPasswordDO.getEncrptPassword()); + userModel.setEncryptPassword(userPasswordDO.getEncryptPassword()); } return userModel; @@ -88,7 +91,7 @@ public class UserServiceImpl implements UserService { return null; } UserPasswordDO userPasswordDO = new UserPasswordDO(); - userPasswordDO.setEncrptPassword(userModel.getEncryptPassword()); + userPasswordDO.setEncryptPassword(userModel.getEncryptPassword()); userPasswordDO.setUserId(userModel.getId()); return userPasswordDO; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 678540b..0fa7365 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -4,7 +4,7 @@ server.port=8090 mybatis.mapperLocations=classpath:mapping/*.xml spring.datasource.name=flashsale -spring.datasource.url=jdbc:mysql://127.0.0.1:3306/flashsale +spring.datasource.url=jdbc:mysql://127.0.0.1:3306/flashsale?useSSL=false spring.datasource.username=root spring.datasource.password=111111 diff --git a/src/main/resources/mapping/UserDOMapper.xml b/src/main/resources/mapping/UserDOMapper.xml index 3f81392..c93d176 100644 --- a/src/main/resources/mapping/UserDOMapper.xml +++ b/src/main/resources/mapping/UserDOMapper.xml @@ -56,7 +56,7 @@ #{age,jdbcType=INTEGER}, #{telephone,jdbcType=VARCHAR}, #{registerMode,jdbcType=VARCHAR}, #{thirdPartyId,jdbcType=VARCHAR}) - + - + @@ -17,7 +17,7 @@ This element is automatically generated by MyBatis Generator, do not modify. This element was generated on Tue Mar 01 14:58:29 CST 2022. --> - id, encrpt_password, user_id + id, encrypt_password, user_id