From d322e781998efb424ede22d31793b56ba264a0dd Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 22 May 2022 20:09:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=20Token=E3=80=81=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E3=80=81=E5=AF=86=E7=A0=81=E7=AD=89=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=EF=BC=8C=E7=BB=9F=E4=B8=80=E4=BD=BF=E7=94=A8=20LocalStorage=20?= =?UTF-8?q?=E6=9B=BF=E4=BB=A3=20Cookie=20=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/auth/MemberAuthServiceImpl.java | 2 +- .../service/auth/AdminAuthServiceImpl.java | 2 +- yudao-ui-admin/src/utils/auth.js | 88 +++++++++++++++++-- yudao-ui-admin/src/utils/request.js | 13 ++- yudao-ui-admin/src/views/login.vue | 55 ++++++------ yudao-ui-admin/src/views/socialLogin.vue | 56 +++++++++--- 6 files changed, 159 insertions(+), 57 deletions(-) diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java index a1d5b4d0b4..5911d81c38 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java @@ -100,7 +100,7 @@ public class MemberAuthServiceImpl implements MemberAuthService { } // 创建 Token 令牌,记录登录日志 - return createTokenAfterLoginSuccess(user, null, LoginLogTypeEnum.LOGIN_SOCIAL); + return createTokenAfterLoginSuccess(user, user.getMobile(), LoginLogTypeEnum.LOGIN_SOCIAL); } @Override diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java index 66c00a2330..b85e0e1829 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java @@ -180,7 +180,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { } // 创建 Token 令牌,记录登录日志 - return createTokenAfterLoginSuccess(user.getId(), null, LoginLogTypeEnum.LOGIN_SOCIAL); + return createTokenAfterLoginSuccess(user.getId(), user.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL); } @Override diff --git a/yudao-ui-admin/src/utils/auth.js b/yudao-ui-admin/src/utils/auth.js index 0c00919edd..15bbf661e0 100644 --- a/yudao-ui-admin/src/utils/auth.js +++ b/yudao-ui-admin/src/utils/auth.js @@ -1,22 +1,96 @@ -import Cookies from 'js-cookie' +import {decrypt, encrypt} from "@/utils/jsencrypt"; const AccessTokenKey = 'ACCESS_TOKEN' const RefreshTokenKey = 'REFRESH_TOKEN' +// ========== Token 相关 ========== + export function getAccessToken() { - return Cookies.get(AccessTokenKey) + return localStorage.getItem(AccessTokenKey) } export function getRefreshToken() { - return Cookies.get(RefreshTokenKey) + return localStorage.getItem(RefreshTokenKey) } export function setToken(token) { - Cookies.set(AccessTokenKey, token.accessToken) - Cookies.set(RefreshTokenKey, token.refreshToken) + localStorage.setItem(AccessTokenKey, token.accessToken) + localStorage.setItem(RefreshTokenKey, token.refreshToken) } export function removeToken() { - Cookies.remove(AccessTokenKey) - Cookies.remove(RefreshTokenKey) + localStorage.removeItem(AccessTokenKey) + localStorage.removeItem(RefreshTokenKey) +} + +// ========== 账号相关 ========== + +const UsernameKey = 'USERNAME' +const PasswordKey = 'PASSWORD' +const RememberMeKey = 'REMEMBER_ME' + +export function getUsername() { + return localStorage.getItem(UsernameKey) +} + +export function setUsername(username) { + localStorage.setItem(UsernameKey, username) +} + +export function removeUsername() { + localStorage.removeItem(UsernameKey) +} + +export function getPassword() { + const password = localStorage.getItem(PasswordKey) + return password ? decrypt(password) : undefined +} + +export function setPassword(password) { + localStorage.setItem(PasswordKey, encrypt(password)) +} + +export function removePassword() { + localStorage.removeItem(PasswordKey) +} + +export function getRememberMe() { + return localStorage.getItem(RememberMeKey) === 'true' +} + +export function setRememberMe(rememberMe) { + localStorage.setItem(RememberMeKey, rememberMe) +} + +export function removeRememberMe() { + localStorage.removeItem(RememberMeKey) +} + +// ========== 租户相关 ========== + +const TenantIdKey = 'TENANT_ID' +const TenantNameKey = 'TENANT_NAME' + +export function getTenantName() { + return localStorage.getItem(TenantNameKey) +} + +export function setTenantName(username) { + localStorage.setItem(TenantNameKey, username) +} + +export function removeTenantName() { + localStorage.removeItem(TenantNameKey) +} + +export function getTenantId() { + return localStorage.getItem(TenantIdKey) +} + +export function setTenantId(username) { + localStorage.setItem(TenantIdKey, username) +} + +export function removeTenantId() { + localStorage.removeItem(TenantIdKey) } diff --git a/yudao-ui-admin/src/utils/request.js b/yudao-ui-admin/src/utils/request.js index ce33675c4b..b42cffd013 100644 --- a/yudao-ui-admin/src/utils/request.js +++ b/yudao-ui-admin/src/utils/request.js @@ -1,9 +1,8 @@ import axios from 'axios' import {Message, MessageBox, Notification} from 'element-ui' import store from '@/store' -import {getAccessToken, getRefreshToken, setToken} from '@/utils/auth' +import {getAccessToken, getRefreshToken, getTenantId, setToken} from '@/utils/auth' import errorCode from '@/utils/errorCode' -import Cookies from "js-cookie"; import {getPath, getTenantEnable} from "@/utils/ruoyi"; import {refreshToken} from "@/api/login"; @@ -21,14 +20,15 @@ let requestList = [] // 是否正在刷新中 let isRefreshToken = false - axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8' // 创建axios实例 const service = axios.create({ // axios中请求配置有baseURL选项,表示请求URL公共部分 baseURL: process.env.VUE_APP_BASE_API + '/admin-api/', // 此处的 /admin-api/ 地址,原因是后端的基础路径为 /admin-api/ // 超时 - timeout: 30000 + timeout: 30000, + // 禁用 Cookie 等信息 + withCredentials: false, }) // request拦截器 service.interceptors.request.use(config => { @@ -39,7 +39,7 @@ service.interceptors.request.use(config => { } // 设置租户 if (getTenantEnable()) { - const tenantId = Cookies.get('tenantId'); + const tenantId = getTenantId(); if (tenantId) { config.headers['tenant-id'] = tenantId; } @@ -79,7 +79,6 @@ service.interceptors.response.use(async res => { // 获取错误信息 const msg = res.data.msg || errorCode[code] || errorCode['default'] if (ignoreMsgs.indexOf(msg) !== -1) { // 如果是忽略的错误码,直接返回 msg 异常 - console.log('132312311'); return Promise.reject(msg) } else if (code === 401) { // 如果未认证,并且未进行刷新令牌,说明可能是访问令牌过期了 @@ -166,7 +165,7 @@ service.interceptors.response.use(async res => { export function getBaseHeader() { return { 'Authorization': "Bearer " + getAccessToken(), - 'tenant-id': Cookies.get('tenantId'), + 'tenant-id': getTenantId(), } } diff --git a/yudao-ui-admin/src/views/login.vue b/yudao-ui-admin/src/views/login.vue index c25fcc5ae7..0344210713 100644 --- a/yudao-ui-admin/src/views/login.vue +++ b/yudao-ui-admin/src/views/login.vue @@ -107,9 +107,17 @@ import {getCodeImg, sendSmsCode, socialAuthRedirect} from "@/api/login"; import {getTenantIdByName} from "@/api/system/tenant"; import Cookies from "js-cookie"; -import {decrypt, encrypt} from '@/utils/jsencrypt' import {SystemUserSocialTypeEnum} from "@/utils/constants"; import {getTenantEnable} from "@/utils/ruoyi"; +import { + getPassword, + getRememberMe, getTenantName, + getUsername, + removePassword, removeRememberMe, removeTenantName, + removeUsername, + setPassword, setRememberMe, setTenantId, setTenantName, + setUsername +} from "@/utils/auth"; export default { name: "Login", @@ -161,7 +169,7 @@ export default { const tenantId = res.data; if (tenantId && tenantId >= 0) { // 设置租户 - Cookies.set("tenantId", tenantId); + setTenantId(tenantId) callback(); } else { callback('租户不存在'); @@ -172,8 +180,6 @@ export default { } ] }, - - loading: false, redirect: undefined, // 枚举 @@ -213,21 +219,16 @@ export default { }); }, getCookie() { - const username = Cookies.get("username"); - const password = Cookies.get("password"); - const rememberMe = Cookies.get('rememberMe') - const tenantName = Cookies.get('tenantName'); - const mobile = Cookies.get('mobile'); - const mobileCode = Cookies.get('mobileCode'); - const loginType = Cookies.get('loginType'); + const username = getUsername(); + const password = getPassword(); + const rememberMe = getRememberMe(); + const tenantName = getTenantName(); this.loginForm = { - username: username === undefined ? this.loginForm.username : username, - password: password === undefined ? this.loginForm.password : decrypt(password), - rememberMe: rememberMe === undefined ? false : Boolean(rememberMe), - tenantName: tenantName === undefined ? this.loginForm.tenantName : tenantName, - mobile: mobile === undefined ? this.loginForm.mobile : mobile, - mobileCode: mobileCode === undefined ? this.loginForm.mobileCode : mobileCode, - loginType: loginType === undefined ? this.loginForm.loginType : loginType, + ...this.loginForm, + username: username ? username : this.loginForm.username, + password: password ? password : this.loginForm.password, + rememberMe: rememberMe ? getRememberMe() : false, + tenantName: tenantName ? tenantName : this.loginForm.tenantName, }; }, handleLogin() { @@ -236,18 +237,18 @@ export default { this.loading = true; // 设置 Cookie if (this.loginForm.rememberMe) { - Cookies.set("username", this.loginForm.username, {expires: 30}); - Cookies.set("password", encrypt(this.loginForm.password), {expires: 30}); - Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30}); - Cookies.set('tenantName', this.loginForm.tenantName, {expires: 30}); + setUsername(this.loginForm.username) + setPassword(this.loginForm.password) + setRememberMe(this.loginForm.rememberMe) + setTenantName(this.loginForm.tenantName) } else { - Cookies.remove("username"); - Cookies.remove("password"); - Cookies.remove('rememberMe'); - Cookies.remove('tenantName'); + removeUsername() + removePassword() + removeRememberMe() + removeTenantName() } // 发起登陆 - console.log("发起登录", this.loginForm); + // console.log("发起登录", this.loginForm); this.$store.dispatch(this.loginForm.loginType === "sms" ? "SmsLogin" : "Login", this.loginForm).then(() => { this.$router.push({path: this.redirect || "/"}).catch(() => { }); diff --git a/yudao-ui-admin/src/views/socialLogin.vue b/yudao-ui-admin/src/views/socialLogin.vue index caab08e0e1..1d18e6011e 100644 --- a/yudao-ui-admin/src/views/socialLogin.vue +++ b/yudao-ui-admin/src/views/socialLogin.vue @@ -19,7 +19,7 @@
-