前端 Token、账号、密码等信息,统一使用 LocalStorage 替代 Cookie 存储
parent
7926ea9896
commit
d322e78199
|
@ -100,7 +100,7 @@ public class MemberAuthServiceImpl implements MemberAuthService {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建 Token 令牌,记录登录日志
|
// 创建 Token 令牌,记录登录日志
|
||||||
return createTokenAfterLoginSuccess(user, null, LoginLogTypeEnum.LOGIN_SOCIAL);
|
return createTokenAfterLoginSuccess(user, user.getMobile(), LoginLogTypeEnum.LOGIN_SOCIAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -180,7 +180,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建 Token 令牌,记录登录日志
|
// 创建 Token 令牌,记录登录日志
|
||||||
return createTokenAfterLoginSuccess(user.getId(), null, LoginLogTypeEnum.LOGIN_SOCIAL);
|
return createTokenAfterLoginSuccess(user.getId(), user.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,22 +1,96 @@
|
||||||
import Cookies from 'js-cookie'
|
import {decrypt, encrypt} from "@/utils/jsencrypt";
|
||||||
|
|
||||||
const AccessTokenKey = 'ACCESS_TOKEN'
|
const AccessTokenKey = 'ACCESS_TOKEN'
|
||||||
const RefreshTokenKey = 'REFRESH_TOKEN'
|
const RefreshTokenKey = 'REFRESH_TOKEN'
|
||||||
|
|
||||||
|
// ========== Token 相关 ==========
|
||||||
|
|
||||||
export function getAccessToken() {
|
export function getAccessToken() {
|
||||||
return Cookies.get(AccessTokenKey)
|
return localStorage.getItem(AccessTokenKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRefreshToken() {
|
export function getRefreshToken() {
|
||||||
return Cookies.get(RefreshTokenKey)
|
return localStorage.getItem(RefreshTokenKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setToken(token) {
|
export function setToken(token) {
|
||||||
Cookies.set(AccessTokenKey, token.accessToken)
|
localStorage.setItem(AccessTokenKey, token.accessToken)
|
||||||
Cookies.set(RefreshTokenKey, token.refreshToken)
|
localStorage.setItem(RefreshTokenKey, token.refreshToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function removeToken() {
|
export function removeToken() {
|
||||||
Cookies.remove(AccessTokenKey)
|
localStorage.removeItem(AccessTokenKey)
|
||||||
Cookies.remove(RefreshTokenKey)
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import {Message, MessageBox, Notification} from 'element-ui'
|
import {Message, MessageBox, Notification} from 'element-ui'
|
||||||
import store from '@/store'
|
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 errorCode from '@/utils/errorCode'
|
||||||
import Cookies from "js-cookie";
|
|
||||||
import {getPath, getTenantEnable} from "@/utils/ruoyi";
|
import {getPath, getTenantEnable} from "@/utils/ruoyi";
|
||||||
import {refreshToken} from "@/api/login";
|
import {refreshToken} from "@/api/login";
|
||||||
|
|
||||||
|
@ -21,14 +20,15 @@ let requestList = []
|
||||||
// 是否正在刷新中
|
// 是否正在刷新中
|
||||||
let isRefreshToken = false
|
let isRefreshToken = false
|
||||||
|
|
||||||
|
|
||||||
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
|
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
|
||||||
// 创建axios实例
|
// 创建axios实例
|
||||||
const service = axios.create({
|
const service = axios.create({
|
||||||
// axios中请求配置有baseURL选项,表示请求URL公共部分
|
// axios中请求配置有baseURL选项,表示请求URL公共部分
|
||||||
baseURL: process.env.VUE_APP_BASE_API + '/admin-api/', // 此处的 /admin-api/ 地址,原因是后端的基础路径为 /admin-api/
|
baseURL: process.env.VUE_APP_BASE_API + '/admin-api/', // 此处的 /admin-api/ 地址,原因是后端的基础路径为 /admin-api/
|
||||||
// 超时
|
// 超时
|
||||||
timeout: 30000
|
timeout: 30000,
|
||||||
|
// 禁用 Cookie 等信息
|
||||||
|
withCredentials: false,
|
||||||
})
|
})
|
||||||
// request拦截器
|
// request拦截器
|
||||||
service.interceptors.request.use(config => {
|
service.interceptors.request.use(config => {
|
||||||
|
@ -39,7 +39,7 @@ service.interceptors.request.use(config => {
|
||||||
}
|
}
|
||||||
// 设置租户
|
// 设置租户
|
||||||
if (getTenantEnable()) {
|
if (getTenantEnable()) {
|
||||||
const tenantId = Cookies.get('tenantId');
|
const tenantId = getTenantId();
|
||||||
if (tenantId) {
|
if (tenantId) {
|
||||||
config.headers['tenant-id'] = 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']
|
const msg = res.data.msg || errorCode[code] || errorCode['default']
|
||||||
if (ignoreMsgs.indexOf(msg) !== -1) { // 如果是忽略的错误码,直接返回 msg 异常
|
if (ignoreMsgs.indexOf(msg) !== -1) { // 如果是忽略的错误码,直接返回 msg 异常
|
||||||
console.log('132312311');
|
|
||||||
return Promise.reject(msg)
|
return Promise.reject(msg)
|
||||||
} else if (code === 401) {
|
} else if (code === 401) {
|
||||||
// 如果未认证,并且未进行刷新令牌,说明可能是访问令牌过期了
|
// 如果未认证,并且未进行刷新令牌,说明可能是访问令牌过期了
|
||||||
|
@ -166,7 +165,7 @@ service.interceptors.response.use(async res => {
|
||||||
export function getBaseHeader() {
|
export function getBaseHeader() {
|
||||||
return {
|
return {
|
||||||
'Authorization': "Bearer " + getAccessToken(),
|
'Authorization': "Bearer " + getAccessToken(),
|
||||||
'tenant-id': Cookies.get('tenantId'),
|
'tenant-id': getTenantId(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -107,9 +107,17 @@
|
||||||
import {getCodeImg, sendSmsCode, socialAuthRedirect} from "@/api/login";
|
import {getCodeImg, sendSmsCode, socialAuthRedirect} from "@/api/login";
|
||||||
import {getTenantIdByName} from "@/api/system/tenant";
|
import {getTenantIdByName} from "@/api/system/tenant";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import {decrypt, encrypt} from '@/utils/jsencrypt'
|
|
||||||
import {SystemUserSocialTypeEnum} from "@/utils/constants";
|
import {SystemUserSocialTypeEnum} from "@/utils/constants";
|
||||||
import {getTenantEnable} from "@/utils/ruoyi";
|
import {getTenantEnable} from "@/utils/ruoyi";
|
||||||
|
import {
|
||||||
|
getPassword,
|
||||||
|
getRememberMe, getTenantName,
|
||||||
|
getUsername,
|
||||||
|
removePassword, removeRememberMe, removeTenantName,
|
||||||
|
removeUsername,
|
||||||
|
setPassword, setRememberMe, setTenantId, setTenantName,
|
||||||
|
setUsername
|
||||||
|
} from "@/utils/auth";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "Login",
|
name: "Login",
|
||||||
|
@ -161,7 +169,7 @@ export default {
|
||||||
const tenantId = res.data;
|
const tenantId = res.data;
|
||||||
if (tenantId && tenantId >= 0) {
|
if (tenantId && tenantId >= 0) {
|
||||||
// 设置租户
|
// 设置租户
|
||||||
Cookies.set("tenantId", tenantId);
|
setTenantId(tenantId)
|
||||||
callback();
|
callback();
|
||||||
} else {
|
} else {
|
||||||
callback('租户不存在');
|
callback('租户不存在');
|
||||||
|
@ -172,8 +180,6 @@ export default {
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
loading: false,
|
loading: false,
|
||||||
redirect: undefined,
|
redirect: undefined,
|
||||||
// 枚举
|
// 枚举
|
||||||
|
@ -213,21 +219,16 @@ export default {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
getCookie() {
|
getCookie() {
|
||||||
const username = Cookies.get("username");
|
const username = getUsername();
|
||||||
const password = Cookies.get("password");
|
const password = getPassword();
|
||||||
const rememberMe = Cookies.get('rememberMe')
|
const rememberMe = getRememberMe();
|
||||||
const tenantName = Cookies.get('tenantName');
|
const tenantName = getTenantName();
|
||||||
const mobile = Cookies.get('mobile');
|
|
||||||
const mobileCode = Cookies.get('mobileCode');
|
|
||||||
const loginType = Cookies.get('loginType');
|
|
||||||
this.loginForm = {
|
this.loginForm = {
|
||||||
username: username === undefined ? this.loginForm.username : username,
|
...this.loginForm,
|
||||||
password: password === undefined ? this.loginForm.password : decrypt(password),
|
username: username ? username : this.loginForm.username,
|
||||||
rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
|
password: password ? password : this.loginForm.password,
|
||||||
tenantName: tenantName === undefined ? this.loginForm.tenantName : tenantName,
|
rememberMe: rememberMe ? getRememberMe() : false,
|
||||||
mobile: mobile === undefined ? this.loginForm.mobile : mobile,
|
tenantName: tenantName ? tenantName : this.loginForm.tenantName,
|
||||||
mobileCode: mobileCode === undefined ? this.loginForm.mobileCode : mobileCode,
|
|
||||||
loginType: loginType === undefined ? this.loginForm.loginType : loginType,
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
handleLogin() {
|
handleLogin() {
|
||||||
|
@ -236,18 +237,18 @@ export default {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
// 设置 Cookie
|
// 设置 Cookie
|
||||||
if (this.loginForm.rememberMe) {
|
if (this.loginForm.rememberMe) {
|
||||||
Cookies.set("username", this.loginForm.username, {expires: 30});
|
setUsername(this.loginForm.username)
|
||||||
Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
|
setPassword(this.loginForm.password)
|
||||||
Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
|
setRememberMe(this.loginForm.rememberMe)
|
||||||
Cookies.set('tenantName', this.loginForm.tenantName, {expires: 30});
|
setTenantName(this.loginForm.tenantName)
|
||||||
} else {
|
} else {
|
||||||
Cookies.remove("username");
|
removeUsername()
|
||||||
Cookies.remove("password");
|
removePassword()
|
||||||
Cookies.remove('rememberMe');
|
removeRememberMe()
|
||||||
Cookies.remove('tenantName');
|
removeTenantName()
|
||||||
}
|
}
|
||||||
// 发起登陆
|
// 发起登陆
|
||||||
console.log("发起登录", this.loginForm);
|
// console.log("发起登录", this.loginForm);
|
||||||
this.$store.dispatch(this.loginForm.loginType === "sms" ? "SmsLogin" : "Login", this.loginForm).then(() => {
|
this.$store.dispatch(this.loginForm.loginType === "sms" ? "SmsLogin" : "Login", this.loginForm).then(() => {
|
||||||
this.$router.push({path: this.redirect || "/"}).catch(() => {
|
this.$router.push({path: this.redirect || "/"}).catch(() => {
|
||||||
});
|
});
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
</el-tab-pane>
|
</el-tab-pane>
|
||||||
</el-tabs>
|
</el-tabs>
|
||||||
<div>
|
<div>
|
||||||
<el-form ref="loginForm" :model="loginForm" :rules="LoginRules" class="login-form">
|
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
|
||||||
<!-- 账号密码登录 -->
|
<!-- 账号密码登录 -->
|
||||||
<el-form-item prop="username">
|
<el-form-item prop="username">
|
||||||
<el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
|
<el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
|
||||||
|
@ -65,16 +65,28 @@
|
||||||
<script>
|
<script>
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { encrypt, decrypt } from '@/utils/jsencrypt'
|
import { encrypt, decrypt } from '@/utils/jsencrypt'
|
||||||
|
import {
|
||||||
|
getPassword, getRememberMe,
|
||||||
|
getUsername,
|
||||||
|
removePassword,
|
||||||
|
removeUsername,
|
||||||
|
setPassword,
|
||||||
|
setRememberMe,
|
||||||
|
setUsername
|
||||||
|
} from "@/utils/auth";
|
||||||
|
import {getCodeImg} from "@/api/login";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "ThirdLogin",
|
name: "ThirdLogin",
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
|
codeUrl: "",
|
||||||
|
captchaEnable: true,
|
||||||
loginForm: {
|
loginForm: {
|
||||||
loginType: "uname",
|
loginType: "uname",
|
||||||
username: "admin",
|
username: "admin",
|
||||||
password: "admin123",
|
password: "admin123",
|
||||||
rememberMe: false, // TODO 芋艿:后面看情况,去掉这块
|
rememberMe: false,
|
||||||
},
|
},
|
||||||
loginRules: {
|
loginRules: {
|
||||||
username: [
|
username: [
|
||||||
|
@ -104,6 +116,7 @@ export default {
|
||||||
this.getCookie();
|
this.getCookie();
|
||||||
// 重定向地址
|
// 重定向地址
|
||||||
this.redirect = this.$route.query.redirect;
|
this.redirect = this.$route.query.redirect;
|
||||||
|
this.getCode();
|
||||||
// 社交登录相关
|
// 社交登录相关
|
||||||
this.type = this.$route.query.type;
|
this.type = this.$route.query.type;
|
||||||
this.code = this.$route.query.code;
|
this.code = this.$route.query.code;
|
||||||
|
@ -119,16 +132,30 @@ export default {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
getCode() {
|
||||||
|
// 只有开启的状态,才加载验证码。默认开启
|
||||||
|
if (!this.captchaEnable) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 请求远程,获得验证码
|
||||||
|
getCodeImg().then(res => {
|
||||||
|
res = res.data;
|
||||||
|
this.captchaEnable = res.enable;
|
||||||
|
if (this.captchaEnable) {
|
||||||
|
this.codeUrl = "data:image/gif;base64," + res.img;
|
||||||
|
this.loginForm.uuid = res.uuid;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
getCookie() {
|
getCookie() {
|
||||||
const username = Cookies.get("username");
|
const username = getUsername();
|
||||||
const password = Cookies.get("password");
|
const password = getPassword();
|
||||||
const rememberMe = Cookies.get('rememberMe')
|
const rememberMe = getRememberMe();
|
||||||
const loginType = Cookies.get('loginType');
|
|
||||||
this.loginForm = {
|
this.loginForm = {
|
||||||
username: username === undefined ? this.loginForm.username : username,
|
username: username ? username : this.loginForm.username,
|
||||||
password: password === undefined ? this.loginForm.password : decrypt(password),
|
password: password ? password : this.loginForm.password,
|
||||||
rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
|
rememberMe: rememberMe ? getRememberMe() : false,
|
||||||
loginType: loginType === undefined ? this.loginForm.loginType : loginType,
|
loginType: this.loginForm.loginType,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
handleLogin() {
|
handleLogin() {
|
||||||
|
@ -136,11 +163,12 @@ export default {
|
||||||
if (valid) {
|
if (valid) {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
if (this.loginForm.rememberMe) {
|
if (this.loginForm.rememberMe) {
|
||||||
Cookies.set("username", this.loginForm.username, { expires: 30 });
|
setUsername(this.loginForm.username)
|
||||||
Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
|
setPassword(this.loginForm.password)
|
||||||
|
setRememberMe(this.loginForm.rememberMe)
|
||||||
} else {
|
} else {
|
||||||
Cookies.remove("username");
|
removeUsername()
|
||||||
Cookies.remove("password");
|
removePassword()
|
||||||
}
|
}
|
||||||
this.$store.dispatch("SocialLogin2", {
|
this.$store.dispatch("SocialLogin2", {
|
||||||
code: this.code,
|
code: this.code,
|
||||||
|
|
Loading…
Reference in New Issue