nsgm-cli 2.1.9 → 2.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/utils/sso.ts +54 -14
- package/package.json +1 -1
- package/pages/_app.tsx +35 -4
- package/pages/login.tsx +14 -4
- package/server/apis/sso.js +9 -7
package/client/utils/sso.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import axios from 'axios'
|
|
2
2
|
import { setCookie, getCookie, delCookie } from './cookie'
|
|
3
|
-
import { getUrlParamByKey, getLocalApiPrefix, getLocalEnv } from './common'
|
|
3
|
+
import { getUrlParamByKey, getLocalApiPrefix, getLocalEnv, handleXSS } from './common'
|
|
4
4
|
import _ from 'lodash'
|
|
5
5
|
|
|
6
6
|
const env = getLocalEnv()
|
|
@@ -180,26 +180,66 @@ export const login = (callback: any) => {
|
|
|
180
180
|
if (typeof window !== 'undefined') {
|
|
181
181
|
const locationHref = window.location.href
|
|
182
182
|
|
|
183
|
-
|
|
183
|
+
// 如果已经在登录页面,不需要进行登录检查
|
|
184
|
+
if (locationHref.indexOf('/login') !== -1) {
|
|
185
|
+
callback && callback()
|
|
186
|
+
return
|
|
187
|
+
}
|
|
184
188
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const urlParamName = getUrlParamByKey('name')
|
|
190
|
-
// console.log('urlParamTicket', urlParamTicket, urlParamName)
|
|
189
|
+
// 如果没有登录凭证,立即跳转到登录页面
|
|
190
|
+
if (cookieLoginValue === '') {
|
|
191
|
+
const urlParamTicket = getUrlParamByKey('ticket')
|
|
192
|
+
const urlParamName = getUrlParamByKey('name')
|
|
191
193
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
194
|
+
if (urlParamTicket !== '') {
|
|
195
|
+
validateLogin(urlParamTicket, urlParamName, callback)
|
|
196
|
+
} else {
|
|
197
|
+
// 没有ticket参数,直接跳转到登录页
|
|
198
|
+
jumpToLogin()
|
|
199
|
+
// 不执行回调,因为页面将被重定向
|
|
200
|
+
return
|
|
197
201
|
}
|
|
202
|
+
} else {
|
|
203
|
+
// 有登录凭证,验证登录状态
|
|
204
|
+
principalLogin(cookieLoginValue, callback)
|
|
198
205
|
}
|
|
206
|
+
} else {
|
|
207
|
+
callback && callback()
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export const directLogin = (userName: string, userPassword: string, callback: any) => {
|
|
212
|
+
if (userName === '') {
|
|
213
|
+
return { success: false, message: '请输入用户名' };
|
|
199
214
|
}
|
|
215
|
+
if (userPassword === '') {
|
|
216
|
+
return { success: false, message: '请输入密码' };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// 使用 encodeURIComponent 处理可能的特殊字符,然后再进行 Base64 编码
|
|
220
|
+
const safeStr = handleXSS(userName + "," + userPassword);
|
|
221
|
+
const encodedName = btoa(encodeURIComponent(safeStr));
|
|
222
|
+
const url = `${getLocalApiPrefix()}/rest/sso/ticketCheck?ticket=XXX&name=${encodedName}`;
|
|
223
|
+
|
|
224
|
+
return fetch(url)
|
|
225
|
+
.then(response => response.json())
|
|
226
|
+
.then(data => {
|
|
227
|
+
if (data && data.returnCode === 0) {
|
|
228
|
+
// 登录成功,设置cookie
|
|
229
|
+
if (typeof window !== 'undefined') {
|
|
230
|
+
storeLogin(data.cookieValue, data.cookieExpire, data.userAttr, callback);
|
|
231
|
+
return { success: true };
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return { success: false, message: '用户名或密码错误' };
|
|
235
|
+
})
|
|
236
|
+
.catch(error => {
|
|
237
|
+
console.error('登录请求失败:', error);
|
|
238
|
+
return { success: false, message: '登录请求失败,请稍后重试' };
|
|
239
|
+
});
|
|
200
240
|
}
|
|
201
241
|
|
|
202
242
|
export const logout = () => {
|
|
203
|
-
|
|
243
|
+
jumpToLogin()
|
|
204
244
|
}
|
|
205
245
|
|
package/package.json
CHANGED
package/pages/_app.tsx
CHANGED
|
@@ -18,13 +18,35 @@ const App = ({ Component, pageProps }) => {
|
|
|
18
18
|
const store = useStore(pageProps.initialReduxState)
|
|
19
19
|
const [ssoUser, setSsoUser] = useState(null)
|
|
20
20
|
const [pageLoad, setPageLoad] = useState(false)
|
|
21
|
+
const [loginChecked, setLoginChecked] = useState(false)
|
|
21
22
|
|
|
22
23
|
useEffect(() => {
|
|
24
|
+
// 检查当前路径是否为登录页
|
|
25
|
+
const isLoginPage = typeof window !== 'undefined' && window.location.pathname === '/login'
|
|
26
|
+
|
|
27
|
+
// 如果是登录页,直接设置加载完成
|
|
28
|
+
if (isLoginPage) {
|
|
29
|
+
setLoginChecked(true)
|
|
30
|
+
setPageLoad(true)
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 检查是否有登录凭证
|
|
35
|
+
const hasLoginCookie = typeof window !== 'undefined' && document.cookie.includes('_cas_nsgm')
|
|
36
|
+
|
|
37
|
+
// 如果没有登录凭证,直接跳转到登录页面
|
|
38
|
+
if (!hasLoginCookie && typeof window !== 'undefined') {
|
|
39
|
+
window.location.href = window.location.origin + '/login'
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 否则执行登录检查
|
|
23
44
|
login((user: any) => {
|
|
24
45
|
if (user) {
|
|
25
46
|
// console.log('checkLogin_user', user)
|
|
26
47
|
setSsoUser(user)
|
|
27
48
|
}
|
|
49
|
+
setLoginChecked(true)
|
|
28
50
|
})
|
|
29
51
|
|
|
30
52
|
setTimeout(() => {
|
|
@@ -38,14 +60,23 @@ const App = ({ Component, pageProps }) => {
|
|
|
38
60
|
<ThemeProvider theme={theme}>
|
|
39
61
|
<Provider store={store}>
|
|
40
62
|
{
|
|
41
|
-
|
|
42
|
-
|
|
63
|
+
!loginChecked ? (
|
|
64
|
+
<Loading>
|
|
65
|
+
<Spin size="large" />
|
|
66
|
+
</Loading>
|
|
67
|
+
) : pageLoad ? (
|
|
68
|
+
ssoUser ? (
|
|
69
|
+
<LayoutComponent user={ssoUser}>
|
|
70
|
+
<Component {...pageProps} />
|
|
71
|
+
</LayoutComponent>
|
|
72
|
+
) : (
|
|
43
73
|
<Component {...pageProps} />
|
|
44
|
-
|
|
45
|
-
|
|
74
|
+
)
|
|
75
|
+
) : (
|
|
46
76
|
<Loading>
|
|
47
77
|
<Spin size="large" />
|
|
48
78
|
</Loading>
|
|
79
|
+
)
|
|
49
80
|
}
|
|
50
81
|
</Provider>
|
|
51
82
|
</ThemeProvider>
|
package/pages/login.tsx
CHANGED
|
@@ -3,9 +3,10 @@ import _ from 'lodash'
|
|
|
3
3
|
import { LoginContainer } from '../client/styled/common'
|
|
4
4
|
// import getConfig from 'next/config'
|
|
5
5
|
import React, { useState } from 'react'
|
|
6
|
-
import {
|
|
6
|
+
import { getLocalEnv } from '../client/utils/common'
|
|
7
7
|
import { Input, Button, Form, Typography, message } from 'antd'
|
|
8
8
|
import { UserOutlined, LockOutlined } from '@ant-design/icons'
|
|
9
|
+
import { directLogin } from '../client/utils/sso'
|
|
9
10
|
|
|
10
11
|
const md = new MarkdownIt({
|
|
11
12
|
html: true,
|
|
@@ -24,6 +25,9 @@ renderArr.push('Login')
|
|
|
24
25
|
const Page = ({ html }) => {
|
|
25
26
|
const [userName, setUserName] = useState("")
|
|
26
27
|
const [userPassword, setUserPassword] = useState("")
|
|
28
|
+
const env = getLocalEnv()
|
|
29
|
+
const LOGIN_COOKIE_ID = env + '_cas_nsgm'
|
|
30
|
+
const LOGIN_COOKIE_USER = env + '_nsgm_user'
|
|
27
31
|
|
|
28
32
|
const createMarkup = () => {
|
|
29
33
|
return {
|
|
@@ -42,9 +46,15 @@ const Page = ({ html }) => {
|
|
|
42
46
|
return;
|
|
43
47
|
}
|
|
44
48
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
directLogin(userName, userPassword, (user) => {
|
|
50
|
+
if (user) {
|
|
51
|
+
window.location.href = window.location.origin;
|
|
52
|
+
}
|
|
53
|
+
}).then(result => {
|
|
54
|
+
if (!result.success) {
|
|
55
|
+
message.error(result.message);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
48
58
|
}
|
|
49
59
|
}
|
|
50
60
|
|
package/server/apis/sso.js
CHANGED
|
@@ -4,9 +4,9 @@ const router = express.Router()
|
|
|
4
4
|
|
|
5
5
|
router.get('/sessionCheck', (req, res) => {
|
|
6
6
|
const { query } = req
|
|
7
|
-
const { cookieValue, redirectUrl, appId } = query
|
|
7
|
+
// const { cookieValue, redirectUrl, appId } = query
|
|
8
8
|
|
|
9
|
-
res.json({
|
|
9
|
+
res.json({
|
|
10
10
|
name: 'sessionCheck',
|
|
11
11
|
query,
|
|
12
12
|
returnCode: 0,
|
|
@@ -18,11 +18,13 @@ router.get('/sessionCheck', (req, res) => {
|
|
|
18
18
|
|
|
19
19
|
router.get('/ticketCheck', (req, res) => {
|
|
20
20
|
const { query } = req
|
|
21
|
-
const {
|
|
22
|
-
//
|
|
21
|
+
const { name } = query
|
|
22
|
+
// 使用 Buffer 解码 Base64 字符串,然后使用 decodeURIComponent 处理特殊字符
|
|
23
|
+
const decodedBase64 = Buffer.from(name, 'base64').toString('utf-8')
|
|
24
|
+
const decodedName = decodeURIComponent(decodedBase64)
|
|
23
25
|
|
|
24
|
-
if(
|
|
25
|
-
res.json({
|
|
26
|
+
if (decodedName === "erishen,123456") {
|
|
27
|
+
res.json({
|
|
26
28
|
name: 'ticketCheck',
|
|
27
29
|
query,
|
|
28
30
|
returnCode: 0,
|
|
@@ -33,7 +35,7 @@ router.get('/ticketCheck', (req, res) => {
|
|
|
33
35
|
cookieExpire: 10000
|
|
34
36
|
})
|
|
35
37
|
} else {
|
|
36
|
-
res.json({
|
|
38
|
+
res.json({
|
|
37
39
|
name: 'ticketCheck',
|
|
38
40
|
query,
|
|
39
41
|
returnCode: -1
|