手機端H5登入
阿新 • • 發佈:2022-03-31
@Controller @Slf4j @RequestMapping("/mini-app") public class AppLoginController extends BaseController { @Resource private PasswordService passwordService; @Resource private IUserService userService; @Resource private RedisCache redisCache; @Value("${rsa.privateKey}")private String privateKey; /** * h5端登入 * @param loginName 登入名 * @param password 密碼 * @return */ @PostMapping("/login") @ResponseBody public AjaxResult login(String loginName,String password){ try { log.info("h5端登入"); if (StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)){return error("賬號或密碼不能為空"); } String logName = RSAUtils.decryptByPrivateKey(loginName, privateKey); String pwd = RSAUtils.decryptByPrivateKey(password, privateKey); log.debug("loginName:{},logName:{}", loginName, logName); log.debug("password:{},pwd:{}", password, pwd); User user= userService.selectUserByLoginName(logName); if (null == user){ return error("賬號或密碼錯誤"); } String salt = user.getSalt(); String md5PassWord = passwordService.encryptPassword(logName, pwd, salt); if (!StringUtils.equals(md5PassWord,user.getPassword())){ return error("賬號或密碼錯誤"); } Map<String, Object> map = new HashMap<String, Object>() { private static final long serialVersionUID = 1L; { put("userId", user.getUserId()); put("expire_time", System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 7); } }; String token = JWTUtil.createToken(map, user.getLoginName().getBytes()); //儲存token 24小時 每次登入會覆蓋原來的token(自動登入有效時間) redisCache.setCacheObject("APP_TOKEN_" + user.getUserId(), token, 7, TimeUnit.DAYS); //返回使用者資訊 UserVo userVo = getUser(user, token); return AjaxResult.success("登入成功",userVo); } catch (Exception e) { return error("賬戶或密碼錯誤!"); } } /** * 自動登入 * @param token * @return */ @GetMapping("/autoLogin") @ResponseBody public AjaxResult autoLogin(String token){ log.info("自動登入"); JWT jwt = JWTUtil.parseToken(token); Integer userId = (Integer) jwt.getPayload("userId"); User user = userService.selectUserById(Long.valueOf(userId)); if (null == user){ return error("自動登入失敗!"); } Date expireTime = new Date((Long)jwt.getPayload("expire_time")); Date now = DateUtils.getNowDate(); if (now.after(expireTime)){ return error("登入過期!"); } boolean verify = JWTUtil.verify(token, user.getLoginName().getBytes()); if (!verify){ return error("自動登入失敗!"); } //返回使用者資訊 UserVo userVo = getUser(user, token); return AjaxResult.success("登入成功",userVo); } /** * 返回登入使用者資訊 */ public UserVo getUser(User user,String token){ UserVo userVo = new UserVo(); userVo.setUserId(user.getUserId()); userVo.setUserName(user.getUserName()); userVo.setLoginName(user.getLoginName()); userVo.setUserType(user.getUserType()); userVo.setEmail(user.getEmail()); userVo.setPhoneNumber(user.getPhonenumber()); userVo.setSex(user.getSex()); userVo.setAvatar(user.getAvatar()); userVo.setToken(token); return userVo; } /** * 退出登入 * @param loginName 登入名 * @return */ @PostMapping("/logout") @ResponseBody public AjaxResult logout(String loginName){ log.info("退出登入"); User user = userService.selectUserByLoginName(loginName); String key = "APP_TOKEN_" + user.getUserId(); redisCache.deleteObject(key); return AjaxResult.success("退出成功"); } }