refactor(auth): 重构登录认证和验证码功能
- 创建 SystemAuthVo 类用于统一登录返回数据结构 - 创建 SystemCaptchaVo 类用于验证码信息封装 - 将验证码功能从 SystemAuthService 拆分为独立的 SystemCaptchaService - 登录接口返回类型从 Map 改为 SystemAuthVo - 验证码接口返回类型从 Map 改为 SystemCaptchaVo - 登录时验证码校验改为调用 SystemCaptchaService - 移除 SystemAuthService 中的验证码相关代码 - 统一数据返回格式,提高代码可读性
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
|||||||
|
package cn.odboy.system.dal.model;
|
||||||
|
|
||||||
|
import cn.odboy.base.KitObject;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录成功后:token 与 用户信息
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class SystemAuthVo extends KitObject {
|
||||||
|
|
||||||
|
private String token;
|
||||||
|
private SystemUserInfoVo user;
|
||||||
|
}
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
package cn.odboy.system.dal.model;
|
||||||
|
|
||||||
|
import cn.odboy.base.KitObject;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证码信息
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class SystemCaptchaVo extends KitObject {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* redis key
|
||||||
|
*/
|
||||||
|
private String uuid;
|
||||||
|
/**
|
||||||
|
* image base64
|
||||||
|
*/
|
||||||
|
private String img;
|
||||||
|
}
|
||||||
+59
@@ -0,0 +1,59 @@
|
|||||||
|
package cn.odboy.system.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.odboy.constant.CaptchaCodeEnum;
|
||||||
|
import cn.odboy.constant.SystemConst;
|
||||||
|
import cn.odboy.framework.exception.BadRequestException;
|
||||||
|
import cn.odboy.framework.properties.AppProperties;
|
||||||
|
import cn.odboy.framework.redis.KitRedisHelper;
|
||||||
|
import cn.odboy.system.dal.model.SystemCaptchaVo;
|
||||||
|
import cn.odboy.system.dal.redis.SystemCacheKey;
|
||||||
|
import com.wf.captcha.base.Captcha;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证码
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class SystemCaptchaService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private KitRedisHelper redisHelper;
|
||||||
|
@Autowired
|
||||||
|
private AppProperties properties;
|
||||||
|
|
||||||
|
public SystemCaptchaVo getLoginCaptcha() {
|
||||||
|
// 获取运算的结果
|
||||||
|
Captcha captcha = properties.getLogin().getCaptchaSetting().getCaptcha();
|
||||||
|
String uuid = SystemCacheKey.CAPTCHA_LOGIN + IdUtil.simpleUUID();
|
||||||
|
//当验证码类型为 arithmetic时且长度 >= 2 时, captcha.text()的结果有几率为浮点型
|
||||||
|
String captchaValue = captcha.text();
|
||||||
|
if (captcha.getCharType() - 1 == CaptchaCodeEnum.ARITHMETIC.ordinal() &&
|
||||||
|
captchaValue.contains(SystemConst.SYMBOL_DOT)) {
|
||||||
|
captchaValue = captchaValue.split("\\.")[0];
|
||||||
|
}
|
||||||
|
// 保存
|
||||||
|
redisHelper.set(uuid, captchaValue, properties.getLogin().getCaptchaSetting().getExpiration(), TimeUnit.MINUTES);
|
||||||
|
// 验证码信息
|
||||||
|
SystemCaptchaVo captchaVo = new SystemCaptchaVo();
|
||||||
|
captchaVo.setUuid(uuid);
|
||||||
|
captchaVo.setImg(captcha.toBase64());
|
||||||
|
return captchaVo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validate(String uuid, String inputCode) {
|
||||||
|
// 查询验证码
|
||||||
|
String code = redisHelper.get(uuid, String.class);
|
||||||
|
if (StrUtil.isBlank(code)) {
|
||||||
|
throw new BadRequestException("验证码不存在或已过期");
|
||||||
|
}
|
||||||
|
// 清除验证码
|
||||||
|
redisHelper.del(uuid);
|
||||||
|
if (StrUtil.isBlank(inputCode) || !code.equalsIgnoreCase(inputCode)) {
|
||||||
|
throw new BadRequestException("验证码错误");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-4
@@ -16,11 +16,11 @@
|
|||||||
package cn.odboy.system.controller;
|
package cn.odboy.system.controller;
|
||||||
|
|
||||||
import cn.odboy.annotation.AnonymousPostMapping;
|
import cn.odboy.annotation.AnonymousPostMapping;
|
||||||
|
import cn.odboy.system.dal.model.SystemAuthVo;
|
||||||
import cn.odboy.system.dal.model.SystemUserLoginArgs;
|
import cn.odboy.system.dal.model.SystemUserLoginArgs;
|
||||||
import cn.odboy.system.service.SystemAuthService;
|
import cn.odboy.system.service.SystemAuthService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import java.util.Map;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -44,9 +44,8 @@ public class SystemAuthController {
|
|||||||
|
|
||||||
@ApiOperation("登录授权")
|
@ApiOperation("登录授权")
|
||||||
@AnonymousPostMapping(value = "/login")
|
@AnonymousPostMapping(value = "/login")
|
||||||
public ResponseEntity<Map<String, Object>> login(@Validated @RequestBody SystemUserLoginArgs loginRequest,
|
public ResponseEntity<SystemAuthVo> login(@Validated @RequestBody SystemUserLoginArgs loginRequest, HttpServletRequest request) throws Exception {
|
||||||
HttpServletRequest request) throws Exception {
|
SystemAuthVo loginInfo = systemAuthService.doLogin(loginRequest, request);
|
||||||
Map<String, Object> loginInfo = systemAuthService.doLogin(loginRequest, request);
|
|
||||||
return ResponseEntity.ok(loginInfo);
|
return ResponseEntity.ok(loginInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
package cn.odboy.system.dal.model;
|
||||||
|
|
||||||
|
import cn.odboy.base.KitObject;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录成功后:token 与 用户信息
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class SystemAuthVo extends KitObject {
|
||||||
|
|
||||||
|
private String token;
|
||||||
|
private SystemUserInfoVo user;
|
||||||
|
}
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
package cn.odboy.system.dal.model;
|
||||||
|
|
||||||
|
import cn.odboy.base.KitObject;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证码信息
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class SystemCaptchaVo extends KitObject {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* redis key
|
||||||
|
*/
|
||||||
|
private String uuid;
|
||||||
|
/**
|
||||||
|
* image base64
|
||||||
|
*/
|
||||||
|
private String img;
|
||||||
|
}
|
||||||
+5
-5
@@ -16,10 +16,10 @@
|
|||||||
package cn.odboy.system.openapi;
|
package cn.odboy.system.openapi;
|
||||||
|
|
||||||
import cn.odboy.annotation.AnonymousPostMapping;
|
import cn.odboy.annotation.AnonymousPostMapping;
|
||||||
import cn.odboy.system.service.SystemAuthService;
|
import cn.odboy.system.dal.model.SystemCaptchaVo;
|
||||||
|
import cn.odboy.system.service.SystemCaptchaService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import java.util.Map;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -31,12 +31,12 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
public class SystemCaptchaOpenController {
|
public class SystemCaptchaOpenController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private SystemAuthService systemAuthService;
|
private SystemCaptchaService systemCaptchaService;
|
||||||
|
|
||||||
@ApiOperation("获取验证码")
|
@ApiOperation("获取验证码")
|
||||||
@AnonymousPostMapping(value = "/getCode")
|
@AnonymousPostMapping(value = "/getCode")
|
||||||
public ResponseEntity<Map<String, Object>> getCode() {
|
public ResponseEntity<SystemCaptchaVo> getCode() {
|
||||||
Map<String, Object> imgResult = systemAuthService.getCaptchaInfo();
|
SystemCaptchaVo imgResult = systemCaptchaService.getLoginCaptcha();
|
||||||
return ResponseEntity.ok(imgResult);
|
return ResponseEntity.ok(imgResult);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-45
@@ -1,26 +1,19 @@
|
|||||||
package cn.odboy.system.service;
|
package cn.odboy.system.service;
|
||||||
|
|
||||||
import cn.hutool.core.util.IdUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import cn.odboy.constant.CaptchaCodeEnum;
|
|
||||||
import cn.odboy.constant.SystemConst;
|
import cn.odboy.constant.SystemConst;
|
||||||
import cn.odboy.framework.exception.BadRequestException;
|
import cn.odboy.framework.exception.BadRequestException;
|
||||||
import cn.odboy.framework.properties.AppProperties;
|
import cn.odboy.framework.properties.AppProperties;
|
||||||
import cn.odboy.framework.redis.KitRedisHelper;
|
import cn.odboy.framework.redis.KitRedisHelper;
|
||||||
|
import cn.odboy.system.dal.model.SystemAuthVo;
|
||||||
import cn.odboy.system.dal.model.SystemUserInfoVo;
|
import cn.odboy.system.dal.model.SystemUserInfoVo;
|
||||||
import cn.odboy.system.dal.model.SystemUserJwtVo;
|
import cn.odboy.system.dal.model.SystemUserJwtVo;
|
||||||
import cn.odboy.system.dal.model.SystemUserLoginArgs;
|
import cn.odboy.system.dal.model.SystemUserLoginArgs;
|
||||||
import cn.odboy.system.dal.redis.SystemCacheKey;
|
|
||||||
import cn.odboy.system.dal.redis.SystemUserOnlineInfoDAO;
|
import cn.odboy.system.dal.redis.SystemUserOnlineInfoDAO;
|
||||||
import cn.odboy.system.framework.permission.core.KitSecurityHelper;
|
import cn.odboy.system.framework.permission.core.KitSecurityHelper;
|
||||||
import cn.odboy.system.framework.permission.core.handler.TokenProvider;
|
import cn.odboy.system.framework.permission.core.handler.TokenProvider;
|
||||||
import cn.odboy.system.framework.permission.core.handler.UserDetailsHandler;
|
import cn.odboy.system.framework.permission.core.handler.UserDetailsHandler;
|
||||||
import cn.odboy.util.KitBeanUtil;
|
import cn.odboy.util.KitBeanUtil;
|
||||||
import cn.odboy.util.KitRsaEncryptUtil;
|
import cn.odboy.util.KitRsaEncryptUtil;
|
||||||
import com.wf.captcha.base.Captcha;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
@@ -35,29 +28,23 @@ public class SystemAuthService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private KitRedisHelper redisHelper;
|
private KitRedisHelper redisHelper;
|
||||||
@Autowired
|
@Autowired
|
||||||
|
private AppProperties properties;
|
||||||
|
@Autowired
|
||||||
private SystemUserOnlineInfoDAO systemUserOnlineInfoDAO;
|
private SystemUserOnlineInfoDAO systemUserOnlineInfoDAO;
|
||||||
@Autowired
|
@Autowired
|
||||||
private TokenProvider tokenProvider;
|
private TokenProvider tokenProvider;
|
||||||
@Autowired
|
@Autowired
|
||||||
private AppProperties properties;
|
|
||||||
@Autowired
|
|
||||||
private PasswordEncoder passwordEncoder;
|
private PasswordEncoder passwordEncoder;
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserDetailsHandler userDetailsService;
|
private UserDetailsHandler userDetailsService;
|
||||||
|
@Autowired
|
||||||
|
private SystemCaptchaService systemCaptchaService;
|
||||||
|
|
||||||
public Map<String, Object> doLogin(SystemUserLoginArgs loginRequest, HttpServletRequest request) throws Exception {
|
public SystemAuthVo doLogin(SystemUserLoginArgs loginRequest, HttpServletRequest request) throws Exception {
|
||||||
// 密码解密
|
// 密码解密
|
||||||
String password = KitRsaEncryptUtil.decryptByPrivateKey(properties.getRsa().getPrivateKey(), loginRequest.getPassword());
|
String password = KitRsaEncryptUtil.decryptByPrivateKey(properties.getRsa().getPrivateKey(), loginRequest.getPassword());
|
||||||
// 查询验证码
|
// 校验验证码
|
||||||
String code = redisHelper.get(loginRequest.getUuid(), String.class);
|
systemCaptchaService.validate(loginRequest.getUuid(), loginRequest.getCode());
|
||||||
// 清除验证码
|
|
||||||
redisHelper.del(loginRequest.getUuid());
|
|
||||||
if (StrUtil.isBlank(code)) {
|
|
||||||
throw new BadRequestException("验证码不存在或已过期");
|
|
||||||
}
|
|
||||||
if (StrUtil.isBlank(loginRequest.getCode()) || !loginRequest.getCode().equalsIgnoreCase(code)) {
|
|
||||||
throw new BadRequestException("验证码错误");
|
|
||||||
}
|
|
||||||
// 获取用户信息
|
// 获取用户信息
|
||||||
SystemUserJwtVo jwtUser = userDetailsService.loadUserByUsername(loginRequest.getUsername());
|
SystemUserJwtVo jwtUser = userDetailsService.loadUserByUsername(loginRequest.getUsername());
|
||||||
// 验证用户密码
|
// 验证用户密码
|
||||||
@@ -69,11 +56,10 @@ public class SystemAuthService {
|
|||||||
// 生成令牌
|
// 生成令牌
|
||||||
String token = tokenProvider.createToken(jwtUser);
|
String token = tokenProvider.createToken(jwtUser);
|
||||||
// 返回 token 与 用户信息
|
// 返回 token 与 用户信息
|
||||||
Map<String, Object> authInfo = new HashMap<>(2) {{
|
SystemAuthVo authInfo = new SystemAuthVo();
|
||||||
put("token", String.format("%s %s", SystemConst.TOKEN_PREFIX, token));
|
authInfo.setToken(String.format("%s %s", SystemConst.TOKEN_PREFIX, token));
|
||||||
// 这里是为了清空密码
|
// 这里是为了清空密码
|
||||||
put("user", KitBeanUtil.copyToClass(jwtUser, SystemUserInfoVo.class));
|
authInfo.setUser(KitBeanUtil.copyToClass(jwtUser, SystemUserInfoVo.class));
|
||||||
}};
|
|
||||||
if (properties.getLogin().isSingle()) {
|
if (properties.getLogin().isSingle()) {
|
||||||
// 踢掉之前已经登录的token
|
// 踢掉之前已经登录的token
|
||||||
systemUserOnlineInfoDAO.kickOutByUsername(loginRequest.getUsername());
|
systemUserOnlineInfoDAO.kickOutByUsername(loginRequest.getUsername());
|
||||||
@@ -92,26 +78,6 @@ public class SystemAuthService {
|
|||||||
return jwtUser;
|
return jwtUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Object> getCaptchaInfo() {
|
|
||||||
// 获取运算的结果
|
|
||||||
Captcha captcha = properties.getLogin().getCaptchaSetting().getCaptcha();
|
|
||||||
String uuid = SystemCacheKey.CAPTCHA_LOGIN + IdUtil.simpleUUID();
|
|
||||||
//当验证码类型为 arithmetic时且长度 >= 2 时, captcha.text()的结果有几率为浮点型
|
|
||||||
String captchaValue = captcha.text();
|
|
||||||
if (captcha.getCharType() - 1 == CaptchaCodeEnum.ARITHMETIC.ordinal() &&
|
|
||||||
captchaValue.contains(SystemConst.SYMBOL_DOT)) {
|
|
||||||
captchaValue = captchaValue.split("\\.")[0];
|
|
||||||
}
|
|
||||||
// 保存
|
|
||||||
redisHelper.set(uuid, captchaValue, properties.getLogin().getCaptchaSetting().getExpiration(),
|
|
||||||
TimeUnit.MINUTES);
|
|
||||||
// 验证码信息
|
|
||||||
return new HashMap<>(2) {{
|
|
||||||
put("img", captcha.toBase64());
|
|
||||||
put("uuid", uuid);
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
public void doLogout(HttpServletRequest request) {
|
public void doLogout(HttpServletRequest request) {
|
||||||
String token = tokenProvider.getToken(request);
|
String token = tokenProvider.getToken(request);
|
||||||
systemUserOnlineInfoDAO.logoutByToken(token);
|
systemUserOnlineInfoDAO.logoutByToken(token);
|
||||||
|
|||||||
+59
@@ -0,0 +1,59 @@
|
|||||||
|
package cn.odboy.system.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.odboy.constant.CaptchaCodeEnum;
|
||||||
|
import cn.odboy.constant.SystemConst;
|
||||||
|
import cn.odboy.framework.exception.BadRequestException;
|
||||||
|
import cn.odboy.framework.properties.AppProperties;
|
||||||
|
import cn.odboy.framework.redis.KitRedisHelper;
|
||||||
|
import cn.odboy.system.dal.model.SystemCaptchaVo;
|
||||||
|
import cn.odboy.system.dal.redis.SystemCacheKey;
|
||||||
|
import com.wf.captcha.base.Captcha;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证码
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class SystemCaptchaService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private KitRedisHelper redisHelper;
|
||||||
|
@Autowired
|
||||||
|
private AppProperties properties;
|
||||||
|
|
||||||
|
public SystemCaptchaVo getLoginCaptcha() {
|
||||||
|
// 获取运算的结果
|
||||||
|
Captcha captcha = properties.getLogin().getCaptchaSetting().getCaptcha();
|
||||||
|
String uuid = SystemCacheKey.CAPTCHA_LOGIN + IdUtil.simpleUUID();
|
||||||
|
//当验证码类型为 arithmetic时且长度 >= 2 时, captcha.text()的结果有几率为浮点型
|
||||||
|
String captchaValue = captcha.text();
|
||||||
|
if (captcha.getCharType() - 1 == CaptchaCodeEnum.ARITHMETIC.ordinal() &&
|
||||||
|
captchaValue.contains(SystemConst.SYMBOL_DOT)) {
|
||||||
|
captchaValue = captchaValue.split("\\.")[0];
|
||||||
|
}
|
||||||
|
// 保存
|
||||||
|
redisHelper.set(uuid, captchaValue, properties.getLogin().getCaptchaSetting().getExpiration(), TimeUnit.MINUTES);
|
||||||
|
// 验证码信息
|
||||||
|
SystemCaptchaVo captchaVo = new SystemCaptchaVo();
|
||||||
|
captchaVo.setUuid(uuid);
|
||||||
|
captchaVo.setImg(captcha.toBase64());
|
||||||
|
return captchaVo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validate(String uuid, String inputCode) {
|
||||||
|
// 查询验证码
|
||||||
|
String code = redisHelper.get(uuid, String.class);
|
||||||
|
if (StrUtil.isBlank(code)) {
|
||||||
|
throw new BadRequestException("验证码不存在或已过期");
|
||||||
|
}
|
||||||
|
// 清除验证码
|
||||||
|
redisHelper.del(uuid);
|
||||||
|
if (StrUtil.isBlank(inputCode) || !code.equalsIgnoreCase(inputCode)) {
|
||||||
|
throw new BadRequestException("验证码错误");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user