Unverified Commit 1ef32561 authored by zhu-mingye's avatar zhu-mingye Committed by GitHub

[Feature] added user token info (#2149)

parent 0a332896
...@@ -35,6 +35,8 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -35,6 +35,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -93,4 +95,14 @@ public class AdminController { ...@@ -93,4 +95,14 @@ public class AdminController {
public Result<Tenant> switchingTenant(@RequestParam("tenantId") Integer tenantId) { public Result<Tenant> switchingTenant(@RequestParam("tenantId") Integer tenantId) {
return userService.chooseTenant(tenantId); return userService.chooseTenant(tenantId);
} }
/**
* get tenant info
*
* @return {@link Result}{@link SaTokenInfo}
*/
@GetMapping("/tokenInfo")
public Result<SaTokenInfo> getTokenInfo() {
return Result.succeed(StpUtil.getTokenInfo());
}
} }
...@@ -25,6 +25,7 @@ import org.dinky.data.model.User; ...@@ -25,6 +25,7 @@ import org.dinky.data.model.User;
import java.util.List; import java.util.List;
import cn.dev33.satoken.stp.SaTokenInfo;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
...@@ -38,4 +39,5 @@ public class UserDTO { ...@@ -38,4 +39,5 @@ public class UserDTO {
private List<Role> roleList; private List<Role> roleList;
private List<Tenant> tenantList; private List<Tenant> tenantList;
private Tenant currentTenant; private Tenant currentTenant;
private SaTokenInfo tokenInfo;
} }
...@@ -427,6 +427,7 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen ...@@ -427,6 +427,7 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen
userInfo.setUser(user); userInfo.setUser(user);
userInfo.setRoleList(roleList); userInfo.setRoleList(roleList);
userInfo.setTenantList(tenantList); userInfo.setTenantList(tenantList);
userInfo.setTokenInfo(StpUtil.getTokenInfo());
return userInfo; return userInfo;
} }
} }
...@@ -65,6 +65,7 @@ export async function getInitialState(): Promise<{ ...@@ -65,6 +65,7 @@ export async function getInitialState(): Promise<{
roleList: result.datas.roleList, roleList: result.datas.roleList,
tenantList: result.datas.tenantList, tenantList: result.datas.tenantList,
currentTenant: result.datas.currentTenant, currentTenant: result.datas.currentTenant,
tokenInfo: result.datas.saTokenInfo,
}; };
return currentUser; return currentUser;
}, (error) => { }, (error) => {
......
...@@ -31,7 +31,10 @@ const KeyBoard = () => { ...@@ -31,7 +31,10 @@ const KeyBoard = () => {
const buildKeyBoard = () => { const buildKeyBoard = () => {
return KEY_BOARD.map((item,index) => { return KEY_BOARD.map((item,index) => {
return <> return <>
{index % 6 === 0 ? <Divider plain orientationMargin={0}/> : <> {index % 6 === 0 ? <>
<Divider plain orientationMargin={0}/>
<Text keyboard>{item.label}</Text>{item.description}<Divider type="vertical"/>
</>: <>
<Text keyboard>{item.label}</Text>{item.description}<Divider type="vertical"/> <Text keyboard>{item.label}</Text>{item.description}<Divider type="vertical"/>
</>} </>}
</> </>
......
...@@ -20,12 +20,53 @@ ...@@ -20,12 +20,53 @@
export const QUICK_GUIDE = [ export const QUICK_GUIDE = [
{ {
key: 'registcluster', key: 'clusterInstance',
label: '注册 Session 集群',
link: '/registration/cluster/instance'
},{
key: 'registcluster',
label: '注册 Session 集群', label: '注册 Session 集群',
link: '/registration/cluster/instance' link: '/registration/cluster/instance'
},
{
key: 'clusterConfiguration',
label: '注册集群配置',
link: '/registration/cluster/configuration'
},
{
key: 'database',
label: '注册数据源',
link: '/registration/database'
},
{
key: 'alertInstance',
label: '注册告警实例',
link: '/registration/alert/instance'
},
{
key: 'alertGroup',
label: '注册告警组',
link: '/registration/alert/group'
},
{
key: 'fragment',
label: '全局变量',
link: '/registration/fragment'
},
{
key: 'document',
label: '文档',
link: '/registration/document'
},
{
key: 'gitprojects',
label: 'Git 项目',
link: '/registration/gitprojects'
},
{
key: 'udf',
label: 'UDF',
link: '/registration/udf'
},
{
key: 'resource',
label: '资源',
link: '/registration/resource'
} }
]; ];
\ No newline at end of file
...@@ -20,8 +20,9 @@ ...@@ -20,8 +20,9 @@
import {Divider, Typography} from "antd"; import {Divider, Typography} from "antd";
import React from "react"; import React from "react";
import {QUICK_GUIDE} from "@/pages/DataStudio/MiddleContainer/QuickGuide/constant"; import {QUICK_GUIDE} from "@/pages/DataStudio/MiddleContainer/QuickGuide/constant";
import { Link } from "@umijs/max";
const {Title, Paragraph, Link} = Typography; const {Title, Paragraph} = Typography;
...@@ -30,21 +31,23 @@ const QuickGuide = () => { ...@@ -30,21 +31,23 @@ const QuickGuide = () => {
const buildQuickGuide = () => { const buildQuickGuide = () => {
return QUICK_GUIDE.map((item,index) => { return QUICK_GUIDE.map((item,index) => {
return <> return <>
{index % 7 === 0 ? <Divider plain orientationMargin={0}/> : <> {index % 10 === 0 ? <>
<Link href={item.link} key={item.key}>{item.label}</Link> <Divider plain orientationMargin={0}/>
<Link to={item.link} key={item.key}>{item.label}</Link>
</> : <>
<Divider type="vertical"/> <Divider type="vertical"/>
<Link to={item.link} key={item.key}>{item.label}</Link>
</>} </>}
</> </>
}); });
} }
return <> return <>
<Typography style={{padding: '2px', textAlign: 'center', border: 'salmon'}}> <Typography style={{padding: '2px', textAlign: 'center', border: 'salmon'}}>
<Title level={4}>快捷引导</Title> <Title level={4}>快捷引导</Title>
todo: buildQuickGuide <Paragraph style={{padding: 0, margin: 0, textAlign: 'center', border: 'salmon'}} >
{/*<Paragraph style={{padding: 0, margin: 0, textAlign: 'center', border: 'salmon'}} >*/} {buildQuickGuide()}
{/* {buildQuickGuide()}*/} </Paragraph>
{/*</Paragraph>*/}
</Typography> </Typography>
</> </>
}; };
......
...@@ -20,11 +20,18 @@ ...@@ -20,11 +20,18 @@
import {history} from "@@/core/history"; import {history} from "@@/core/history";
/** 此方法会跳转到 redirect 参数所在的位置 */ /** 此方法会跳转到 redirect 参数所在的位置 */
export const gotoRedirectUrl = () => { export const gotoRedirectUrl = () => {
if (!history) return; if (!history) return;
setTimeout(() => { setTimeout(() => {
const urlParams = new URL(window.location.href).searchParams; const urlParams = new URL(window.location.href).searchParams;
history.push(urlParams.get("redirect") || "/"); history.push(urlParams.get("redirect") || "/");
}, 10); }, 10);
}; };
export const redirectToLogin = () => {
//TODO: 弹出确认框
window.location.href = '/login'
};
\ No newline at end of file
...@@ -20,9 +20,9 @@ import {setTenantStorageAndCookie} from "@/utils/function"; ...@@ -20,9 +20,9 @@ import {setTenantStorageAndCookie} from "@/utils/function";
import {l} from "@/utils/intl"; import {l} from "@/utils/intl";
import {useEmotionCss} from "@ant-design/use-emotion-css"; import {useEmotionCss} from "@ant-design/use-emotion-css";
import {useModel} from "@umijs/max"; import {useModel} from "@umijs/max";
import React, {useState} from "react"; import React, {useEffect, useState} from "react";
import {flushSync} from "react-dom"; import {flushSync} from "react-dom";
import {chooseTenantSubmit, login} from "@/services/BusinessCrud"; import {chooseTenantSubmit, login, queryDataByParams} from "@/services/BusinessCrud";
import { import {
ErrorMessage, ErrorMessage,
SuccessMessageAsync, SuccessMessageAsync,
...@@ -31,8 +31,10 @@ import {UserBaseInfo} from "@/types/User/data"; ...@@ -31,8 +31,10 @@ import {UserBaseInfo} from "@/types/User/data";
import LoginForm from "./LoginForm"; import LoginForm from "./LoginForm";
import HelmetTitle from "./HelmetTitle"; import HelmetTitle from "./HelmetTitle";
import ChooseModal from "@/pages/Other/Login/ChooseModal"; import ChooseModal from "@/pages/Other/Login/ChooseModal";
import {gotoRedirectUrl} from "@/pages/Other/Login/function"; import {gotoRedirectUrl, redirectToLogin} from "@/pages/Other/Login/function";
import LangSwitch from "@/pages/Other/Login/LangSwitch"; import LangSwitch from "@/pages/Other/Login/LangSwitch";
import {useLocalStorage} from "@/utils/hook/useLocalStorage";
import {API_CONSTANTS} from "@/services/constants";
const Login: React.FC = () => { const Login: React.FC = () => {
const [submitting, setSubmitting] = useState(false); const [submitting, setSubmitting] = useState(false);
...@@ -40,6 +42,8 @@ const Login: React.FC = () => { ...@@ -40,6 +42,8 @@ const Login: React.FC = () => {
const [tenantVisible, handleTenantVisible] = useState<boolean>(false); const [tenantVisible, handleTenantVisible] = useState<boolean>(false);
const [tenant, setTenant] = useState<UserBaseInfo.Tenant[]>([]); const [tenant, setTenant] = useState<UserBaseInfo.Tenant[]>([]);
const [localStorageOfToken, setLocalStorageOfToken] = useLocalStorage("token","");
const containerClassName = useEmotionCss(() => { const containerClassName = useEmotionCss(() => {
return { return {
display: "flex", display: "flex",
...@@ -60,6 +64,26 @@ const Login: React.FC = () => { ...@@ -60,6 +64,26 @@ const Login: React.FC = () => {
} }
}; };
/**
* When the token is expired, redirect to login
*/
useEffect(() => {
const expirationTime = JSON.parse(JSON.stringify(localStorageOfToken)).tokenTimeout ?? 0; // GET TOKEN TIMEOUT
let timeRemaining = 0;
let timer : NodeJS.Timeout;
if (expirationTime > 0) {
// calculate the time difference
const currentTime = Date.now();
timeRemaining = expirationTime - currentTime;
// use setInterval to set a timer
timer = setInterval(()=> redirectToLogin(), timeRemaining);
}
return () => {
clearTimeout(timer);
}
},[initialState,localStorageOfToken])
const handleChooseTenant = async (chooseTenantResult: API.Result) => { const handleChooseTenant = async (chooseTenantResult: API.Result) => {
if (chooseTenantResult.code === 0) { if (chooseTenantResult.code === 0) {
await SuccessMessageAsync(l("login.chooseTenantSuccess", "", { await SuccessMessageAsync(l("login.chooseTenantSuccess", "", {
...@@ -109,6 +133,10 @@ const Login: React.FC = () => { ...@@ -109,6 +133,10 @@ const Login: React.FC = () => {
try { try {
// login // login
const result = await login({...values}); const result = await login({...values});
if (result.code === 0 ){
// if login success then get token info and set it to local storage
await queryDataByParams(API_CONSTANTS.TOKEN_INFO).then( res => setLocalStorageOfToken(JSON.stringify(res)));
}
setInitialState((s) => ({...s, currentUser: result.datas})); setInitialState((s) => ({...s, currentUser: result.datas}));
await SuccessMessageAsync(l("login.result", "", {msg: result.msg, time: result.time})); await SuccessMessageAsync(l("login.result", "", {msg: result.msg, time: result.time}));
/** /**
......
...@@ -26,6 +26,7 @@ export const API_CONSTANTS = { ...@@ -26,6 +26,7 @@ export const API_CONSTANTS = {
// --- user --- // --- user ---
// login path // login path
LOGIN_PATH: "/user/login", LOGIN_PATH: "/user/login",
TOKEN_INFO: "/api/tokenInfo",
// user login // user login
LOGIN: "/api/login", LOGIN: "/api/login",
// current user info // current user info
......
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
* limitations under the License. * limitations under the License.
*/ */
import {SaTokenInfo} from "@/types/User/data";
declare namespace API { declare namespace API {
type Result = { type Result = {
code: number; code: number;
...@@ -28,6 +31,7 @@ declare namespace API { ...@@ -28,6 +31,7 @@ declare namespace API {
roleList?: UserBaseInfo.Role[]; roleList?: UserBaseInfo.Role[];
tenantList?: UserBaseInfo.Tenant[]; tenantList?: UserBaseInfo.Tenant[];
currentTenant?: UserBaseInfo.Tenant; currentTenant?: UserBaseInfo.Tenant;
tokenInfo: SaTokenInfo;
}; };
type PageParams = { type PageParams = {
......
...@@ -55,6 +55,19 @@ declare namespace UserBaseInfo { ...@@ -55,6 +55,19 @@ declare namespace UserBaseInfo {
}; };
} }
export type SaTokenInfo = {
tokenName: string;
tokenValue: string;
isLogin: boolean;
loginId: number;
loginType: string;
tokenTimeout: number;
sessionTimeout: number;
tokenSessionTimeout: number;
tokenActivityTimeout: number;
loginDevice: string;
tag: string;
}
export type RowPermissions = ExcludeNameAndEnableColumns & { export type RowPermissions = ExcludeNameAndEnableColumns & {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment