目录
一、项目前准备
1、新建项目
2、导入pom依赖
3、运行sql脚本
4、配置yml文件
5、在启动类加入注解
6、自动生成器
二、前端构建
1、templates增加相关界面
2、导入layui
3、编写login.js
4、新建PathController类(controller软件包下)
三、MD5加密
1、导入帮助包与exception包
2、前后端传值
3、登录方法:
4、密码加密
四、 全局异常抓获
1、jsr303验证
2、导入帮助包validate,异常抓获
3、在UserController类方法中加入注解
4、实现类抛出异常
5、运行测试
一、项目前准备
1、新建项目
2、导入pom依赖
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-aop
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
com.baomidou
mybatis-plus-boot-starter
3.5.1
com.baomidou
mybatis-plus-generator
3.5.2
commons-codec
commons-codec
org.apache.commons
commons-lang3
3.12.0
org.springframework.boot
spring-boot-starter-validation
org.springframework.boot
spring-boot-starter-data-redis
com.zaxxer
HikariCP
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
3、运行sql脚本
--
-- Table structure for table `t_goods`
--
DROp TABLE IF EXISTS `t_goods`;
;
;
CREATE TABLE `t_goods` (
`gid` bigint NOT NULL AUTO_INCREMENT COMMENT '商品ID',
`goods_name` varchar(16) DEFAULT NULL COMMENT '商品名称',
`goods_title` varchar(64) DEFAULT NULL COMMENT '商品标题',
`goods_img` varchar(64) DEFAULT NULL COMMENT '商品图片',
`goods_detail` longtext COMMENT '商品详情',
`goods_price` decimal(10,2) DEFAULT '0.00' COMMENT '商品价格',
`goods_stock` int DEFAULT '0' COMMENT '商品库存',
PRIMARY KEY (`gid`)
) ENGINE=InnoDB AUTO_INCREMENT=310 DEFAULT CHARSET=utf8mb3 COMMENT='商品信息表';
;
--
-- Dumping data for table `t_goods`
--
LOCK TABLES `t_goods` WRITE;
;
INSERT INTO `t_goods` VALUES (110,'小米手机_0','0o1LAfCk','/static/images/a.png','GC2F',3348.81,3150),(111,'小米手机_1','FAs','/static/images/a.png','tw4lCUHBrH',6901.60,5438),(112,'小米手机_2','rG','/static/images/a.png','ucOear5S',9820.62,8162),(113,'小米手机_3','iSLPOycDo','/static/images/a.png','IK7',3178.17,6091),(114,'小米手机_4','ZYO','/static/images/a.png','ei',8488.26,3667),(115,'小米手机_5','tKdm2fOXF','/static/images/a.png','HGlXj',5626.75,6162),(116,'小米手机_6','M','/static/images/a.png','nos',201.12,8885),(117,'小米手机_7','31T','/static/images/a.png','6bcF',5661.70,8088),(118,'小米手机_8','20SGw8VQr','/static/images/a.png','IfdHib',313.46,4),(119,'小米手机_9','fvN1s','/static/images/a.png','DRujrNOX',1526.50,5304),(120,'小米手机_10','yh0cu','/static/images/a.png','53lREj2FdZ',8999.94,2597),(121,'小米手机_11','i5kTHWFeX','/static/images/a.png','K',1662.61,6778),(122,'小米手机_12','yjPlune','/static/images/a.png','E2mH',6361.92,9336),(123,'小米手机_13','6Q','/static/images/a.png','v87vEdw',3585.59,2044),(124,'小米手机_14','6sxNLi','/static/images/a.png','52',2811.25,7051),(125,'小米手机_15','kX4s5oHmQ','/static/images/a.png','6FaUJghtc',6705.92,3931),(126,'小米手机_16','wG','/static/images/a.png','7S9',5493.15,4804),(127,'小米手机_17','qirDoootYr','/static/images/a.png','c',5336.25,8692),(128,'小米手机_18','reGMc2sU','/static/images/a.png','oh2',3608.50,8847),(129,'小米手机_19','h4ct','/static/images/a.png','9faj1RfK',5965.39,1924),(130,'小米手机_20','b12OK','/static/images/a.png','MPdQKzhOqr',8914.38,4868),(131,'小米手机_21','C3Pw','/static/images/a.png','rQtoY',9726.14,2019),(132,'小米手机_22','i34ci','/static/images/a.png','a4MV21rfr',3304.16,687),(133,'小米手机_23','mHStH87R','/static/images/a.png','g',5082.51,1670),(134,'小米手机_24','lpyx','/static/images/a.png','V',7675.51,8417),(135,'小米手机_25','o','/static/images/a.png','XH0RB',2680.58,5271),(136,'小米手机_26','m','/static/images/a.png','tgHJh1JDy',4100.02,4024),(137,'小米手机_27','VJSMI','/static/images/a.png','gsU7c',8241.24,7095),(138,'小米手机_28','UeGgfD9wp','/static/images/a.png','m2e',2952.57,8649),(139,'小米手机_29','H','/static/images/a.png','ER',7532.04,6592),(140,'小米手机_30','cJBDCH','/static/images/a.png','HkvJ5j',5824.62,6226),(141,'小米手机_31','L','/static/images/a.png','gdgPFod',2418.16,9585),(142,'小米手机_32','RcIQk','/static/images/a.png','LpngOQD',2440.95,4108),(143,'小米手机_33','Jn8','/static/images/a.png','vVL',8134.51,6632),(144,'小米手机_34','iW','/static/images/a.png','y8NV2TVL',7775.00,3051),(145,'小米手机_35','S','/static/images/a.png','RJIJvbXD',6781.55,6870),(146,'小米手机_36','Zr','/static/images/a.png','59Yjp9xI',1742.38,3917),(147,'小米手机_37','1QgD4fE3jR','/static/images/a.png','vRCBJX1Y',8754.92,2464),(148,'小米手机_38','YTEif9U8','/static/images/a.png','qRsyqzT',1406.98,7620),(149,'小米手机_39','7cg7ssi4N','/static/images/a.png','rL0oybu',7840.18,5530),(150,'小米手机_40','PtMc0pHT57','/static/images/a.png','cnzwh',5304.13,2889),(151,'小米手机_41','EQtOGY3t5w','/static/images/a.png','eX',5200.70,2749),(152,'小米手机_42','w','/static/images/a.png','iGN4F',4257.39,5322),(153,'小米手机_43','X','/static/images/a.png','zj6gWX',770.96,4016),(154,'小米手机_44','VMfup','/static/images/a.png','k',3480.28,487),(155,'小米手机_45','Qy','/static/images/a.png','7mKe',4805.12,6757),(156,'小米手机_46','a6IznSQp','/static/images/a.png','uQhM',770.74,4819),(157,'小米手机_47','LrzdrxUZ','/static/images/a.png','21QQ',6943.10,251),(158,'小米手机_48','uQO','/static/images/a.png','Hc',2445.68,4561),(159,'小米手机_49','FxiKL','/static/images/a.png','3',1275.24,493),(160,'小米手机_50','f','/static/images/a.png','i91H',6992.30,7235),(161,'小米手机_51','AnHnkQ7x9n','/static/images/a.png','SuIDRtI1lm',7602.27,9183),(162,'小米手机_52','lXQ1TQWh0','/static/images/a.png','jOBHZr5',7960.13,5244),(163,'小米手机_53','rXNovMjTp','/static/images/a.png','gdeZ2li',7609.59,6336),(164,'小米手机_54','5','/static/images/a.png','o',6233.58,4182),(165,'小米手机_55','rNdks','/static/images/a.png','mblj2l',1877.58,4780),(166,'小米手机_56','7GTK','/static/images/a.png','3iZ6THyDUS',4120.94,246),(167,'小米手机_57','YNQ','/static/images/a.png','S',6222.65,922),(168,'小米手机_58','WLj6h','/static/images/a.png','S3e8n22uu0',8662.73,4202),(169,'小米手机_59','abh5C','/static/images/a.png','3eOt6bjFp',5178.10,1561),(170,'小米手机_60','b','/static/images/a.png','IL',2410.07,4731),(171,'小米手机_61','GQl','/static/images/a.png','kgxP9',9569.25,4777),(172,'小米手机_62','a8','/static/images/a.png','VHQ',103.13,5695),(173,'小米手机_63','NN','/static/images/a.png','pHcTO',726.42,4110),(174,'小米手机_64','Ws','/static/images/a.png','v',8427.08,9850),(175,'小米手机_65','Aab1AonB','/static/images/a.png','ME9',1651.76,3132),(176,'小米手机_66','DXT','/static/images/a.png','tXgyX',3216.09,1308),(177,'小米手机_67','qz','/static/images/a.png','E',7686.70,8848),(178,'小米手机_68','Vn5pTL','/static/images/a.png','7',8650.18,8874),(179,'小米手机_69','ksF','/static/images/a.png','NbJSW',3592.45,5203),(180,'小米手机_70','PA','/static/images/a.png','PnlVsy',861.33,6171),(181,'小米手机_71','s','/static/images/a.png','zd',2804.05,8410),(182,'小米手机_72','n9r','/static/images/a.png','CQK3aGm23m',6840.12,2486),(183,'小米手机_73','E1nRAku5M','/static/images/a.png','oZ',5369.82,2962),(184,'小米手机_74','WYSgU','/static/images/a.png','kDfvc0',2205.38,6295),(185,'小米手机_75','eF','/static/images/a.png','1',4148.24,3945),(186,'小米手机_76','Y','/static/images/a.png','914oM',4325.91,6488),(187,'小米手机_77','CT750QFt','/static/images/a.png','dNe8',7848.73,4184),(188,'小米手机_78','J2lUrv6i','/static/images/a.png','o',1534.95,4717),(189,'小米手机_79','msCI0','/static/images/a.png','h',1459.57,1005),(190,'小米手机_80','hTM','/static/images/a.png','QZVSz5R',5346.83,7693),(191,'小米手机_81','XtdBhUJeO','/static/images/a.png','vkOSx',6286.26,3343),(192,'小米手机_82','glCMkI','/static/images/a.png','pk0w',5059.55,5747),(193,'小米手机_83','ocO7lht2','/static/images/a.png','u',5816.83,3866),(194,'小米手机_84','X5ZJGYm','/static/images/a.png','JQ',6013.34,5435),(195,'小米手机_85','UIc','/static/images/a.png','8QBjET4b7t',9376.66,2818),(196,'小米手机_86','ESfn','/static/images/a.png','Q3v',3160.92,5383),(197,'小米手机_87','M4uLQ3mX','/static/images/a.png','tj',8379.01,9062),(198,'小米手机_88','hSheclcKIJ','/static/images/a.png','lHweM',7817.58,3902),(199,'小米手机_89','IvUVbfwl6t','/static/images/a.png','J74',435.05,5717),(200,'小米手机_90','x','/static/images/a.png','o0M',7240.00,8152),(201,'小米手机_91','gzdzprux','/static/images/a.png','zv83ermoO',4650.63,5804),(202,'小米手机_92','MI','/static/images/a.png','1p',4640.69,1325),(203,'小米手机_93','vpM4LY','/static/images/a.png','shp',7154.25,5793),(204,'小米手机_94','TeP1','/static/images/a.png','Ci',7418.85,6140),(205,'小米手机_95','Pfvf','/static/images/a.png','xs2qvk1sj',7851.31,1864),(206,'小米手机_96','bVzWT','/static/images/a.png','7wcGAjqD1',5268.82,4277),(207,'小米手机_97','k5OH','/static/images/a.png','S',3629.98,2463),(208,'小米手机_98','pY','/static/images/a.png','VsENSZuCs',3453.47,7599),(209,'小米手机_99','Od2bU','/static/images/a.png','n',4736.12,5295);
;
UNLOCK TABLES;
--
-- Table structure for table `t_order`
--
DROP TABLE IF EXISTS `t_order`;
;
;
CREATE TABLE `t_order` (
`oid` bigint NOT NULL AUTO_INCREMENT COMMENT '订单ID',
`user_id` bigint DEFAULT NULL COMMENT '用户ID',
`goods_id` bigint DEFAULT NULL COMMENT '商品ID',
`goods_name` varchar(16) DEFAULT NULL COMMENT '商品名称',
`goods_count` int DEFAULT '0' COMMENT '商品数量',
`goods_price` decimal(10,2) DEFAULT '0.00' COMMENT '商品单价',
`order_channel` tinyint DEFAULT '1' COMMENT '1=pc,2=android,3=ios',
`status` tinyint DEFAULT '0' COMMENT '订单状态,0=创建未支付,1=已支付,2=已取消,3=已发货,4=已退款,5=已完成',
`create_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '订单创建时间',
`pay_date` timestamp NULL DEFAULT NULL COMMENT '支付时间',
PRIMARY KEY (`oid`)
) ENGINE=InnoDB AUTO_INCREMENT=700736744551256065 DEFAULT CHARSET=utf8mb3 COMMENT='订单信息表';
;
--
-- Dumping data for table `t_order`
--
LOCK TABLES `t_order` WRITE;
;
;
UNLOCK TABLES;
--
-- Table structure for table `t_seckill_goods`
--
DROP TABLE IF EXISTS `t_seckill_goods`;
;
;
CREATE TABLE `t_seckill_goods` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '秒杀商品ID',
`goods_id` bigint DEFAULT NULL COMMENT '商品ID',
`seckill_price` decimal(10,2) DEFAULT '0.00' COMMENT '秒杀价',
`stock_count` int DEFAULT NULL COMMENT '存库数量',
`start_date` timestamp NULL DEFAULT NULL COMMENT '秒杀开始时间',
`end_date` timestamp NULL DEFAULT NULL COMMENT '秒杀结束时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb3 COMMENT='秒杀商品信息表';
;
--
-- Dumping data for table `t_seckill_goods`
--
LOCK TABLES `t_seckill_goods` WRITE;
;
INSERT INTO `t_seckill_goods` VALUES (17,110,3348.81,10,'2022-03-14 00:00:00','2022-04-14 00:00:00'),(18,111,6901.60,5438,'2022-03-14 00:00:00','2022-04-14 00:00:00'),(19,112,9820.62,8162,'2022-03-14 00:00:00','2022-04-14 00:00:00'),(20,113,3178.17,6091,'2022-03-14 00:00:00','2022-04-14 00:00:00'),(21,114,8488.26,3667,'2022-03-14 00:00:00','2022-04-14 00:00:00');
;
UNLOCK TABLES;
--
-- Table structure for table `t_seckill_order`
--
DROP TABLE IF EXISTS `t_seckill_order`;
;
;
CREATE TABLE `t_seckill_order` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '秒杀订单ID',
`user_id` bigint DEFAULT NULL COMMENT '用户ID',
`order_id` bigint DEFAULT NULL COMMENT '订单ID',
`goods_id` bigint DEFAULT NULL COMMENT '商品ID',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1677 DEFAULT CHARSET=utf8mb3 COMMENT='秒杀订单信息表';
;
--
-- Dumping data for table `t_seckill_order`
--
LOCK TABLES `t_seckill_order` WRITE;
;
;
UNLOCK TABLES;
--
-- Table structure for table `t_user`
--
DROP TABLE IF EXISTS `t_user`;
;
;
CREATE TABLE `t_user` (
`id` bigint NOT NULL COMMENT '用户ID,手机号码',
`nickname` varchar(255) NOT NULL COMMENT '昵称',
`password` varchar(32) DEFAULT NULL COMMENT 'MD5(MD5(pass明文+固定salt)+salt)',
`salt` varchar(32) DEFAULT NULL COMMENT '随机salt',
`register_date` timestamp NULL DEFAULT NULL COMMENT '注册时间',
`last_login_date` timestamp NULL DEFAULT NULL COMMENT '最后一次登录时间',
`login_count` int DEFAULT '0' COMMENT '登录次数',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='用户信息表';
;
--
-- Dumping data for table `t_user`
--
LOCK TABLES `t_user` WRITE;
;
INSERT INTO `t_user` VALUES (18684671234,'陈辉','0fd01e4e22c9c4c579065df382ed883c','a37ac1b94cfc40ce8e193c6be497f826','2022-03-07 11:51:50','2022-03-07 11:51:53',1);
-- -- Table structure for table `t_goods` -- DROp TABLE IF EXISTS `t_goods`; ; ; CREATE TABLE `t_goods` ( `gid` bigint NOT NULL AUTO_INCREMENT COMMENT '商品ID', `goods_name` varchar(16) DEFAULT NULL COMMENT '商品名称', `goods_title` varchar(64) DEFAULT NULL COMMENT '商品标题', `goods_img` varchar(64) DEFAULT NULL COMMENT '商品图片', `goods_detail` longtext COMMENT '商品详情', `goods_price` decimal(10,2) DEFAULT '0.00' COMMENT '商品价格', `goods_stock` int DEFAULT '0' COMMENT '商品库存', PRIMARY KEY (`gid`) ) ENGINE=InnoDB AUTO_INCREMENT=310 DEFAULT CHARSET=utf8mb3 COMMENT='商品信息表'; ; -- -- Dumping data for table `t_goods` -- LOCK TABLES `t_goods` WRITE; ; INSERT INTO `t_goods` VALUES (110,'小米手机_0','0o1LAfCk','/static/images/a.png','GC2F',3348.81,3150),(111,'小米手机_1','FAs','/static/images/a.png','tw4lCUHBrH',6901.60,5438),(112,'小米手机_2','rG','/static/images/a.png','ucOear5S',9820.62,8162),(113,'小米手机_3','iSLPOycDo','/static/images/a.png','IK7',3178.17,6091),(114,'小米手机_4','ZYO','/static/images/a.png','ei',8488.26,3667),(115,'小米手机_5','tKdm2fOXF','/static/images/a.png','HGlXj',5626.75,6162),(116,'小米手机_6','M','/static/images/a.png','nos',201.12,8885),(117,'小米手机_7','31T','/static/images/a.png','6bcF',5661.70,8088),(118,'小米手机_8','20SGw8VQr','/static/images/a.png','IfdHib',313.46,4),(119,'小米手机_9','fvN1s','/static/images/a.png','DRujrNOX',1526.50,5304),(120,'小米手机_10','yh0cu','/static/images/a.png','53lREj2FdZ',8999.94,2597),(121,'小米手机_11','i5kTHWFeX','/static/images/a.png','K',1662.61,6778),(122,'小米手机_12','yjPlune','/static/images/a.png','E2mH',6361.92,9336),(123,'小米手机_13','6Q','/static/images/a.png','v87vEdw',3585.59,2044),(124,'小米手机_14','6sxNLi','/static/images/a.png','52',2811.25,7051),(125,'小米手机_15','kX4s5oHmQ','/static/images/a.png','6FaUJghtc',6705.92,3931),(126,'小米手机_16','wG','/static/images/a.png','7S9',5493.15,4804),(127,'小米手机_17','qirDoootYr','/static/images/a.png','c',5336.25,8692),(128,'小米手机_18','reGMc2sU','/static/images/a.png','oh2',3608.50,8847),(129,'小米手机_19','h4ct','/static/images/a.png','9faj1RfK',5965.39,1924),(130,'小米手机_20','b12OK','/static/images/a.png','MPdQKzhOqr',8914.38,4868),(131,'小米手机_21','C3Pw','/static/images/a.png','rQtoY',9726.14,2019),(132,'小米手机_22','i34ci','/static/images/a.png','a4MV21rfr',3304.16,687),(133,'小米手机_23','mHStH87R','/static/images/a.png','g',5082.51,1670),(134,'小米手机_24','lpyx','/static/images/a.png','V',7675.51,8417),(135,'小米手机_25','o','/static/images/a.png','XH0RB',2680.58,5271),(136,'小米手机_26','m','/static/images/a.png','tgHJh1JDy',4100.02,4024),(137,'小米手机_27','VJSMI','/static/images/a.png','gsU7c',8241.24,7095),(138,'小米手机_28','UeGgfD9wp','/static/images/a.png','m2e',2952.57,8649),(139,'小米手机_29','H','/static/images/a.png','ER',7532.04,6592),(140,'小米手机_30','cJBDCH','/static/images/a.png','HkvJ5j',5824.62,6226),(141,'小米手机_31','L','/static/images/a.png','gdgPFod',2418.16,9585),(142,'小米手机_32','RcIQk','/static/images/a.png','LpngOQD',2440.95,4108),(143,'小米手机_33','Jn8','/static/images/a.png','vVL',8134.51,6632),(144,'小米手机_34','iW','/static/images/a.png','y8NV2TVL',7775.00,3051),(145,'小米手机_35','S','/static/images/a.png','RJIJvbXD',6781.55,6870),(146,'小米手机_36','Zr','/static/images/a.png','59Yjp9xI',1742.38,3917),(147,'小米手机_37','1QgD4fE3jR','/static/images/a.png','vRCBJX1Y',8754.92,2464),(148,'小米手机_38','YTEif9U8','/static/images/a.png','qRsyqzT',1406.98,7620),(149,'小米手机_39','7cg7ssi4N','/static/images/a.png','rL0oybu',7840.18,5530),(150,'小米手机_40','PtMc0pHT57','/static/images/a.png','cnzwh',5304.13,2889),(151,'小米手机_41','EQtOGY3t5w','/static/images/a.png','eX',5200.70,2749),(152,'小米手机_42','w','/static/images/a.png','iGN4F',4257.39,5322),(153,'小米手机_43','X','/static/images/a.png','zj6gWX',770.96,4016),(154,'小米手机_44','VMfup','/static/images/a.png','k',3480.28,487),(155,'小米手机_45','Qy','/static/images/a.png','7mKe',4805.12,6757),(156,'小米手机_46','a6IznSQp','/static/images/a.png','uQhM',770.74,4819),(157,'小米手机_47','LrzdrxUZ','/static/images/a.png','21QQ',6943.10,251),(158,'小米手机_48','uQO','/static/images/a.png','Hc',2445.68,4561),(159,'小米手机_49','FxiKL','/static/images/a.png','3',1275.24,493),(160,'小米手机_50','f','/static/images/a.png','i91H',6992.30,7235),(161,'小米手机_51','AnHnkQ7x9n','/static/images/a.png','SuIDRtI1lm',7602.27,9183),(162,'小米手机_52','lXQ1TQWh0','/static/images/a.png','jOBHZr5',7960.13,5244),(163,'小米手机_53','rXNovMjTp','/static/images/a.png','gdeZ2li',7609.59,6336),(164,'小米手机_54','5','/static/images/a.png','o',6233.58,4182),(165,'小米手机_55','rNdks','/static/images/a.png','mblj2l',1877.58,4780),(166,'小米手机_56','7GTK','/static/images/a.png','3iZ6THyDUS',4120.94,246),(167,'小米手机_57','YNQ','/static/images/a.png','S',6222.65,922),(168,'小米手机_58','WLj6h','/static/images/a.png','S3e8n22uu0',8662.73,4202),(169,'小米手机_59','abh5C','/static/images/a.png','3eOt6bjFp',5178.10,1561),(170,'小米手机_60','b','/static/images/a.png','IL',2410.07,4731),(171,'小米手机_61','GQl','/static/images/a.png','kgxP9',9569.25,4777),(172,'小米手机_62','a8','/static/images/a.png','VHQ',103.13,5695),(173,'小米手机_63','NN','/static/images/a.png','pHcTO',726.42,4110),(174,'小米手机_64','Ws','/static/images/a.png','v',8427.08,9850),(175,'小米手机_65','Aab1AonB','/static/images/a.png','ME9',1651.76,3132),(176,'小米手机_66','DXT','/static/images/a.png','tXgyX',3216.09,1308),(177,'小米手机_67','qz','/static/images/a.png','E',7686.70,8848),(178,'小米手机_68','Vn5pTL','/static/images/a.png','7',8650.18,8874),(179,'小米手机_69','ksF','/static/images/a.png','NbJSW',3592.45,5203),(180,'小米手机_70','PA','/static/images/a.png','PnlVsy',861.33,6171),(181,'小米手机_71','s','/static/images/a.png','zd',2804.05,8410),(182,'小米手机_72','n9r','/static/images/a.png','CQK3aGm23m',6840.12,2486),(183,'小米手机_73','E1nRAku5M','/static/images/a.png','oZ',5369.82,2962),(184,'小米手机_74','WYSgU','/static/images/a.png','kDfvc0',2205.38,6295),(185,'小米手机_75','eF','/static/images/a.png','1',4148.24,3945),(186,'小米手机_76','Y','/static/images/a.png','914oM',4325.91,6488),(187,'小米手机_77','CT750QFt','/static/images/a.png','dNe8',7848.73,4184),(188,'小米手机_78','J2lUrv6i','/static/images/a.png','o',1534.95,4717),(189,'小米手机_79','msCI0','/static/images/a.png','h',1459.57,1005),(190,'小米手机_80','hTM','/static/images/a.png','QZVSz5R',5346.83,7693),(191,'小米手机_81','XtdBhUJeO','/static/images/a.png','vkOSx',6286.26,3343),(192,'小米手机_82','glCMkI','/static/images/a.png','pk0w',5059.55,5747),(193,'小米手机_83','ocO7lht2','/static/images/a.png','u',5816.83,3866),(194,'小米手机_84','X5ZJGYm','/static/images/a.png','JQ',6013.34,5435),(195,'小米手机_85','UIc','/static/images/a.png','8QBjET4b7t',9376.66,2818),(196,'小米手机_86','ESfn','/static/images/a.png','Q3v',3160.92,5383),(197,'小米手机_87','M4uLQ3mX','/static/images/a.png','tj',8379.01,9062),(198,'小米手机_88','hSheclcKIJ','/static/images/a.png','lHweM',7817.58,3902),(199,'小米手机_89','IvUVbfwl6t','/static/images/a.png','J74',435.05,5717),(200,'小米手机_90','x','/static/images/a.png','o0M',7240.00,8152),(201,'小米手机_91','gzdzprux','/static/images/a.png','zv83ermoO',4650.63,5804),(202,'小米手机_92','MI','/static/images/a.png','1p',4640.69,1325),(203,'小米手机_93','vpM4LY','/static/images/a.png','shp',7154.25,5793),(204,'小米手机_94','TeP1','/static/images/a.png','Ci',7418.85,6140),(205,'小米手机_95','Pfvf','/static/images/a.png','xs2qvk1sj',7851.31,1864),(206,'小米手机_96','bVzWT','/static/images/a.png','7wcGAjqD1',5268.82,4277),(207,'小米手机_97','k5OH','/static/images/a.png','S',3629.98,2463),(208,'小米手机_98','pY','/static/images/a.png','VsENSZuCs',3453.47,7599),(209,'小米手机_99','Od2bU','/static/images/a.png','n',4736.12,5295); ; UNLOCK TABLES; -- -- Table structure for table `t_order` -- DROP TABLE IF EXISTS `t_order`; ; ; CREATE TABLE `t_order` ( `oid` bigint NOT NULL AUTO_INCREMENT COMMENT '订单ID', `user_id` bigint DEFAULT NULL COMMENT '用户ID', `goods_id` bigint DEFAULT NULL COMMENT '商品ID', `goods_name` varchar(16) DEFAULT NULL COMMENT '商品名称', `goods_count` int DEFAULT '0' COMMENT '商品数量', `goods_price` decimal(10,2) DEFAULT '0.00' COMMENT '商品单价', `order_channel` tinyint DEFAULT '1' COMMENT '1=pc,2=android,3=ios', `status` tinyint DEFAULT '0' COMMENT '订单状态,0=创建未支付,1=已支付,2=已取消,3=已发货,4=已退款,5=已完成', `create_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '订单创建时间', `pay_date` timestamp NULL DEFAULT NULL COMMENT '支付时间', PRIMARY KEY (`oid`) ) ENGINE=InnoDB AUTO_INCREMENT=700736744551256065 DEFAULT CHARSET=utf8mb3 COMMENT='订单信息表'; ; -- -- Dumping data for table `t_order` -- LOCK TABLES `t_order` WRITE; ; ; UNLOCK TABLES; -- -- Table structure for table `t_seckill_goods` -- DROP TABLE IF EXISTS `t_seckill_goods`; ; ; CREATE TABLE `t_seckill_goods` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '秒杀商品ID', `goods_id` bigint DEFAULT NULL COMMENT '商品ID', `seckill_price` decimal(10,2) DEFAULT '0.00' COMMENT '秒杀价', `stock_count` int DEFAULT NULL COMMENT '存库数量', `start_date` timestamp NULL DEFAULT NULL COMMENT '秒杀开始时间', `end_date` timestamp NULL DEFAULT NULL COMMENT '秒杀结束时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb3 COMMENT='秒杀商品信息表'; ; -- -- Dumping data for table `t_seckill_goods` -- LOCK TABLES `t_seckill_goods` WRITE; ; INSERT INTO `t_seckill_goods` VALUES (17,110,3348.81,10,'2022-03-14 00:00:00','2022-04-14 00:00:00'),(18,111,6901.60,5438,'2022-03-14 00:00:00','2022-04-14 00:00:00'),(19,112,9820.62,8162,'2022-03-14 00:00:00','2022-04-14 00:00:00'),(20,113,3178.17,6091,'2022-03-14 00:00:00','2022-04-14 00:00:00'),(21,114,8488.26,3667,'2022-03-14 00:00:00','2022-04-14 00:00:00'); ; UNLOCK TABLES; -- -- Table structure for table `t_seckill_order` -- DROP TABLE IF EXISTS `t_seckill_order`; ; ; CREATE TABLE `t_seckill_order` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '秒杀订单ID', `user_id` bigint DEFAULT NULL COMMENT '用户ID', `order_id` bigint DEFAULT NULL COMMENT '订单ID', `goods_id` bigint DEFAULT NULL COMMENT '商品ID', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1677 DEFAULT CHARSET=utf8mb3 COMMENT='秒杀订单信息表'; ; -- -- Dumping data for table `t_seckill_order` -- LOCK TABLES `t_seckill_order` WRITE; ; ; UNLOCK TABLES; -- -- Table structure for table `t_user` -- DROP TABLE IF EXISTS `t_user`; ; ; CREATE TABLE `t_user` ( `id` bigint NOT NULL COMMENT '用户ID,手机号码', `nickname` varchar(255) NOT NULL COMMENT '昵称', `password` varchar(32) DEFAULT NULL COMMENT 'MD5(MD5(pass明文+固定salt)+salt)', `salt` varchar(32) DEFAULT NULL COMMENT '随机salt', `register_date` timestamp NULL DEFAULT NULL COMMENT '注册时间', `last_login_date` timestamp NULL DEFAULT NULL COMMENT '最后一次登录时间', `login_count` int DEFAULT '0' COMMENT '登录次数', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='用户信息表'; ; -- -- Dumping data for table `t_user` -- LOCK TABLES `t_user` WRITE; ; INSERT INTO `t_user` VALUES (18684671234,'陈辉','0fd01e4e22c9c4c579065df382ed883c','a37ac1b94cfc40ce8e193c6be497f826','2022-03-07 11:51:50','2022-03-07 11:51:53',1);
得到表:
4、配置yml文件
spring:
application:
name: seckill
datasource:
url: jdbc:mysql://localhost:3306/seckill?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&characterEncoding=UTF8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
hikari:
# 最小空闲连接数量
minimum-idle: 5
# 空闲连接存活最大时间,默认600000(10分钟)
idle-timeout: 180000
# 连接池最大连接数,默认是10
maximum-pool-size: 10
# 此属性控制从池返回的连接的默认自动提交行为,默认值:true
auto-commit: true
# 连接池名称
pool-name: MyHikariCP
# 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
max-lifetime: 1800000
# 数据库连接超时时间,默认30秒,即30000
connection-timeout: 30000
freemarker:
#设置编码格式
charset: UTF-8
#后缀
suffix: .ftl
#文档类型
content-type: text/html
#模板前端
template-loader-path: classpath:/templates/
#启用模板
enabled: true
# 开启静态资源
mvc:
static-path-pattern: /static, builder) ->
builder.author(scanner.apply("请输入作者名称?"))
.enableSwagger()
.fileOverride()
.outputDir(System.getProperty("user.dir") + "\src\main\java")
.commentDate("yyyy-MM-dd")
.dateType(DateType.TIME_PACK)
)
.packageConfig((builder) ->
builder.parent("com.mwy.seckill")
.entity("pojo")
.service("service")
.serviceImpl("service.impl")
.mapper("mapper")
.xml("mapper.xml")
.pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "\src\main\resources\mapper"))
)
.injectionConfig((builder) ->
builder.beforeOutputFile(
(a, b) -> log.warn("tableInfo: " + a.getEntityName())
)
)
.strategyConfig((scanner, builder) ->
builder.addInclude(getTables(scanner.apply("请输入表名,多个英文逗号分隔?所有输入 all")))
.addTablePrefix("tb_", "t_")
.entityBuilder()
.enableChainModel()
.enableLombok()
.enableTableFieldAnnotation()
.addTableFills(
new Column("create_time", FieldFill.INSERT)
)
.controllerBuilder()
.enableRestStyle()
.enableHyphenStyle()
.build())
.templateEngine(new FreemarkerTemplateEngine())
.execute();
}
protected static List getTables(String tables) {
return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
}
}
(2)运行:
(3)UserMapper类增加注解
二、前端构建
1、templates增加相关界面
(1) login.ftl
<#include "common/head.ftl"/>
.layui-panel {
position: absolute;
width: 400px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 15px 15px 0px 15px;
border-radius: 20px;
}
.layui-form-label {
padding: 9px 0px;
}
h3 {
text-align: center;
line-height: 45px;
font-size: 40px;
color: white;
padding-bottom: 15px;
}
用户登录
(2)common目录,head.ftl
秒杀项目 <#assign ctx> ${springMacroRequestContext.getContextPath()} #assign>
(3)goods目录,goodsList.ftl
<#include "../common/head.ftl">
这是商品展示界面
2、导入layui
(1)resources新建static目录,建asset... 等目录
3、编写login.js
(1)resources->static->asset->js下新建project目录
①、新建Javascript文件命名为login
login.js
layui.define(()=>{
// 得到layui中封装的jquery
let $=layui.jquery
// 给登录按钮设置事件
$(login).click(()=>{
// 取到表单的值
var mobile = $(mobile).val();
var password=$(password).val();
// 将数据给后台(前后端分离axios,普通开发ajax)
$.ajax({
url:"",//后台登录接口
data:{
// 需要携带的数据
mobile,
password
},
datatype: "json",//后端给你的数据类型
success(e){
// 成功的回调函数
},
error(e){
// 报错的回调函数
}
})
})
})
4、新建PathController类(controller软件包下)
(1)专门用于跳转路径:PathController类
package com.mwy.seckill.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PathController {
// 登录跳首页
@RequestMapping("/")
public String toPath(){
return "login";
}
// 跳所有二级页面
@RequestMapping("/{dir}/{path}")
public String toPath(@PathVariable("dir") String dir, @PathVariable("path") String path){
return dir+"/"+path;
}
}
(2)运行得到界面
三、MD5加密
1、导入帮助包与exception包
2、前后端传值
新建vo软件包,在建uservo类
package com.mwy.seckill.vo;
import lombok.Data;
@Data
public class UserVo {
// 手机号
private String mobile;
// 密码
private String password;
}
3、登录方法:
(1)Service层:
①、IUserService接口
package com.mwy.seckill.service; import com.mwy.seckill.pojo.User; import com.baomidou.mybatisplus.extension.service.IService; import com.mwy.seckill.util.response.ResponseResult; import com.mwy.seckill.vo.UserVo; public interface IUserService extends IService{ ResponseResult> findByAccount(UserVo userVo); }
②、实现接口UserServiceImpl
package com.mwy.seckill.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.mwy.seckill.pojo.User; import com.mwy.seckill.mapper.UserMapper; import com.mwy.seckill.service.IUserService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.mwy.seckill.util.ValidatorUtils; import com.mwy.seckill.util.response.ResponseResult; import com.mwy.seckill.util.response.ResponseResultCode; import com.mwy.seckill.vo.UserVo; import org.springframework.stereotype.Service; @Service public class UserServiceImpl extends ServiceImplimplements IUserService { @Override public ResponseResult> findByAccount(UserVo userVo) { // 先判断信息是否符合(账号是否是手机号码,密码是不是空) if(!ValidatorUtils.isMobile(userVo.getMobile())){ return ResponseResult.failure(ResponseResultCode.USER_ACCOUNT_NOT_MOBLIE); } if(!StringUtils.isBlank(userVo.getPassword())){ return ResponseResult.failure(ResponseResultCode.USER_PASSWORD_NOT_MATCH); } // 再去数据库查出对应的用户(mobile) User user=this.getOne(new QueryWrapper ().eq("id",userVo.getMobile())); if(user==null){ return ResponseResult.failure(ResponseResultCode.USER_ACCOUNT_NOT_FIND); } // 比较密码 if(userVo.getPassword().equals(user.getPassword())){ return ResponseResult.failure(ResponseResultCode.USER_PASSWORD_NOT_MATCH); } return ResponseResult.success(); } }
(2)Controller层
①、UserController
package com.mwy.seckill.controller;
import com.mwy.seckill.service.IUserService;
import com.mwy.seckill.util.response.ResponseResult;
import com.mwy.seckill.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
// 用户登录
@RequestMapping("/login")
public ResponseResult> login(UserVo userVo){
// 调用service的登录验证
return userService.findByAccount(userVo);
}
}
4、密码加密
(1)、将md5.js放到js文件中
(function ($) {
'use strict'
function safeAdd (x, y) {
var lsw = (x & 0xffff) + (y & 0xffff)
var msw = (x >> 16) + (y >> 16) + (lsw >> 16)
return (msw << 16) | (lsw & 0xffff)
}
function bitRotateLeft (num, cnt) {
return (num << cnt) | (num >>> (32 - cnt))
}
function md5cmn (q, a, b, x, s, t) {
return safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b)
}
function md5ff (a, b, c, d, x, s, t) {
return md5cmn((b & c) | (~b & d), a, b, x, s, t)
}
function md5gg (a, b, c, d, x, s, t) {
return md5cmn((b & d) | (c & ~d), a, b, x, s, t)
}
function md5hh (a, b, c, d, x, s, t) {
return md5cmn(b ^ c ^ d, a, b, x, s, t)
}
function md5ii (a, b, c, d, x, s, t) {
return md5cmn(c ^ (b | ~d), a, b, x, s, t)
}
function binlMD5 (x, len) {
x[len >> 5] |= 0x80 << (len % 32)
x[((len + 64) >>> 9 << 4) + 14] = len
var i
var olda
var oldb
var oldc
var oldd
var a = 1732584193
var b = -271733879
var c = -1732584194
var d = 271733878
for (i = 0; i < x.length; i += 16) {
olda = a
oldb = b
oldc = c
oldd = d
a = md5ff(a, b, c, d, x[i], 7, -680876936)
d = md5ff(d, a, b, c, x[i + 1], 12, -389564586)
c = md5ff(c, d, a, b, x[i + 2], 17, 606105819)
b = md5ff(b, c, d, a, x[i + 3], 22, -1044525330)
a = md5ff(a, b, c, d, x[i + 4], 7, -176418897)
d = md5ff(d, a, b, c, x[i + 5], 12, 1200080426)
c = md5ff(c, d, a, b, x[i + 6], 17, -1473231341)
b = md5ff(b, c, d, a, x[i + 7], 22, -45705983)
a = md5ff(a, b, c, d, x[i + 8], 7, 1770035416)
d = md5ff(d, a, b, c, x[i + 9], 12, -1958414417)
c = md5ff(c, d, a, b, x[i + 10], 17, -42063)
b = md5ff(b, c, d, a, x[i + 11], 22, -1990404162)
a = md5ff(a, b, c, d, x[i + 12], 7, 1804603682)
d = md5ff(d, a, b, c, x[i + 13], 12, -40341101)
c = md5ff(c, d, a, b, x[i + 14], 17, -1502002290)
b = md5ff(b, c, d, a, x[i + 15], 22, 1236535329)
a = md5gg(a, b, c, d, x[i + 1], 5, -165796510)
d = md5gg(d, a, b, c, x[i + 6], 9, -1069501632)
c = md5gg(c, d, a, b, x[i + 11], 14, 643717713)
b = md5gg(b, c, d, a, x[i], 20, -373897302)
a = md5gg(a, b, c, d, x[i + 5], 5, -701558691)
d = md5gg(d, a, b, c, x[i + 10], 9, 38016083)
c = md5gg(c, d, a, b, x[i + 15], 14, -660478335)
b = md5gg(b, c, d, a, x[i + 4], 20, -405537848)
a = md5gg(a, b, c, d, x[i + 9], 5, 568446438)
d = md5gg(d, a, b, c, x[i + 14], 9, -1019803690)
c = md5gg(c, d, a, b, x[i + 3], 14, -187363961)
b = md5gg(b, c, d, a, x[i + 8], 20, 1163531501)
a = md5gg(a, b, c, d, x[i + 13], 5, -1444681467)
d = md5gg(d, a, b, c, x[i + 2], 9, -51403784)
c = md5gg(c, d, a, b, x[i + 7], 14, 1735328473)
b = md5gg(b, c, d, a, x[i + 12], 20, -1926607734)
a = md5hh(a, b, c, d, x[i + 5], 4, -378558)
d = md5hh(d, a, b, c, x[i + 8], 11, -2022574463)
c = md5hh(c, d, a, b, x[i + 11], 16, 1839030562)
b = md5hh(b, c, d, a, x[i + 14], 23, -35309556)
a = md5hh(a, b, c, d, x[i + 1], 4, -1530992060)
d = md5hh(d, a, b, c, x[i + 4], 11, 1272893353)
c = md5hh(c, d, a, b, x[i + 7], 16, -155497632)
b = md5hh(b, c, d, a, x[i + 10], 23, -1094730640)
a = md5hh(a, b, c, d, x[i + 13], 4, 681279174)
d = md5hh(d, a, b, c, x[i], 11, -358537222)
c = md5hh(c, d, a, b, x[i + 3], 16, -722521979)
b = md5hh(b, c, d, a, x[i + 6], 23, 76029189)
a = md5hh(a, b, c, d, x[i + 9], 4, -640364487)
d = md5hh(d, a, b, c, x[i + 12], 11, -421815835)
c = md5hh(c, d, a, b, x[i + 15], 16, 530742520)
b = md5hh(b, c, d, a, x[i + 2], 23, -995338651)
a = md5ii(a, b, c, d, x[i], 6, -198630844)
d = md5ii(d, a, b, c, x[i + 7], 10, 1126891415)
c = md5ii(c, d, a, b, x[i + 14], 15, -1416354905)
b = md5ii(b, c, d, a, x[i + 5], 21, -57434055)
a = md5ii(a, b, c, d, x[i + 12], 6, 1700485571)
d = md5ii(d, a, b, c, x[i + 3], 10, -1894986606)
c = md5ii(c, d, a, b, x[i + 10], 15, -1051523)
b = md5ii(b, c, d, a, x[i + 1], 21, -2054922799)
a = md5ii(a, b, c, d, x[i + 8], 6, 1873313359)
d = md5ii(d, a, b, c, x[i + 15], 10, -30611744)
c = md5ii(c, d, a, b, x[i + 6], 15, -1560198380)
b = md5ii(b, c, d, a, x[i + 13], 21, 1309151649)
a = md5ii(a, b, c, d, x[i + 4], 6, -145523070)
d = md5ii(d, a, b, c, x[i + 11], 10, -1120210379)
c = md5ii(c, d, a, b, x[i + 2], 15, 718787259)
b = md5ii(b, c, d, a, x[i + 9], 21, -343485551)
a = safeAdd(a, olda)
b = safeAdd(b, oldb)
c = safeAdd(c, oldc)
d = safeAdd(d, oldd)
}
return [a, b, c, d]
}
function binl2rstr (input) {
var i
var output = ''
var length32 = input.length * 32
for (i = 0; i < length32; i += 8) {
output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xff)
}
return output
}
function rstr2binl (input) {
var i
var output = []
output[(input.length >> 2) - 1] = undefined
for (i = 0; i < output.length; i += 1) {
output[i] = 0
}
var length8 = input.length * 8
for (i = 0; i < length8; i += 8) {
output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << (i % 32)
}
return output
}
function rstrMD5 (s) {
return binl2rstr(binlMD5(rstr2binl(s), s.length * 8))
}
function rstrHMACMD5 (key, data) {
var i
var bkey = rstr2binl(key)
var ipad = []
var opad = []
var hash
ipad[15] = opad[15] = undefined
if (bkey.length > 16) {
bkey = binlMD5(bkey, key.length * 8)
}
for (i = 0; i < 16; i += 1) {
ipad[i] = bkey[i] ^ 0x36363636
opad[i] = bkey[i] ^ 0x5c5c5c5c
}
hash = binlMD5(ipad.concat(rstr2binl(data)), 512 + data.length * 8)
return binl2rstr(binlMD5(opad.concat(hash), 512 + 128))
}
function rstr2hex (input) {
var hexTab = '0123456789abcdef'
var output = ''
var x
var i
for (i = 0; i < input.length; i += 1) {
x = input.charCodeAt(i)
output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f)
}
return output
}
function str2rstrUTF8 (input) {
return unescape(encodeURIComponent(input))
}
function rawMD5 (s) {
return rstrMD5(str2rstrUTF8(s))
}
function hexMD5 (s) {
return rstr2hex(rawMD5(s))
}
function rawHMACMD5 (k, d) {
return rstrHMACMD5(str2rstrUTF8(k), str2rstrUTF8(d))
}
function hexHMACMD5 (k, d) {
return rstr2hex(rawHMACMD5(k, d))
}
function md5 (string, key, raw) {
if (!key) {
if (!raw) {
return hexMD5(string)
}
return rawMD5(string)
}
if (!raw) {
return hexHMACMD5(key, string)
}
return rawHMACMD5(key, string)
}
if (typeof define === 'function' && define.amd) {
define(function () {
return md5
})
} else if (typeof module === 'object' && module.exports) {
module.exports = md5
} else {
$.md5 = md5
}
})(this)
(2)前端密码进行加密
①、login.js文件:
layui.use(["jquery","layer"],()=>{
// 得到layui中封装的jquery
let $=layui.jquery
let layer=layui.layer
// 给登录按钮设置事件
$(login).click(()=>{
// 取到表单的值
let mobile = $("#mobile").val();
let password=$("#password").val();
// 前端加密的盐
let salt= "f1g2h3j4";
if(password){
// 将密码和盐混在一起
password=salt.charAt(1) + "" + salt.charAt(5) + password + salt.charAt(0) + "" + salt.charAt(3);
// 进行MD5加密
password=md5(password)
}
console.log(password)
// 将数据给后台(前后端分离axios,普通开发ajax)
$.ajax({
url:"/user/login",//后台登录接口
data:{
// 需要携带的数据
mobile,
password
},
datatype: "json",//后端给你的数据类型
success(e){
// 成功的回调函数
layer.msg(e.message,{icon: 6});
},
error(e){
// 报错的回调函数
}
})
})
})
②、UserServiceImpl:
// 比较密码
// 二重加密(前端->后端,后端->数据库)
String salt=user.getSalt();
// 将前台的加密密码和后端的盐再次进行加密
String newPassword=MD5Utils.formPassToDbPass(userVo.getPassword(),salt);
if(!newPassword.equals(user.getPassword())){
return ResponseResult.failure(ResponseResultCode.USER_PASSWORD_NOT_MATCH);
}
package com.mwy.seckill.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.mwy.seckill.pojo.User; import com.mwy.seckill.mapper.UserMapper; import com.mwy.seckill.service.IUserService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.mwy.seckill.util.MD5Utils; import com.mwy.seckill.util.ValidatorUtils; import com.mwy.seckill.util.response.ResponseResult; import com.mwy.seckill.util.response.ResponseResultCode; import com.mwy.seckill.vo.UserVo; import org.springframework.stereotype.Service; @Service public class UserServiceImpl extends ServiceImplimplements IUserService { @Override public ResponseResult> findByAccount(UserVo userVo) { // 先判断信息是否符合(账号是否是手机号码,密码是不是空) if(!ValidatorUtils.isMobile(userVo.getMobile())){ return ResponseResult.failure(ResponseResultCode.USER_ACCOUNT_NOT_MOBLIE); } if(StringUtils.isBlank(userVo.getPassword())){ return ResponseResult.failure(ResponseResultCode.USER_PASSWORD_NOT_MATCH); } // 再去数据库查出对应的用户(mobile) User user=this.getOne(new QueryWrapper ().eq("id",userVo.getMobile())); if(user==null){ return ResponseResult.failure(ResponseResultCode.USER_ACCOUNT_NOT_FIND); } // 比较密码 // 二重加密(前端->后端,后端->数据库) String salt=user.getSalt(); // 将前台的加密密码和后端的盐再次进行加密 String newPassword= MD5Utils.formPassToDbPass(userVo.getPassword(),salt); if(!newPassword.equals(user.getPassword())){ return ResponseResult.failure(ResponseResultCode.USER_PASSWORD_NOT_MATCH); } return ResponseResult.success(); } }
③、得到密钥,登录成功:
四、 全局异常抓获
1、jsr303验证
jsr303注解
(1)UserVo增加相关注解
package com.mwy.seckill.vo;
import com.mwy.seckill.util.response.ResponseResultCode;
import com.mwy.seckill.util.validate.IsMobile;
import com.mwy.seckill.util.validate.IsRequired;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
@Data
public class UserVo {
// 手机号
@IsMobile(code = ResponseResultCode.USER_ACCOUNT_NOT_FIND)
private String mobile;
// 密码
// 开启jsr303验证 @NotEmpty:不能为空
// @NotEmpty
@IsRequired(code = ResponseResultCode.USER_CREDENTIAL_NOT_BE_EMPTY)
private String password;
}
2、导入帮助包validate,异常抓获
(1)IsMobile
package com.mwy.seckill.util.validate;
import com.mwy.seckill.util.response.ResponseResultCode;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
@documented
@Constraint(
validatedBy = {IsMobilevalidator.class}
)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface IsMobile {
ResponseResultCode code() default ResponseResultCode.UNKNOWN;
String message() default "";
Class>[] groups() default {};
Class extends Payload>[] payload() default {};
}
(2)IsMobilevalidator(IsMobile的解析类)
package com.mwy.seckill.util.validate; import com.mwy.seckill.util.ValidatorUtils; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; public class IsMobilevalidator implements ConstraintValidator{ @Override public boolean isValid(String mobile, ConstraintValidatorContext context) { return ValidatorUtils.isMobile(mobile); } }
(3)IsRequired
package com.mwy.seckill.util.validate;
import com.mwy.seckill.util.response.ResponseResultCode;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
@documented
@Constraint(
validatedBy = {IsRequiredValidator.class}
)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface IsRequired {
ResponseResultCode code() default ResponseResultCode.UNKNOWN;
String message() default "";
Class>[] groups() default {};
Class extends Payload>[] payload() default {};
}
(4)IsRequiredValidator
package com.mwy.seckill.util.validate; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; @Slf4j public class IsRequiredValidator implements ConstraintValidator{ @Override public boolean isValid(String str, ConstraintValidatorContext context) { return StringUtils.isNotBlank(str); } }
(5)ThrowableAdvice
package com.mwy.seckill.util.response;
import com.mwy.seckill.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice //RestController的增强类
@Slf4j
public class ThrowableAdvice {
// 第一种方法
@JsonResponseResult
// ExceptionHandler 抓捕一个异常
@ExceptionHandler(value = {BusinessException.class})
public ResponseResultCode globalBusinessException(Model m, Exception e) {
log.error(e.toString());
return ((BusinessException) e).getResponseResultCode();
}
// 第二种方法
//@JsonResponseResult
//@ExceptionHandler(value = {BusinessException.class})
//public Object globalBusinessException(Model m, Exception e) {
// Object[] arguments=((BindException)e).getFieldError().getArguments();
// return Arrays.stream(arguments)
// .filter(t->t instanceof ResponseResultCode)
// .findAny()
// .orElse(ResponseResultCode.UNKNOWN);
//}
@JsonResponseResult
@ExceptionHandler(value = {BindException.class})
public ResponseResultCode globalBindException(Model m, Exception e) {
log.error(e.toString());
BindException error = (BindException) e;
return (ResponseResultCode) error.getFieldError().getArguments()[1];
}
@JsonResponseResult
@ExceptionHandler(value = {Throwable.class})
public ResponseResultCode globalException(Model m, Exception e) {
log.error(e.toString());
return ResponseResultCode.UNKNOWN;
}
}
3、在UserController类方法中加入注解
(1)开启jsr303验证
@Valid :
4、实现类抛出异常
(1)开启jsr303验证
@Valid :
(1)UserServiceImpl:
package com.mwy.seckill.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.mwy.seckill.exception.BusinessException; import com.mwy.seckill.pojo.User; import com.mwy.seckill.mapper.UserMapper; import com.mwy.seckill.service.IUserService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.mwy.seckill.util.MD5Utils; import com.mwy.seckill.util.ValidatorUtils; import com.mwy.seckill.util.response.ResponseResult; import com.mwy.seckill.util.response.ResponseResultCode; import com.mwy.seckill.vo.UserVo; import org.springframework.stereotype.Service; import java.util.Date; @Service public class UserServiceImpl extends ServiceImplimplements IUserService { @Override public ResponseResult> findByAccount(UserVo userVo) { // 先判断信息是否符合(账号是否是手机号码,密码是不是空) if(!ValidatorUtils.isMobile(userVo.getMobile())){ throw new BusinessException(ResponseResultCode.USER_ACCOUNT_NOT_MOBLIE); } if(StringUtils.isBlank(userVo.getPassword())){ throw new BusinessException(ResponseResultCode.USER_PASSWORD_NOT_MATCH); } // 再去数据库查出对应的用户(mobile) User user=this.getOne(new QueryWrapper ().eq("id",userVo.getMobile())); if(user==null){ throw new BusinessException(ResponseResultCode.USER_ACCOUNT_NOT_FIND); } // 比较密码 // 二重加密(前端->后端,后端->数据库) String salt=user.getSalt(); // 将前台的加密密码和后端的盐再次进行加密 String newPassword=MD5Utils.formPassToDbPass(userVo.getPassword(),salt); if(!newPassword.equals(user.getPassword())){ throw new BusinessException(ResponseResultCode.USER_PASSWORD_NOT_MATCH); } // 修改最后的登录时间 this.update(new UpdateWrapper ().eq("id",userVo.getMobile()).set("last_login_date",new Date()).setSql("login_count=login_count+1")); return ResponseResult.success(); } }
5、运行测试
(1)随机填写用户名得到结果:
(2) 不填写密码:
结束!!!!



