学员安全登出

This commit is contained in:
none
2023-03-21 14:57:00 +08:00
parent ee248b483d
commit ad02bcad59
11 changed files with 114 additions and 49 deletions

View File

@@ -11,5 +11,9 @@ public interface JWTService {
void logout(String token, String prv) throws JwtLogoutException;
void userLogout(String token) throws JwtLogoutException;
void adminUserLogout(String token) throws JwtLogoutException;
JWTPayload parse(String token, String prv) throws JwtLogoutException;
}

View File

@@ -12,4 +12,6 @@ public interface UserLoginRecordService extends IService<UserLoginRecord> {
UserLoginRecord store(Integer userId, String jti, Long expired, String ip, String ipArea, String browser, String browserVersion, String os);
void saveIpArea(Integer id, String area);
void logout(Integer userid, String jti);
}

View File

@@ -7,6 +7,8 @@ import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import xyz.playedu.api.constant.FrontendConstant;
import xyz.playedu.api.constant.SystemConstant;
import xyz.playedu.api.exception.JwtLogoutException;
import xyz.playedu.api.service.JWTService;
import xyz.playedu.api.types.JWTPayload;
@@ -82,6 +84,16 @@ public class JwtServiceImpl implements JWTService {
RedisUtil.set(cacheKey, 1, expire);
}
@Override
public void userLogout(String token) throws JwtLogoutException {
logout(token, SystemConstant.JWT_PRV_USER);
}
@Override
public void adminUserLogout(String token) throws JwtLogoutException {
logout(token, SystemConstant.JWT_PRV_ADMIN_USER);
}
private Claims parseToken(String token, String prv) throws JwtLogoutException {
Claims claims = (Claims) Jwts.parserBuilder().setSigningKey(getSecretKey()).require("prv", prv).build().parse(token).getBody();
if (isInBlack(claims.getId())) {

View File

@@ -36,6 +36,19 @@ public class UserLoginRecordServiceImpl extends ServiceImpl<UserLoginRecordMappe
record.setIpArea(area);
updateById(record);
}
@Override
public void logout(Integer userid, String jti) {
UserLoginRecord record = getOne(query().getWrapper().eq("user_id", userid).eq("jti", jti).eq("is_logout", 0));
if (record == null) {
return;
}
UserLoginRecord newRecord = new UserLoginRecord();
newRecord.setId(record.getId());
newRecord.setIsLogout(1);
updateById(newRecord);
}
}