移动端token策略处理

This commit is contained in:
syruan 2024-04-08 15:08:12 +08:00
parent a40363f3e5
commit 035de35c26
4 changed files with 33 additions and 2 deletions

View File

@ -59,6 +59,19 @@ public class LoginUser implements Serializable
*/ */
private SysUser sysUser; private SysUser sysUser;
/**
* 登录方式 移动端mobile 电脑端pc
*/
private String loginMethod;
public String getLoginMethod() {
return loginMethod;
}
public void setLoginMethod(String loginMethod) {
this.loginMethod = loginMethod;
}
public String getToken() public String getToken()
{ {
return token; return token;

View File

@ -79,7 +79,12 @@ public class TokenController {
public R<?> loginApp(@RequestBody LoginBody form) { public R<?> loginApp(@RequestBody LoginBody form) {
// 用户登录 // 用户登录
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword()); LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
return R.ok(tokenService.createToken(userInfo)); if (userInfo != null) {
userInfo.setLoginMethod("mobile");
return R.ok(tokenService.createToken(userInfo));
} else {
return R.fail("登录信息为空,请重试");
}
} }
@PostMapping("sendCode") @PostMapping("sendCode")
@ -93,6 +98,7 @@ public class TokenController {
// 校验验证码 // 校验验证码
LoginUser loginUser = sysLoginService.loginCode(form.getPhone(), form.getCode()); LoginUser loginUser = sysLoginService.loginCode(form.getPhone(), form.getCode());
if (StringUtils.isNotNull(loginUser)) { if (StringUtils.isNotNull(loginUser)) {
loginUser.setLoginMethod("mobile");
// 创建token // 创建token
Map<String, Object> tokenMap = tokenService.createToken(loginUser); Map<String, Object> tokenMap = tokenService.createToken(loginUser);
return R.ok(tokenService.createToken(loginUser)); return R.ok(tokenService.createToken(loginUser));

View File

@ -12,6 +12,11 @@ public class CacheConstants
*/ */
public final static long EXPIRATION = 720; public final static long EXPIRATION = 720;
/**
* 移动端缓存有效期48小时
*/
public final static long EXPIRATION_MOBILE = 2880;
/** /**
* 缓存刷新时间默认120分钟 * 缓存刷新时间默认120分钟
*/ */

View File

@ -39,6 +39,8 @@ public class TokenService
private final static long EXPIRE_TIME = CacheConstants.EXPIRATION; private final static long EXPIRE_TIME = CacheConstants.EXPIRATION;
private final static long EXPIRATION_MOBILE = CacheConstants.EXPIRATION_MOBILE;
private final static String ACCESS_TOKEN = CacheConstants.LOGIN_TOKEN_KEY; private final static String ACCESS_TOKEN = CacheConstants.LOGIN_TOKEN_KEY;
private final static Long MILLIS_MINUTE_TEN = CacheConstants.REFRESH_TIME * MILLIS_MINUTE; private final static Long MILLIS_MINUTE_TEN = CacheConstants.REFRESH_TIME * MILLIS_MINUTE;
@ -66,7 +68,11 @@ public class TokenService
// 接口返回信息 // 接口返回信息
Map<String, Object> rspMap = new HashMap<String, Object>(16); Map<String, Object> rspMap = new HashMap<String, Object>(16);
rspMap.put("access_token", JwtUtils.createToken(claimsMap)); rspMap.put("access_token", JwtUtils.createToken(claimsMap));
rspMap.put("expires_in", EXPIRE_TIME); if (loginUser.getLoginMethod() != null && "mobile".equals(loginUser.getLoginMethod())) {
rspMap.put("expires_in", EXPIRATION_MOBILE);
} else {
rspMap.put("expires_in", EXPIRE_TIME);
}
//密码置空后返回 //密码置空后返回
loginUser.getSysUser().setPassword(""); loginUser.getSysUser().setPassword("");
rspMap.put("login_user", loginUser); rspMap.put("login_user", loginUser);
@ -74,6 +80,7 @@ public class TokenService
return rspMap; return rspMap;
} }
/** /**
* 获取用户身份信息 * 获取用户身份信息
* *