diff --git a/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/client/UserClient.java b/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/client/UserClient.java index 884b164b0..d794b549d 100644 --- a/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/client/UserClient.java +++ b/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/client/UserClient.java @@ -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; diff --git a/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/controller/AuthController.java b/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/controller/AuthController.java index 03e4a3245..4f573bfce 100644 --- a/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/controller/AuthController.java +++ b/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/controller/AuthController.java @@ -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 getUser() { + return userClient.getUser(); + } } diff --git a/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/core/util/CookieUtil.java b/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/core/util/CookieUtil.java index 870133f5f..503680342 100644 --- a/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/core/util/CookieUtil.java +++ b/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/core/util/CookieUtil.java @@ -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); + } + } } \ No newline at end of file diff --git a/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/service/LoginService.java b/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/service/LoginService.java index 0b5b7cb0d..508451822 100644 --- a/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/service/LoginService.java +++ b/ludu-job-admin/ludu-job-admin-biz/src/main/java/com/xxl/job/admin/service/LoginService.java @@ -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 accessTokenResult = oauth2Client.checkToken(token); diff --git a/ludu-job-admin/ludu-job-admin-biz/src/main/resources/static/js/common.1.js b/ludu-job-admin/ludu-job-admin-biz/src/main/resources/static/js/common.1.js index 619245e3a..09c840da4 100644 --- a/ludu-job-admin/ludu-job-admin-biz/src/main/resources/static/js/common.1.js +++ b/ludu-job-admin/ludu-job-admin-biz/src/main/resources/static/js/common.1.js @@ -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 , { diff --git a/ludu-job-admin/ludu-job-admin-biz/src/main/resources/templates/common/common.macro.ftl b/ludu-job-admin/ludu-job-admin-biz/src/main/resources/templates/common/common.macro.ftl index f8b193861..c02fe2e79 100644 --- a/ludu-job-admin/ludu-job-admin-biz/src/main/resources/templates/common/common.macro.ftl +++ b/ludu-job-admin/ludu-job-admin-biz/src/main/resources/templates/common/common.macro.ftl @@ -84,7 +84,8 @@ <#-- login user -->