1.otp驗(yàn)證碼獲取
//用戶獲取otp短信接口
@RequestMapping(value = "/getotp",method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommenReturnType getOtp(@RequestParam(name="telphone") String telphone){
//1. 按照一定的規(guī)則生成OPT驗(yàn)證碼
Random random = new Random();
int randomInt = random.nextInt(99999);// [0,99999)
randomInt += 10000; // [10000,109999]
String otpCode = String.valueOf(randomInt);
//2. 將OPT驗(yàn)證碼與用戶手機(jī)號相關(guān)聯(lián),使用HttpSession的方式(企業(yè)里使用分布式的Redis)
httpServletRequest.getSession().setAttribute(telphone,otpCode);
//3. 將OPT驗(yàn)證碼通過短信通道發(fā)送給用戶,省略(可夠買第三方短信通道,以HTTP Post 方式發(fā)送)
//企業(yè)里控制臺輸出是用log4j阵面,這里方便調(diào)試轻局,簡單的把驗(yàn)證碼輸入控制臺
System.out.println("telphone = "+telphone+" & otpCode = "+otpCode);
return CommenReturnType.create(null);
}
2.用戶注冊
//用戶注冊接口
@RequestMapping(value = "/register",method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommenReturnType register(@RequestParam(name="telphone") String telphone,
@RequestParam(name="otpCode") String otpCode,
@RequestParam(name="password") String password,
@RequestParam(name="name") String name,
@RequestParam(name="gender") Integer gender,
@RequestParam(name="age") Integer age) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
//驗(yàn)證手機(jī)號和otpcode相符合
String inSessionOtpCode = (String)this.httpServletRequest.getSession().getAttribute(telphone);
if(!StringUtils.equals(otpCode,inSessionOtpCode)){
throw new BusinessException(EmBussinessError.PARMETER_VALIDATION_ERROR,"短信驗(yàn)證碼不符合");
}
//用戶注冊流程
UserModel userModel = new UserModel();
userModel.setName(name);
//userModel.setGender(gender);
userModel.setGender(new Byte(String.valueOf(gender.intValue())));
userModel.setTelphone(telphone);
userModel.setAge(age);
userModel.setEncrptPassWord(this.EncodeByMd5(password));
userService.register(userModel);
return CommenReturnType.create(null);
}
3.用戶登錄
//用戶登錄接口
@RequestMapping(value = "/login",method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommenReturnType login(@RequestParam(name="telphone") String telphone,
@RequestParam(name="password") String password) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
//入?yún)⑿r?yàn)
if(StringUtils.isEmpty(telphone)||
StringUtils.isEmpty(password)){
throw new BusinessException(EmBussinessError.PARMETER_VALIDATION_ERROR);
}
//用戶登錄服務(wù),用來校驗(yàn)用戶登錄是否合法
UserModel userModel = userService.validateLogin(telphone,this.EncodeByMd5(password));
//將登錄憑證加入到用戶登錄成功的session內(nèi)
this.httpServletRequest.getSession().setAttribute("IS_LOGIN",true);
this.httpServletRequest.getSession().setAttribute("LOGIN_USER",userModel);
return CommenReturnType.create(null);
}
4.優(yōu)化校驗(yàn)
(1) 檢驗(yàn)結(jié)果
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
import java.util.Map;
public class ValidationResult {
//校驗(yàn)結(jié)果是否有錯(cuò)
private boolean hasErrors = false;
private Map<String,String> errMsgMap = new HashMap<>();
public boolean isHasErrors() {
return hasErrors;
}
public void setHasErrors(boolean hasErrors) {
this.hasErrors = hasErrors;
}
public Map<String, String> getErrMsgMap() {
return errMsgMap;
}
public void setErrMsgMap(Map<String, String> errMsgMap) {
this.errMsgMap = errMsgMap;
}
//實(shí)現(xiàn)通用的通過格式化字符串信息獲取錯(cuò)誤結(jié)果的msg的方法
public String getErrMsg(){
return StringUtils.join(errMsgMap.values().toArray(),",");
}
}
(2)校驗(yàn)器
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 {
private Validator validator;
//實(shí)現(xiàn)校驗(yàn)方法并返回結(jié)果
public ValidationResult validate(Object bean){
final ValidationResult result = new ValidationResult();
Set<ConstraintViolation<Object>> constraintViolationSet = validator.validate(bean);
if(constraintViolationSet.size()>0){
//有錯(cuò)誤
result.setHasErrors(true);
constraintViolationSet.forEach(constraintViolation->{
String errMsg = constraintViolation.getMessage();
String propertyName = constraintViolation.getPropertyPath().toString();
result.getErrMsgMap().put(propertyName,errMsg);
});
}
return result;
}
@Override
public void afterPropertiesSet() throws Exception {
//將hibernate validator 通過工廠的初始化方式使其實(shí)例化
this.validator = (Validator) Validation.buildDefaultValidatorFactory().getValidator();
}
}