From 5f28d0facbd49ff3628c6b8ae64c330161fdd577 Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Mon, 20 Jun 2022 10:13:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=87=E6=9C=9Ftoken=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/api/app/lib/controllers/auth/index.js | 23 +++++++++++++++++ code/api/app/lib/routes/auth/index.js | 4 +++ code/api/config.js | 4 ++- .../client/src/sections/auth/actions/auth.js | 16 ++++++++++-- .../src/sections/auth/containers/cross.jsx | 25 ++++++++++++++++--- code/web/client/src/utils/webapi.js | 2 ++ 6 files changed, 67 insertions(+), 7 deletions(-) diff --git a/code/api/app/lib/controllers/auth/index.js b/code/api/app/lib/controllers/auth/index.js index 0b73480..f9a8fd9 100644 --- a/code/api/app/lib/controllers/auth/index.js +++ b/code/api/app/lib/controllers/auth/index.js @@ -105,8 +105,31 @@ async function loginAxy (ctx) { } } +async function checkCrossToken (ctx) { + try { + const { token } = ctx.request.body; + + let cross = false + const expired = await ctx.redis.hget(token, 'expired'); + // 也可以在这里做延时操作 需要同步数据库(也可能安心云) + if (expired && moment().valueOf() <= moment(expired).valueOf()) { + cross = true + } + + ctx.status = 200; + ctx.body = { + cross, + } + } catch (error) { + ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); + ctx.status = 400; + ctx.body = {} + } +} + module.exports = { login, logout, loginAxy, + checkCrossToken, }; \ No newline at end of file diff --git a/code/api/app/lib/routes/auth/index.js b/code/api/app/lib/routes/auth/index.js index 42680c5..2f21a5a 100644 --- a/code/api/app/lib/routes/auth/index.js +++ b/code/api/app/lib/routes/auth/index.js @@ -21,4 +21,8 @@ module.exports = function (app, router, opts) { app.fs.api.logAttr['PUT/logout/axy'] = { content: '安心云登出信息同步', visible: false }; router.put('/logout/axy', auth.logout); + + // + app.fs.api.logAttr['POST/cross_token/check'] = { content: '登录信息鉴权信息检测', visible: true }; + router.post('/cross_token/check', auth.checkCrossToken); }; diff --git a/code/api/config.js b/code/api/config.js index b5374d2..14bf8d2 100644 --- a/code/api/config.js +++ b/code/api/config.js @@ -47,7 +47,9 @@ const product = { }, { entry: require('./app').entry, opts: { - exclude: [], // 不做认证的路由,也可以使用 exclude: ["*"] 跳过所有路由 + exclude: [// 不做认证的路由,也可以使用 exclude: ["*"] 跳过所有路由 + { p: '/cross_token/check', o: 'POST' } + ], redis: { host: IOTA_REDIS_SERVER_HOST, port: IOTA_REDIS_SERVER_PORT, diff --git a/code/web/client/src/sections/auth/actions/auth.js b/code/web/client/src/sections/auth/actions/auth.js index a878eb2..6652a1e 100644 --- a/code/web/client/src/sections/auth/actions/auth.js +++ b/code/web/client/src/sections/auth/actions/auth.js @@ -1,7 +1,7 @@ 'use strict'; import { ApiTable } from '$utils' -import { Request } from '@peace/utils'; +import { Request, basicAction } from '@peace/utils'; export const INIT_AUTH = 'INIT_AUTH'; export function initAuth () { @@ -72,8 +72,20 @@ export function logout () { }; } +export function crossCheck (data) { + return (dispatch) => + basicAction({ + type: "post", + dispatch: dispatch, + actionType: "CROSS_CHECK", + url: `${ApiTable.crossCheck}`, + msg: {}, + }); +} + export default { initAuth, login, - logout + logout, + crossCheck, } \ No newline at end of file diff --git a/code/web/client/src/sections/auth/containers/cross.jsx b/code/web/client/src/sections/auth/containers/cross.jsx index f6446df..1e19da9 100644 --- a/code/web/client/src/sections/auth/containers/cross.jsx +++ b/code/web/client/src/sections/auth/containers/cross.jsx @@ -1,17 +1,22 @@ 'use strict'; import React, { useEffect, useRef } from 'react'; import { connect } from "react-redux"; +import authAction from '../actions' const Cross = ({ dispatch, actions }) => { - useEffect(() => { + useEffect(async () => { + function preLogout () { + localStorage.removeItem('fs_iot_cross_user') + sessionStorage.removeItem('user') + } + function messageListen (e) { // 此处需做 域名 验证 const { data } = e if (data && data.action) { if (data.action == 'logout') { - localStorage.removeItem('fs_iot_cross_user') - sessionStorage.removeItem('user') + preLogout() } else if (data.action = 'login') { localStorage.setItem('fs_iot_cross_user', JSON.stringify(data.user)) } @@ -30,8 +35,20 @@ const Cross = ({ dispatch, actions }) => { window.addEventListener('message', messageListen); window.addEventListener("storage", storageListen); const user = localStorage.getItem('fs_iot_cross_user') + if (user) { - window.parent.postMessage({ action: 'initUser', user: JSON.parse(user) }, '*'); + const crossRslt = await dispatch(authAction.crossCheck({ token: user.token })) + if (crossRslt.success) { + if (crossRslt.payload.data.cross) { + window.parent.postMessage({ action: 'initUser', user: JSON.parse(user) }, '*'); + } else { + window.parent.postMessage({ action: 'logout' }, '*'); + preLogout() + } + } else { + window.parent.postMessage({ action: 'logout' }, '*'); + preLogout() + } } else { window.parent.postMessage({ action: 'initNotice' }, '*'); } diff --git a/code/web/client/src/utils/webapi.js b/code/web/client/src/utils/webapi.js index 0794e58..62ebe9b 100644 --- a/code/web/client/src/utils/webapi.js +++ b/code/web/client/src/utils/webapi.js @@ -3,6 +3,8 @@ export const ApiTable = { login: 'login', logout: 'logout', + + crossCheck: 'cross_token/check', }; export const RouteTable = {