登出功能
This commit is contained in:
parent
82e4b54677
commit
309d583e99
@ -1,6 +1,6 @@
|
|||||||
package com.xxl.job.admin.client;
|
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.UserInfoRespDTO;
|
||||||
import com.xxl.job.admin.client.dto.user.UserUpdateReqDTO;
|
import com.xxl.job.admin.client.dto.user.UserUpdateReqDTO;
|
||||||
import com.xxl.job.admin.framework.security.core.LoginUser;
|
import com.xxl.job.admin.framework.security.core.LoginUser;
|
||||||
|
@ -2,13 +2,12 @@ package com.xxl.job.admin.controller;
|
|||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.xxl.job.admin.client.OAuth2Client;
|
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.CommonResult;
|
||||||
import com.xxl.job.admin.client.dto.oauth2.OAuth2AccessTokenRespDTO;
|
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 com.xxl.job.admin.framework.security.core.util.SecurityUtils;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@ -16,7 +15,8 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/auth")
|
@RequestMapping("/auth")
|
||||||
public class AuthController {
|
public class AuthController {
|
||||||
|
@Resource
|
||||||
|
private UserClient userClient;
|
||||||
@Resource
|
@Resource
|
||||||
private OAuth2Client oauth2Client;
|
private OAuth2Client oauth2Client;
|
||||||
|
|
||||||
@ -60,4 +60,14 @@ public class AuthController {
|
|||||||
return new CommonResult<>();
|
return new CommonResult<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得当前登录用户的基本信息
|
||||||
|
*
|
||||||
|
* @return 用户信息;注意,实际项目中,最好创建对应的 ResponseVO 类,只返回必要的字段
|
||||||
|
*/
|
||||||
|
@GetMapping("/get")
|
||||||
|
public CommonResult<UserInfoRespDTO> getUser() {
|
||||||
|
return userClient.getUser();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,10 +89,20 @@ public class CookieUtil {
|
|||||||
* @param key
|
* @param key
|
||||||
*/
|
*/
|
||||||
public static void remove(HttpServletRequest request, HttpServletResponse response, String key) {
|
public static void remove(HttpServletRequest request, HttpServletResponse response, String key) {
|
||||||
Cookie cookie = get(request, key);
|
remove(request, response, key, COOKIE_PATH);
|
||||||
if (cookie != null) {
|
|
||||||
set(response, key, "", null, COOKIE_PATH, 0, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -33,6 +33,8 @@ public class LoginService {
|
|||||||
public static final String LOGIN_IDENTITY_KEY = "XXL_JOB_LOGIN_IDENTITY";
|
public static final String LOGIN_IDENTITY_KEY = "XXL_JOB_LOGIN_IDENTITY";
|
||||||
public static final String ACCESS_TOKEN = "ACCESS_TOKEN";
|
public static final String ACCESS_TOKEN = "ACCESS_TOKEN";
|
||||||
public static final String REFRESH_TOKEN = "REFRESH_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
|
@Resource
|
||||||
private OAuth2Client oauth2Client;
|
private OAuth2Client oauth2Client;
|
||||||
@Resource
|
@Resource
|
||||||
@ -136,15 +138,13 @@ public class LoginService {
|
|||||||
public Boolean ifAuthorizedLogin(HttpServletRequest request, HttpServletResponse response) {
|
public Boolean ifAuthorizedLogin(HttpServletRequest request, HttpServletResponse response) {
|
||||||
String accessToken = CookieUtil.getValue(request, ACCESS_TOKEN);
|
String accessToken = CookieUtil.getValue(request, ACCESS_TOKEN);
|
||||||
String refreshToken = CookieUtil.getValue(request, REFRESH_TOKEN);
|
String refreshToken = CookieUtil.getValue(request, REFRESH_TOKEN);
|
||||||
// 如果未登录授权过
|
String loginState = CookieUtil.getValue(request, LUNDU_LOGIN);
|
||||||
if (accessToken == null && refreshToken == null) {
|
if (loginState == null) {
|
||||||
// 登录授权 需要跳转登录授权页面
|
CookieUtil.remove(request, response, ACCESS_TOKEN, COOKIE_PATH);
|
||||||
// 基于 token 构建登录用户
|
CookieUtil.remove(request, response, REFRESH_TOKEN, COOKIE_PATH);
|
||||||
LoginUser loginUser = buildLoginUserByToken(accessToken);
|
return false;
|
||||||
// 设置当前用户
|
} else if (accessToken == null && refreshToken == null) {
|
||||||
if (loginUser != null) {
|
// 如果未登录授权过 需要跳转登录授权页面
|
||||||
SecurityUtils.setLoginUser(loginUser, request);
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
} else if (accessToken == null) {
|
} 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);
|
CookieUtil.set(response, ACCESS_TOKEN, refreshData.getAccessToken(), null, "/xxl-job-admin", refreshData.getExpiresIn().intValue(), true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
// 基于 token 构建登录用户
|
||||||
|
LoginUser loginUser = buildLoginUserByToken(accessToken);
|
||||||
|
// 设置当前用户
|
||||||
|
if (loginUser != null) {
|
||||||
|
SecurityUtils.setLoginUser(loginUser, request);
|
||||||
|
}
|
||||||
// 令牌未过期不需要重新登录
|
// 令牌未过期不需要重新登录
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private LoginUser buildLoginUserByToken(String token) {
|
private LoginUser buildLoginUserByToken(String token) {
|
||||||
try {
|
try {
|
||||||
CommonResult<OAuth2CheckTokenRespDTO> accessTokenResult = oauth2Client.checkToken(token);
|
CommonResult<OAuth2CheckTokenRespDTO> accessTokenResult = oauth2Client.checkToken(token);
|
||||||
|
@ -1,5 +1,12 @@
|
|||||||
$(function(){
|
$(function(){
|
||||||
|
// 获取当前登录用户信息
|
||||||
|
$.get(base_url + "/auth/get", function(data, status) {
|
||||||
|
if (data.code !== 0) {
|
||||||
|
alert('获得个人信息失败,原因:' + result.msg)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$('#authUsername').text(data.data.nickname);
|
||||||
|
});
|
||||||
// logout
|
// logout
|
||||||
$("#logoutBtn").click(function(){
|
$("#logoutBtn").click(function(){
|
||||||
layer.confirm( I18n.logout_confirm , {
|
layer.confirm( I18n.logout_confirm , {
|
||||||
|
@ -84,7 +84,8 @@
|
|||||||
<#-- login user -->
|
<#-- login user -->
|
||||||
<li class="dropdown">
|
<li class="dropdown">
|
||||||
<a href="javascript:" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
|
<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>
|
<span class="caret"></span>
|
||||||
</a>
|
</a>
|
||||||
<ul class="dropdown-menu" role="menu">
|
<ul class="dropdown-menu" role="menu">
|
||||||
|
@ -86,7 +86,7 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 设置cookie
|
// 设置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";
|
document.cookie = "REFRESH_TOKEN=" + result.data.refresh_token + "; max-age=43000; path=/xxl-job-admin";
|
||||||
// 跳转回首页
|
// 跳转回首页
|
||||||
window.location.href = '/xxl-job-admin';
|
window.location.href = '/xxl-job-admin';
|
||||||
|
Loading…
Reference in New Issue
Block a user