package com.roma.romaapi.service; import com.roma.romaapi.dao.ApiDao; import com.roma.romaapi.utils.JWTUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import com.roma.romaapi.utils.SecurityUtils; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.concurrent.TimeUnit; @Service public class ApiService { @Autowired private RedisTemplate redisTemplate; @Autowired private ApiDao apiDao; @Autowired SecurityUtils securityUtils; @Autowired JWTUtil jwtUtil; // 登录方法 public Map login(Map maps) { // 请求参数,获取验证码,验证码token,用户名,密码 String userName = maps.get("api_name")[0]; // 用户名 String password = maps.get("api_password")[0]; // 密码 String captcha = maps.get("api_captcha")[0]; // 用户输入验证码 String captchaToken = maps.get("captchaToken")[0]; // 验证码token,获取验证码的值 String captchaTokenValue = redisTemplate.opsForValue().get(captchaToken); // 缓存中验证码的值 Map loginResult = new HashMap<>(); // String aa = securityUtils.encodePassword(password); // 加密密码 // 判断是否存在此用户,用户密码是否正确 Map UserInfo = apiDao.adminInfoQueryForMap(userName); if(UserInfo.containsKey("sysErrorCode")) { loginResult.put("sysErrorCode", "500"); loginResult.put("sysErrorMessage", "账号或密码错误"); return loginResult; } Object adminPassword = UserInfo.get("admin_password"); String stringAdminPassword = adminPassword.toString(); // 校验密码是否正确 if(!securityUtils.matchesPassword(password,stringAdminPassword)) { // 校验密码是否正确 密码不正确,返回错误 loginResult.put("sysErrorCode", "500"); loginResult.put("sysErrorMessage", "账号或密码错误"); return loginResult; } // 校验图形验证码 if(!Boolean.TRUE.equals(redisTemplate.hasKey(captchaToken)) || !Objects.equals(captchaTokenValue, captcha)){ loginResult.put("sysErrorCode", "500"); loginResult.put("sysErrorMessage", "验证码错误"); return loginResult; } // 通过密码,验证码校验,发放token Map jwtInfo = new HashMap<>(); Object userId = UserInfo.get("id"); String stringUserId = userId.toString(); jwtInfo.put("id", stringUserId); jwtInfo.put("loginTime", System.currentTimeMillis()/1000+""); jwtInfo.put("name", userName); String jwtToken = jwtUtil.getToken(jwtInfo); Map map = new HashMap<>(); map.put("loginToken", jwtToken); return map; } }