|  |  |  | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.http.MediaType; | 
|---|
|  |  |  | import org.springframework.validation.BindingResult; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.imageio.ImageIO; | 
|---|
|  |  |  | import java.awt.*; | 
|---|
|  |  |  | import java.awt.image.BufferedImage; | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.util.Objects; | 
|---|
|  |  |  | import java.util.Random; | 
|---|
|  |  |  | import java.util.UUID; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 客户端请求用户登录,客户端提交Json数据 | 
|---|
|  |  |  | * @param vo 用户登录值对象 | 
|---|
|  |  |  | * @param bindingResult 输入验证 | 
|---|
|  |  |  | * @return 登录用户值对象 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @PostMapping(path = "login", consumes = MediaType.APPLICATION_JSON_VALUE)//前端提交json数据 | 
|---|
|  |  |  | @Log("用户登录(json)") | 
|---|
|  |  |  | public BaseResponse<UserVo> login(@RequestBody  @Valid LoginVo vo,BindingResult bindingResult, | 
|---|
|  |  |  | HttpSession session) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(bindingResult != null && bindingResult.hasErrors()){ | 
|---|
|  |  |  | return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 从Session中获取保存的验证码 | 
|---|
|  |  |  | String sessionCaptcha = (String) session.getAttribute("captcha"); | 
|---|
|  |  |  | // 首先验证验证码 | 
|---|
|  |  |  | if (vo.captcha != null && vo.captcha.equalsIgnoreCase(sessionCaptcha)) { | 
|---|
|  |  |  | session.removeAttribute("captcha"); | 
|---|
|  |  |  | return this.doLogin(vo) ; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | // 验证码错误,返回登录页面并显示错误信息 | 
|---|
|  |  |  | return BaseResponseUtils.buildFail("验证码错误"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("用户登录异常", e); | 
|---|
|  |  |  | return BaseResponseUtils.buildException(e.getMessage()); | 
|---|
|  |  |  | @Log("用户登录") | 
|---|
|  |  |  | public BaseResponse<UserVo> login(@RequestBody  @Valid LoginVo vo,HttpSession session) { | 
|---|
|  |  |  | // 从Session中获取保存的验证码 | 
|---|
|  |  |  | String sessionCaptcha = (String) session.getAttribute("captcha"); | 
|---|
|  |  |  | // 首先验证验证码 | 
|---|
|  |  |  | if (vo.captcha != null && vo.captcha.equalsIgnoreCase(sessionCaptcha)) { | 
|---|
|  |  |  | session.removeAttribute("captcha"); | 
|---|
|  |  |  | return this.doLogin(vo) ; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | // 验证码错误,返回登录页面并显示错误信息 | 
|---|
|  |  |  | return BaseResponseUtils.buildFail("验证码错误"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return 登录用户值对象 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @PostMapping(path = "loginForm", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)//前端提交form表单数据 | 
|---|
|  |  |  | @Log("用户登录(form)") | 
|---|
|  |  |  | public BaseResponse<UserVo> loginForm(@Valid LoginVo loginVo, BindingResult bindingResult,HttpSession session){ | 
|---|
|  |  |  | try{ | 
|---|
|  |  |  | if(bindingResult != null && bindingResult.hasErrors()){ | 
|---|
|  |  |  | return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 从Session中获取保存的验证码 | 
|---|
|  |  |  | String sessionCaptcha = (String) session.getAttribute("captcha"); | 
|---|
|  |  |  | // 首先验证验证码 | 
|---|
|  |  |  | if (loginVo.captcha != null && loginVo.captcha.equalsIgnoreCase(sessionCaptcha)) { | 
|---|
|  |  |  | session.removeAttribute("captcha"); | 
|---|
|  |  |  | return this.doLogin(loginVo) ; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | // 验证码错误,返回登录页面并显示错误信息 | 
|---|
|  |  |  | return BaseResponseUtils.buildFail("验证码错误"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("用户登录异常", e); | 
|---|
|  |  |  | return BaseResponseUtils.buildException(e.getMessage()); | 
|---|
|  |  |  | public BaseResponse<UserVo> loginForm(@Valid LoginVo loginVo,HttpSession session){ | 
|---|
|  |  |  | // 从Session中获取保存的验证码 | 
|---|
|  |  |  | String sessionCaptcha = (String) session.getAttribute("captcha"); | 
|---|
|  |  |  | // 首先验证验证码 | 
|---|
|  |  |  | if (loginVo.captcha != null && loginVo.captcha.equalsIgnoreCase(sessionCaptcha)) { | 
|---|
|  |  |  | session.removeAttribute("captcha"); | 
|---|
|  |  |  | return this.doLogin(loginVo) ; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | // 验证码错误,返回登录页面并显示错误信息 | 
|---|
|  |  |  | return BaseResponseUtils.buildFail("验证码错误"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | @GetMapping(path = "logout") | 
|---|
|  |  |  | @Log("退出登录") | 
|---|
|  |  |  | public BaseResponse<Boolean> logout(HttpServletRequest hr){ | 
|---|
|  |  |  | try{ | 
|---|
|  |  |  | String token = hr.getHeader("token") ; | 
|---|
|  |  |  | if(token != null){ | 
|---|
|  |  |  | this.sv.logout(token) ; | 
|---|
|  |  |  | return BaseResponseUtils.buildSuccess(true); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return BaseResponseUtils.buildFail("未从header中得到token"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("查询一个用户数据异常", e); | 
|---|
|  |  |  | return BaseResponseUtils.buildException(e.getMessage()); | 
|---|
|  |  |  | String token = hr.getHeader("token") ; | 
|---|
|  |  |  | if(token != null){ | 
|---|
|  |  |  | this.sv.logout(token) ; | 
|---|
|  |  |  | return BaseResponseUtils.buildSuccess(true); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return BaseResponseUtils.buildFail("未从header中得到token"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private String generateCaptcha() { | 
|---|
|  |  |  | Random r = new Random(); | 
|---|
|  |  |  | return r.nextInt(9000) + 1000 + ""; | 
|---|
|  |  |  | //        Random r = new Random(); | 
|---|
|  |  |  | //        return r.nextInt(9000) + 1000 + ""; | 
|---|
|  |  |  | return "1234"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 用户登录 | 
|---|