nsgm-cli 2.1.8 → 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/lib/generate.js +299 -379
- package/lib/tsconfig.build.tsbuildinfo +1 -1
- package/lib/utils.d.ts +7 -0
- package/lib/utils.js +110 -0
- package/package.json +1 -2
- 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
|
|