diff --git a/luoo_user/pom.xml b/luoo_user/pom.xml index 8c72348..49226ee 100644 --- a/luoo_user/pom.xml +++ b/luoo_user/pom.xml @@ -120,6 +120,20 @@ fastjson 1.2.80 + + + + cn.hutool + hutool-all + 5.8.20 + + + + + com.google.zxing + core + 3.5.1 + app diff --git a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java index 8362878..308e716 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java @@ -4,6 +4,7 @@ import annotation.GlobalInterceptor; import annotation.VerifyParam; import api.Result; import api.StatusCode; +import cn.hutool.extra.qrcode.QrCodeUtil; import com.alibaba.fastjson.JSONObject; import com.luoo.user.dto.UserNameLoginDto; import com.luoo.user.dto.request.*; @@ -365,7 +366,7 @@ public class LoginController extends BaseController { * * @return 登录结果 */ - @ApiOperation(value = "通过用户名/密码登录") + @ApiOperation(value = "7.通过用户名/密码登录") @PostMapping("/login/username") public Result loginByUserName(@Validated @RequestBody UserNameLoginDto userNameLoginDto) { String userName = userNameLoginDto.getUserName(); @@ -388,11 +389,55 @@ public class LoginController extends BaseController { } } - @ApiOperation(value = "修改密码") + @ApiOperation(value = "8.修改密码") @PostMapping("/changePassword") public Result changePassword(@Validated @RequestBody UserNameLoginDto changePasswordDto) { userInfoService.changePassword(changePasswordDto.getUserName(), changePasswordDto.getPassword()); return Result.success(); } + @ApiOperation(value = "9.1.获取UUID", notes = "获取UUID") + @GetMapping(value = "/uuid") + public Result getUuid() { + String uuid = UUID.randomUUID().toString(); + return Result.success(uuid); + } + + @ApiOperation(value = "9.2.校验uuid对应的请求认证状态", notes = "0是未完成,其余是已完成状态的token") + @GetMapping(value = "/check/qr") + public Result getCountryCode(String uuid) { + return Result.success(redisTemplate.opsForValue().get(uuid).toString()); + } + + @ApiOperation(value = "9.3.获取登录二维码、放入Token", notes = "前端获取二维码") + @GetMapping(value = "/getLoginQr") + public void createCodeImg(HttpServletRequest request, HttpServletResponse response, String uuid){ + response.setHeader("Pragma", "No-cache"); + response.setHeader("Cache-Control", "no-cache"); + + response.setDateHeader("Expires", 0); + response.setContentType("image/jpeg"); + + try { + response.setHeader("uuid", uuid); + // 这里是开源工具类 hutool里的QrCodeUtil + // 网址:http://hutool.mydoc.io/ + QrCodeUtil.generate(uuid, 300, 300, "jpg",response.getOutputStream()); + redisTemplate.opsForValue().set(uuid, 0, 60, TimeUnit.SECONDS); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @ApiOperation(value = "9.4.确认身份接口:确定身份以及判断是否二维码过期等", notes = "确认身份接口:确定身份以及判断是否二维码过期等") + @PostMapping(value = "/bindUserIdAndToken") + public Result bindUserIdAndToken(@RequestParam("token") String token , + @RequestParam("userId") String userId){ + try { + return Result.success(userInfoService.bindUserIdAndToken(userId,token)); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java index 7b0f6eb..07ca002 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java @@ -709,4 +709,19 @@ public class UserInfoService { userInfoByUserName.setPassword(encoder.encode(password)); userInfoDao.save(userInfoByUserName); } + + public String bindUserIdAndToken(String userId, String token) throws Exception { + Object o = redisTemplate.opsForValue().get(token); + if (o == null) { + throw new BizException("二维码失效!"); + } + if(!String.valueOf(0).equals("0")) { + throw new BizException("登录认证已完成"); + } + + UserInfo loginUserInfo = userInfoDao.findById(userId).get(); + String authorization = loginOrRegister(loginUserInfo); + redisTemplate.opsForValue().set(token, authorization, 60, TimeUnit.SECONDS); + return authorization; + } }