登出功能

This commit is contained in:
XinWei 2024-07-17 15:52:20 +08:00
parent 82e4b54677
commit 309d583e99
7 changed files with 57 additions and 22 deletions

View File

@ -1,6 +1,6 @@
package com.xxl.job.admin.client;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.xxl.job.admin.client.dto.CommonResult;
import com.xxl.job.admin.client.dto.user.UserInfoRespDTO;
import com.xxl.job.admin.client.dto.user.UserUpdateReqDTO;
import com.xxl.job.admin.framework.security.core.LoginUser;

View File

@ -2,13 +2,12 @@ package com.xxl.job.admin.controller;
import cn.hutool.core.util.StrUtil;
import com.xxl.job.admin.client.OAuth2Client;
import com.xxl.job.admin.client.UserClient;
import com.xxl.job.admin.client.dto.CommonResult;
import com.xxl.job.admin.client.dto.oauth2.OAuth2AccessTokenRespDTO;
import com.xxl.job.admin.client.dto.user.UserInfoRespDTO;
import com.xxl.job.admin.framework.security.core.util.SecurityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@ -16,7 +15,8 @@ import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/auth")
public class AuthController {
@Resource
private UserClient userClient;
@Resource
private OAuth2Client oauth2Client;
@ -60,4 +60,14 @@ public class AuthController {
return new CommonResult<>();
}
/**
* 获得当前登录用户的基本信息
*
* @return 用户信息注意实际项目中最好创建对应的 ResponseVO 只返回必要的字段
*/
@GetMapping("/get")
public CommonResult<UserInfoRespDTO> getUser() {
return userClient.getUser();
}
}

View File

@ -89,10 +89,20 @@ public class CookieUtil {
* @param key
*/
public static void remove(HttpServletRequest request, HttpServletResponse response, String key) {
Cookie cookie = get(request, key);
if (cookie != null) {
set(response, key, "", null, COOKIE_PATH, 0, true);
}
remove(request, response, key, COOKIE_PATH);
}
/**
* 删除Cookie自定义作用范围路径
*
* @param request
* @param response
* @param key
*/
public static void remove(HttpServletRequest request, HttpServletResponse response, String key, String path) {
Cookie cookie = get(request, key);
if (cookie != null) {
set(response, key, "", null, path, 0, true);
}
}
}

View File

@ -33,6 +33,8 @@ public class LoginService {
public static final String LOGIN_IDENTITY_KEY = "XXL_JOB_LOGIN_IDENTITY";
public static final String ACCESS_TOKEN = "ACCESS_TOKEN";
public static final String REFRESH_TOKEN = "REFRESH_TOKEN";
public static final String LUNDU_LOGIN = "LUNDU_LOGIN";
public static final String COOKIE_PATH = "/xxl-job-admin";
@Resource
private OAuth2Client oauth2Client;
@Resource
@ -136,15 +138,13 @@ public class LoginService {
public Boolean ifAuthorizedLogin(HttpServletRequest request, HttpServletResponse response) {
String accessToken = CookieUtil.getValue(request, ACCESS_TOKEN);
String refreshToken = CookieUtil.getValue(request, REFRESH_TOKEN);
// 如果未登录授权过
if (accessToken == null && refreshToken == null) {
// 登录授权 需要跳转登录授权页面
// 基于 token 构建登录用户
LoginUser loginUser = buildLoginUserByToken(accessToken);
// 设置当前用户
if (loginUser != null) {
SecurityUtils.setLoginUser(loginUser, request);
}
String loginState = CookieUtil.getValue(request, LUNDU_LOGIN);
if (loginState == null) {
CookieUtil.remove(request, response, ACCESS_TOKEN, COOKIE_PATH);
CookieUtil.remove(request, response, REFRESH_TOKEN, COOKIE_PATH);
return false;
} else if (accessToken == null && refreshToken == null) {
// 如果未登录授权过 需要跳转登录授权页面
return false;
} else if (accessToken == null) {
// 刷新令牌 不需要跳转登录授权页面
@ -152,9 +152,16 @@ public class LoginService {
CookieUtil.set(response, ACCESS_TOKEN, refreshData.getAccessToken(), null, "/xxl-job-admin", refreshData.getExpiresIn().intValue(), true);
return true;
}
// 基于 token 构建登录用户
LoginUser loginUser = buildLoginUserByToken(accessToken);
// 设置当前用户
if (loginUser != null) {
SecurityUtils.setLoginUser(loginUser, request);
}
// 令牌未过期不需要重新登录
return true;
}
private LoginUser buildLoginUserByToken(String token) {
try {
CommonResult<OAuth2CheckTokenRespDTO> accessTokenResult = oauth2Client.checkToken(token);

View File

@ -1,5 +1,12 @@
$(function(){
// 获取当前登录用户信息
$.get(base_url + "/auth/get", function(data, status) {
if (data.code !== 0) {
alert('获得个人信息失败,原因:' + result.msg)
return;
}
$('#authUsername').text(data.data.nickname);
});
// logout
$("#logoutBtn").click(function(){
layer.confirm( I18n.logout_confirm , {

View File

@ -84,7 +84,8 @@
<#-- login user -->
<li class="dropdown">
<a href="javascript:" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
${I18n.system_welcome} ${Request["XXL_JOB_LOGIN_IDENTITY"].username}
<#-- ${I18n.system_welcome} ${Request["XXL_JOB_LOGIN_IDENTITY"].username}-->
${I18n.system_welcome} <span id="authUsername"></span>
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">

View File

@ -86,7 +86,7 @@
return;
}
// 设置cookie
document.cookie = "ACCESS_TOKEN=" + result.data.access_token + "; max-age=" + result.data.expires_in + "; path=/xxl-job-admin";
document.cookie = "ACCESS_TOKEN=" + result.data.access_token + "; max-age=" + (result.data.expires_in - 20) + "; path=/xxl-job-admin";
document.cookie = "REFRESH_TOKEN=" + result.data.refresh_token + "; max-age=43000; path=/xxl-job-admin";
// 跳转回首页
window.location.href = '/xxl-job-admin';