补充:
用户登录:
http协议是在网络上是明文传输,如果数据包被截取到了,那么别人就会得到明文密码。
第一次MD5是防止用户的明文密码在网络上传输
第二次MD5是防止数据库泄露以后密码被反查出
GlobalException:
package com.imooc.miaosha.exception; import com.imooc.miaosha.result.CodeMsg; public class GlobalException extends RuntimeException{ private static final long serialVersionUID = 1L; private CodeMsg cm; public GlobalException(CodeMsg cm) { super(cm.toString()); this.cm = cm; } public CodeMsg getCm() { return cm; } }
GlobalExceptionHandler:
package com.imooc.miaosha.exception; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.validation.BindException; import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import com.imooc.miaosha.result.CodeMsg; import com.imooc.miaosha.result.Result; @ControllerAdvice @ResponseBody public class GlobalExceptionHandler { @ExceptionHandler(value=Exception.class) public Result<String> exceptionHandler(HttpServletRequest request, Exception e){ e.printStackTrace(); if(e instanceof GlobalException) { GlobalException ex = (GlobalException)e; return Result.error(ex.getCm()); }else if(e instanceof BindException) { BindException ex = (BindException)e; List<ObjectError> errors = ex.getAllErrors(); ObjectError error = errors.get(0); String msg = error.getDefaultMessage(); return Result.error(CodeMsg.BIND_ERROR.fillArgs(msg)); }else { return Result.error(CodeMsg.SERVER_ERROR); } } }
然后service层需要抛出global异常,controller不用做处理。