【修改】 退出接口新增,筛选已经登录的全部用户进行退出

This commit is contained in:
dlaren
2025-08-17 15:53:20 +08:00
parent da33f0c690
commit c7f933029e
2 changed files with 22 additions and 11 deletions

View File

@@ -17,6 +17,7 @@ public enum LoginLogTypeEnum {
LOGOUT_SELF(200), // 自己主动登出 LOGOUT_SELF(200), // 自己主动登出
LOGOUT_DELETE(202), // 强制退出 LOGOUT_DELETE(202), // 强制退出
LOGOUT_ADMIN_SELF(203), // 自己主动登出
; ;
/** /**

View File

@@ -8,6 +8,7 @@ import pc.exam.pp.framework.common.enums.UserTypeEnum;
import pc.exam.pp.framework.common.pojo.CommonResult; import pc.exam.pp.framework.common.pojo.CommonResult;
import pc.exam.pp.framework.common.util.json.JsonUtils; import pc.exam.pp.framework.common.util.json.JsonUtils;
import pc.exam.pp.framework.security.config.SecurityProperties; import pc.exam.pp.framework.security.config.SecurityProperties;
import pc.exam.pp.framework.security.core.LoginUser;
import pc.exam.pp.framework.security.core.util.SecurityFrameworkUtils; import pc.exam.pp.framework.security.core.util.SecurityFrameworkUtils;
import pc.exam.pp.framework.tenant.core.aop.TenantIgnore; import pc.exam.pp.framework.tenant.core.aop.TenantIgnore;
import pc.exam.pp.module.infra.dal.dataobject.config.ConfigDO; import pc.exam.pp.module.infra.dal.dataobject.config.ConfigDO;
@@ -118,13 +119,20 @@ public class AuthController {
@PermitAll @PermitAll
@Operation(summary = "登出系统") @Operation(summary = "登出系统")
public CommonResult<Boolean> logout(HttpServletRequest request) { public CommonResult<Boolean> logout(HttpServletRequest request) {
String token = SecurityFrameworkUtils.obtainAuthorization(request, String loginUserName = SecurityFrameworkUtils.getLoginUserName();
securityProperties.getTokenHeader(), securityProperties.getTokenParameter()); // 查询所有已经登录的信息
if (StrUtil.isNotBlank(token)) { Set<String> oauth2_access_token_set = stringRedisTemplate.keys("oauth2_access_token:*");
for (String oauth2_access_token : oauth2_access_token_set) {
OAuth2AccessTokenDO oAuth2AccessTokenDO = JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(oauth2_access_token), OAuth2AccessTokenDO.class);
// 判断账号是否登录过
if (oAuth2AccessTokenDO != null && oAuth2AccessTokenDO.getUserInfo().get("username").equals(loginUserName)) {
String token = oauth2_access_token.split(":")[1];
authService.logout(token, LoginLogTypeEnum.LOGOUT_SELF.getType()); authService.logout(token, LoginLogTypeEnum.LOGOUT_SELF.getType());
} }
}
return success(true); return success(true);
} }
@PostMapping("/refreshLogout") @PostMapping("/refreshLogout")
@PermitAll @PermitAll
@TenantIgnore @TenantIgnore
@@ -139,13 +147,15 @@ public class AuthController {
for (String oauth2_access_token : oauth2_access_token_set) { for (String oauth2_access_token : oauth2_access_token_set) {
OAuth2AccessTokenDO oAuth2AccessTokenDO = JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(oauth2_access_token), OAuth2AccessTokenDO.class); OAuth2AccessTokenDO oAuth2AccessTokenDO = JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(oauth2_access_token), OAuth2AccessTokenDO.class);
// 判断账号是否登录过 // 判断账号是否登录过
if (oAuth2AccessTokenDO.getUserInfo().get("username").equals(loginoutVo.getUsername())) { if (oAuth2AccessTokenDO != null && oAuth2AccessTokenDO.getUserInfo().get("username").equals(loginoutVo.getUsername())) {
// 说明登录了,别的地方登录了,请重新登录 // 说明登录了,别的地方登录了,请重新登录
stringRedisTemplate.opsForValue().getAndDelete(oauth2_access_token); String token = oauth2_access_token.split(":")[1];
authService.logout(token, LoginLogTypeEnum.LOGOUT_ADMIN_SELF.getType());
} }
} }
return success("退出成功,请重新输入用户名密码登录!"); return success("退出成功,请重新输入用户名密码登录!");
} }
@PostMapping("/refresh-token") @PostMapping("/refresh-token")
@PermitAll @PermitAll
@Operation(summary = "刷新令牌") @Operation(summary = "刷新令牌")