springboot秒杀课程学习整理1-3
1)实现手机验证码功能,用户注册功能,用户登入功能(这里讲开发流程,及本人遇到的问题,具体实现请看代码)
1、拦截请求,获取请求参数(这里的consumes是个常量,可以定义在baseController里)
@RequestMapping(value="/register",method={RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED})
@RequestParam(name="telphone")String telphone,
@RequestParam(name="otpCode")String otpCode,
@RequestParam(name="name")String name,
@RequestParam(name="gender")String gender,//性别
@RequestParam(name="age")Integer age,
@RequestParam(name="password")String password
2、使用传入的参数创建userModel对象,调用userService,将userModel转成userDO对象,使用userDOMapper插入数据库
注意:此处有部分细节,需注意如使用userId查userPassword的那个表需要自定义,以及通过手机号查userInfo表也需要自定义,在最后一章我
会把库的地址贴上
2)1、使用hibernate.validator对userModel进行校验
【1】 创建存放校验信息的类ValidationResult
【2】创建一个用于校验的类ValidateImpl,提供方法返回校验信息的方法,里面包括校验的具体实现
2、密码使用md5加密存入数据库
下面是代码详情:
UserController
package com.miaoshaproject.controller; import com.alibaba.druid.util.StringUtils;
import com.miaoshaproject.controller.viewobject.UserVO; import com.miaoshaproject.error.BusinessException;
import com.miaoshaproject.error.EmBusinessError;
import com.miaoshaproject.response.CommonReturnType;
import com.miaoshaproject.service.impl.UserServiceImpl;
import com.miaoshaproject.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.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; @RestController
@RequestMapping("/user")
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")//在跨域的情况下session是不支持共享的,前端也需要设置xhrFields:{widthCredentials:true}
public class UserController extends BaseController{
@Autowired
private UserServiceImpl userService; @Autowired
private HttpServletRequest httpServletRequest; @RequestMapping("/getuser")
@ResponseBody
public CommonReturnType getUserById(@RequestParam(name="id")Integer id)throws BusinessException{
UserVO userVO=new UserVO();
UserModel userModel=userService.getUserById(id);
if(userModel == null){
throw new BusinessException(EmBusinessError.USER_NOT_EXIST);
}
UserVO obj=converUserVOFromUserModel(userModel);
return CommonReturnType.create(obj);
} public UserVO converUserVOFromUserModel(UserModel userModel){
UserVO userVO=new UserVO();
BeanUtils.copyProperties(userModel,userVO);
return userVO;
} //用户获取opt短信验证码
@RequestMapping(value="/getotp",method={RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType getOtp(@RequestParam(name="telphone")String telphone){
//需要按照一定规则生成otp验证码
Random random=new Random();
int randomInt=random.nextInt(99999);
randomInt += 10000;
String otpCode=String.valueOf(randomInt);
//将opt验证码同对应手机号关联,redis可用于分布式,
// 这里使用httpsession的方式绑定手机号和code
this.httpServletRequest.getSession().setAttribute(telphone,otpCode); //将opt验证码通过短信通道发送给用户,
// 这里省略使用控制台的方式打印code码,在企业里开发这种做法是不允许的,
// 这些是敏感信息
System.out.println("telphone = " + telphone + "& otpCode = " + otpCode);
return CommonReturnType.create(null);
}
//用户登入功能
@RequestMapping(value="/login",method={RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType login(@RequestParam(name="telphone")String telphone,
@RequestParam(name="password")String password) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
//入参校验
if(StringUtils.isEmpty(telphone)||
StringUtils.isEmpty(password)){
throw new BusinessException(EmBusinessError.PARAMTER_VALIDATION_ERROR);
}
//验证登入是否合法
UserModel userModel = userService.validateLogin(telphone,
this.EncodeByMd5(password));
//添加登入成功的session
this.httpServletRequest.getSession().setAttribute("IS_LOGIN",true);
this.httpServletRequest.getSession().setAttribute("LOGIN_USER",userModel); return CommonReturnType.create(null); } //用户注册功能
@RequestMapping(value="/register",method={RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType register(@RequestParam(name="telphone")String telphone,
@RequestParam(name="otpCode")String otpCode,
@RequestParam(name="name")String name,
@RequestParam(name="gender")String gender,//性别
@RequestParam(name="age")Integer age,
@RequestParam(name="password")String password
) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
//验证手机号和对应的otpcode相符合
String inSesstionOtpCode=(String)this.httpServletRequest.getSession().getAttribute(telphone);
if(!com.alibaba.druid.util.StringUtils.equals(otpCode,inSesstionOtpCode)){
throw new BusinessException(EmBusinessError.PARAMTER_VALIDATION_ERROR,"短信验证码错误");
} //用户的注册流程
UserModel userModel=new UserModel();
userModel.setName(name);
userModel.setGender(gender);
userModel.setAge(age);
userModel.setTelphone(telphone);
userModel.setRegisterMode("byphone");
userModel.setEnCrptPassword(this.EncodeByMd5(password)); userService.register(userModel); return CommonReturnType.create(null);
}
//md5的加密计算
//java自带的md5只支持16位
public String EncodeByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
//确定计算方法
MessageDigest md5 = MessageDigest.getInstance("MD5");
BASE64Encoder base64en= new BASE64Encoder();
String newStr =base64en.encode(md5.digest(str.getBytes("utf-8")));
return newStr;
}
}
UserServiceIpml
package com.miaoshaproject.service.impl; import com.alibaba.druid.util.StringUtils;
import com.miaoshaproject.dao.UserDOMapper;
import com.miaoshaproject.dao.UserPasswordDOMapper;
import com.miaoshaproject.dataobject.UserDO;
import com.miaoshaproject.dataobject.UserPasswordDO;
import com.miaoshaproject.error.BusinessException;
import com.miaoshaproject.error.EmBusinessError;
import com.miaoshaproject.service.UserService;
import com.miaoshaproject.service.model.UserModel;
import com.miaoshaproject.validator.ValidationResult; import com.miaoshaproject.validator.ValidatorImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; @Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDOMapper userDOMapper;
@Autowired
private UserPasswordDOMapper userPasswordDOMapper;
@Autowired
private ValidatorImpl validator; @Override
public UserModel getUserById(int id) {
UserDO userDO=userDOMapper.selectByPrimaryKey(id);
UserPasswordDO userPasswordDO=userPasswordDOMapper.selectByUserId(id);
return converFromUserDO(userDO,userPasswordDO);
} public UserModel converFromUserDO (UserDO userDO,UserPasswordDO userPasswordDO){
UserModel userModel=new UserModel();
if(userDO==null){
return null;
}
BeanUtils.copyProperties(userDO,userModel);
if(userPasswordDO != null){
userModel.setEnCrptPassword(userPasswordDO.getEncrptPassword());
}
return userModel;
} @Override
@Transactional
public void register(UserModel userModel) throws BusinessException {
//这里需要严谨一些,对所有字段判断不为空
if(userModel == null){
throw new BusinessException(EmBusinessError.PARAMTER_VALIDATION_ERROR);
}
ValidationResult result=validator.validate(userModel);
if(result.isHasError()){
throw new BusinessException(EmBusinessError.PARAMTER_VALIDATION_ERROR,result.getErrMsg());
} //这里需要注意的点是在数据库里设置索引手机号是唯一的
try{
UserDO userDO=convertFromModel(userModel);
userDOMapper.insertSelective(userDO);
}catch(DuplicateKeyException ex){
throw new BusinessException(EmBusinessError.PARAMTER_VALIDATION_ERROR,"手机号已重复注册");
}
UserDO newUserDO=userDOMapper.selectByTelphone(userModel.getTelphone());
userModel.setId(newUserDO.getId());
UserPasswordDO userPasswordDO=convertPasswordFormUserModel(userModel);
userPasswordDOMapper.insertSelective(userPasswordDO);
return;
}
private UserPasswordDO convertPasswordFormUserModel(UserModel userModel){
if(userModel==null){
return null;
}
UserPasswordDO userPasswordDO=new UserPasswordDO();
userPasswordDO.setEncrptPassword(userModel.getEnCrptPassword()); userPasswordDO.setUserId(userModel.getId());
return userPasswordDO;
}
private UserDO convertFromModel(UserModel userModel){
if(userModel == null){
return null;
}
UserDO userDO = new UserDO();
BeanUtils.copyProperties(userModel,userDO);
return userDO;
} //登入是否合法 @Override
public UserModel validateLogin(String telphone, String password) throws BusinessException {
//通过手机获取用户信息,新增查询方式通过手机来获取用户信息
UserDO userDO=userDOMapper.selectByTelphone(telphone);
if(userDO==null){
throw new BusinessException(EmBusinessError.PARAMTER_VALIDATION_ERROR,"用户不存在");
}
UserPasswordDO userPasswordDO=userPasswordDOMapper.selectByUserId( userDO.getId()); //比对用户信息内加密的密码是否和传输进来的一致
if(!StringUtils.equals(userPasswordDO.getEncrptPassword(),password)){
throw new BusinessException(EmBusinessError.PARAMTER_VALIDATION_ERROR,"账号或密码不存在");
}
UserModel userModel=converFromUserDO(userDO,userPasswordDO);
return userModel;
}
}
ValidationResult
package com.miaoshaproject.validator; import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component; import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set; @Component
public class ValidatorImpl implements InitializingBean {
//javax.validator
private Validator validator; @Override
public void afterPropertiesSet() throws Exception {
//将hibernate validator通过工厂的初始化方式使其实例化
this.validator = Validation.buildDefaultValidatorFactory().getValidator(); } //事项校验方法返回校验结果
public ValidationResult validate(Object bean){
ValidationResult result=new ValidationResult();
Set<ConstraintViolation<Object>> constraintViolationsSet=validator.validate(bean);
if(constraintViolationsSet.size()>0){
//有错误
result.setHasError(true);
/*这里有报错说不支持7及以下的语言等级,
* file->project strcture->module->选择resource上面的language level给为7以上的
* file->setting->compiler->java Compiler->将version改为7以上
* */
constraintViolationsSet.forEach(constraintViolation->{
String errMsg=constraintViolation.getMessage();
String propertyName=constraintViolation.getPropertyPath().toString();
result.getErrMsgMap().put(propertyName,errMsg);
});
}
return result;
}
}
ValidateImpl
package com.miaoshaproject.validator; import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component; import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set; @Component
public class ValidatorImpl implements InitializingBean {
//javax.validator
private Validator validator; @Override
public void afterPropertiesSet() throws Exception {
//将hibernate validator通过工厂的初始化方式使其实例化
this.validator = Validation.buildDefaultValidatorFactory().getValidator(); } //事项校验方法返回校验结果
public ValidationResult validate(Object bean){
ValidationResult result=new ValidationResult();
Set<ConstraintViolation<Object>> constraintViolationsSet=validator.validate(bean);
if(constraintViolationsSet.size()>0){
//有错误
result.setHasError(true);
/*这里有报错说不支持7及以下的语言等级,
* file->project strcture->module->选择resource上面的language level给为7以上的
* file->setting->compiler->java Compiler->将version改为7以上
* */
constraintViolationsSet.forEach(constraintViolation->{
String errMsg=constraintViolation.getMessage();
String propertyName=constraintViolation.getPropertyPath().toString();
result.getErrMsgMap().put(propertyName,errMsg);
});
}
return result;
}
}
springboot秒杀课程学习整理1-3的更多相关文章
- springboot秒杀课程学习整理1-6
1)活动模型设计 配饰秒杀的模型(promoModel)id promoName startDate(建议使用joda-time) endDate itemId promoItemPrice 数据库( ...
- springboot秒杀课程学习整理1-1
1)新建一个maven工程quickStart,然后在pom文件里添加依赖 <parent> <groupId>org.springframework.boot</gro ...
- springboot秒杀课程学习整理1-5
1)交易模型设计 交易模型(用户下单的交易模型)OrderModel id(String 交易单号使用String), userId,itemId,amount(数量),orderAmount(总金额 ...
- springboot秒杀课程学习整理1-4
1)商品模型设计 (应该是先设计商品的model,然后才是数据库表) 模型字段(id,title,price(double),stock(库存),description,sales,imgUrl) 创 ...
- springboot秒杀课程学习整理1-2
1)从数据库到前端,做了三层转换,最后统一返回给前端的格式 DO-> model: 放在service中,目的是为了组装来自于数据库的数据,有些字段来自于不同的表的取,这一层相当于真正的业务模型 ...
- SpringBoot源码学习系列之异常处理自动配置
SpringBoot源码学习系列之异常处理自动配置 1.源码学习 先给个SpringBoot中的异常例子,假如访问一个错误链接,让其返回404页面 在浏览器访问: 而在其它的客户端软件,比如postm ...
- 201671010450-姚玉婷-实验十四 团队项目评审&课程学习总结
项目 内容 所属科目 软件工程http://www.cnblogs.com/nwnu-daizh 作业要求 https://www.cnblogs.com/nwnu-daizh/p/11093584. ...
- 金生芳-实验十四 团队项目评审&课程学习总结
实验十四 团队项目评审&课程学习总结 项目 内容 这个作业属于哪个课程 [教师博客主页链接] 这个作业的要求在哪里 [作业链接地址] 作业学习目标 (1)掌握软件项目评审会流程(2)反思总结课 ...
- 201671030117 孙欢灵 实验十四 团队项目评审&课程学习总结
项目 内容 作业所属课程 所属课程 作业要求 作业要求 课程学习目标 (1)掌握软件项目评审会流程:(2)反思总结课程学习内容 任务一:团队项目审核已完成.项目验收过程意见表已上交. 任务二:课程学习 ...
随机推荐
- 程序员 面试题【前端,java,php】
跬步客 网址:https://www.kuibuke.com/wall/index
- Vue 组件&组件之间的通信 之 子组件向父组件传值
子组件向父组件传值:子组件通过$.emit()方法以事件形式向父组件发送消息传值: 使用步骤: 定义组件:现有自定义组件com-a.com-b,com-a是com-b的父组件: 准备获取数据:父组件c ...
- 原创《开源一个用 vue 写的树层级组件 vue-ztree》
最近由于后台管理项目的需要,页面需要制作一个无限树的需求,我第一感就想到了插件 ztree,不过我觉得它太大了,还是自己动手丰衣足食吧. ztree 的 demo 地址:http://www.tree ...
- 关于SQL优化的一点建议
前段时间一直在做关于性能优化相关的工作,结合自己的实际工作经验,只针对SQL层面提一些优化的规范和建议. 针对SQL编写 1.单条SQL长度不宜超过100行: 2.SQL子查询不宜嵌套3层: 子查询嵌 ...
- 通过KSoap三方插件解析WebService接口方法
话不多说,正文如下: 1.在lib中放入ksoap2的jar包并导入 2.在xml 配置文件中加入: <!-- 访问网络的权限 --> <uses-permission androi ...
- allure --version 异常io.airlift.airline.ParseArgumentsUnexpectedException: Found unexpected parameter
执行allure --version时,有时会出现如下异常: io.airlift.airline.ParseArgumentsUnexpectedException: Found unexpecte ...
- 简单GC具体操作参数查看
代码: public class HeapTest { private static final int _1M = 1024 * 1024; public static void main(Stri ...
- AI illustrator 如何裁剪图片(扣取局部区域)
AI如何裁剪图片 在使用illustrator的过程中,很多亲想要AI的裁剪功能与ps一样强大,但是AI常用的裁剪图片方法如下. 工具/原料 illustrator cs6 应用剪切蒙版剪切图片 ...
- P4081 [USACO17DEC]Standing Out from the Herd
思路 对所有串建立广义SAM,之后记录SZ,统计本质不同子串时只统计SZ=1的即可 代码 #include <cstdio> #include <algorithm> #inc ...
- Windows server 2012 install .net core sdk 2.2.103
Windows8.1-KB2919442-x64 Windows8.1-KB2919355-x64 vc_redist.x64 dotnet-sdk-2.2.103-win-x64 dotnet-ho ...