Skip to content

Commit f340701

Browse files
authored
feat: 添加令牌刷新接口 (#741)
* build: v0.20.6 * feat: 优化SecurityProperties结构, token申请同时返回refreshToken * optimize: accessTokenDay默认调整为3 * optimize: token的secretKey应用启动后随机生成 * build: gen new api-client v20.6.0 * fix: Console的登录页申请令牌接口适配 * feat: 添加token刷新接口 * docs: update CHANGELOG.MD
1 parent 2a6fb89 commit f340701

File tree

21 files changed

+387
-52
lines changed

21 files changed

+387
-52
lines changed

CHANGELOG.MD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
更新日志文档,版本顺序从新到旧,最新版本在最前(上)面。
44

5+
# 0.20.6
6+
7+
## 新特性
8+
9+
- 优化SecurityProperties结构
10+
- token申请同时返回refreshToken
11+
- 添加token刷新接口
12+
13+
## 问题修复
14+
15+
- Console的登录页申请令牌接口适配
16+
517
# 0.20.5
618

719
# 新特性
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package run.ikaros.api.infra.exception.security;
2+
3+
import org.springframework.security.core.AuthenticationException;
4+
5+
public class InvalidTokenException extends AuthenticationException {
6+
public InvalidTokenException(String msg) {
7+
super(msg);
8+
}
9+
10+
public InvalidTokenException(String msg, Throwable cause) {
11+
super(msg, cause);
12+
}
13+
}

api/src/main/java/run/ikaros/api/infra/utils/StringUtils.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package run.ikaros.api.infra.utils;
22

3+
import java.security.SecureRandom;
4+
35
public class StringUtils {
6+
private static final String CHARACTERS
7+
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_-+=<>?";
8+
private static final SecureRandom RANDOM = new SecureRandom();
49

510
/**
611
* upper str first char.
@@ -45,4 +50,23 @@ public static String addLikeChar(String str) {
4550
return "%" + str + "%";
4651
}
4752

53+
54+
/**
55+
* 生成指定长度的字符串.
56+
*
57+
* @param length 字符串长度
58+
* @return 随机生成的字符串
59+
*/
60+
public static String generateRandomStr(int length) {
61+
if (length <= 0) {
62+
throw new IllegalArgumentException("字符串长度必须大于 0");
63+
}
64+
65+
StringBuilder sb = new StringBuilder(length);
66+
for (int i = 0; i < length; i++) {
67+
sb.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length())));
68+
}
69+
return sb.toString();
70+
}
71+
4872
}

console/packages/api-client/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ npm publish
5858
选择当前目录下的更改进行`git add .`
5959

6060
```bash
61-
git commit -am "build: gen new api-client v20.1.0"
61+
git commit -am "build: gen new api-client v20.6.0"
6262
```
6363

6464
合成版(powershell),升级 package.json 版本,并启动服务端后,在 api-client 路径下:

console/packages/api-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@runikaros/api-client",
3-
"version": "20.1.0",
3+
"version": "20.6.0",
44
"description": "Project ikaros console api-client package",
55
"type": "module",
66
"scripts": {

console/packages/api-client/src/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ models/episode-resource.ts
4848
models/episode.ts
4949
models/index.ts
5050
models/jwt-apply-param.ts
51+
models/jwt-apply-response.ts
5152
models/link.ts
5253
models/paging-wrap.ts
5354
models/plugin-load-location-file-system.ts

console/packages/api-client/src/api/v1alpha1-indices-api.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ export const V1alpha1IndicesApiAxiosParamCreator = function (
9797
* Search subjects with fuzzy query
9898
* @param {string} keyword
9999
* @param {number} [limit]
100-
* @param {string} [highlightPostTag]
101100
* @param {string} [highlightPreTag]
101+
* @param {string} [highlightPostTag]
102102
* @param {*} [options] Override http request option.
103103
* @throws {RequiredError}
104104
*/
105105
searchSubject: async (
106106
keyword: string,
107107
limit?: number,
108-
highlightPostTag?: string,
109108
highlightPreTag?: string,
109+
highlightPostTag?: string,
110110
options: AxiosRequestConfig = {}
111111
): Promise<RequestArgs> => {
112112
// verify required parameter 'keyword' is not null or undefined
@@ -139,10 +139,6 @@ export const V1alpha1IndicesApiAxiosParamCreator = function (
139139
localVarQueryParameter["limit"] = limit;
140140
}
141141

142-
if (highlightPostTag !== undefined) {
143-
localVarQueryParameter["highlightPostTag"] = highlightPostTag;
144-
}
145-
146142
if (keyword !== undefined) {
147143
localVarQueryParameter["keyword"] = keyword;
148144
}
@@ -151,6 +147,10 @@ export const V1alpha1IndicesApiAxiosParamCreator = function (
151147
localVarQueryParameter["highlightPreTag"] = highlightPreTag;
152148
}
153149

150+
if (highlightPostTag !== undefined) {
151+
localVarQueryParameter["highlightPostTag"] = highlightPostTag;
152+
}
153+
154154
setSearchParams(localVarUrlObj, localVarQueryParameter);
155155
let headersFromBaseOptions =
156156
baseOptions && baseOptions.headers ? baseOptions.headers : {};
@@ -199,25 +199,25 @@ export const V1alpha1IndicesApiFp = function (configuration?: Configuration) {
199199
* Search subjects with fuzzy query
200200
* @param {string} keyword
201201
* @param {number} [limit]
202-
* @param {string} [highlightPostTag]
203202
* @param {string} [highlightPreTag]
203+
* @param {string} [highlightPostTag]
204204
* @param {*} [options] Override http request option.
205205
* @throws {RequiredError}
206206
*/
207207
async searchSubject(
208208
keyword: string,
209209
limit?: number,
210-
highlightPostTag?: string,
211210
highlightPreTag?: string,
211+
highlightPostTag?: string,
212212
options?: AxiosRequestConfig
213213
): Promise<
214214
(axios?: AxiosInstance, basePath?: string) => AxiosPromise<SubjectHints>
215215
> {
216216
const localVarAxiosArgs = await localVarAxiosParamCreator.searchSubject(
217217
keyword,
218218
limit,
219-
highlightPostTag,
220219
highlightPreTag,
220+
highlightPostTag,
221221
options
222222
);
223223
return createRequestFunction(
@@ -265,8 +265,8 @@ export const V1alpha1IndicesApiFactory = function (
265265
.searchSubject(
266266
requestParameters.keyword,
267267
requestParameters.limit,
268-
requestParameters.highlightPostTag,
269268
requestParameters.highlightPreTag,
269+
requestParameters.highlightPostTag,
270270
options
271271
)
272272
.then((request) => request(axios, basePath));
@@ -299,14 +299,14 @@ export interface V1alpha1IndicesApiSearchSubjectRequest {
299299
* @type {string}
300300
* @memberof V1alpha1IndicesApiSearchSubject
301301
*/
302-
readonly highlightPostTag?: string;
302+
readonly highlightPreTag?: string;
303303

304304
/**
305305
*
306306
* @type {string}
307307
* @memberof V1alpha1IndicesApiSearchSubject
308308
*/
309-
readonly highlightPreTag?: string;
309+
readonly highlightPostTag?: string;
310310
}
311311

312312
/**
@@ -343,8 +343,8 @@ export class V1alpha1IndicesApi extends BaseAPI {
343343
.searchSubject(
344344
requestParameters.keyword,
345345
requestParameters.limit,
346-
requestParameters.highlightPostTag,
347346
requestParameters.highlightPreTag,
347+
requestParameters.highlightPostTag,
348348
options
349349
)
350350
.then((request) => request(this.axios, this.basePath));

console/packages/api-client/src/api/v1alpha1-security-api.ts

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import {
3939
} from "../base";
4040
// @ts-ignore
4141
import { JwtApplyParam } from "../models";
42+
// @ts-ignore
43+
import { JwtApplyResponse } from "../models";
4244
/**
4345
* V1alpha1SecurityApi - axios parameter creator
4446
* @export
@@ -97,6 +99,61 @@ export const V1alpha1SecurityApiAxiosParamCreator = function (
9799
configuration
98100
);
99101

102+
return {
103+
url: toPathString(localVarUrlObj),
104+
options: localVarRequestOptions,
105+
};
106+
},
107+
/**
108+
* Refresh access token with refresh token.
109+
* @param {string} [body] Refresh token.
110+
* @param {*} [options] Override http request option.
111+
* @throws {RequiredError}
112+
*/
113+
refreshToken: async (
114+
body?: string,
115+
options: AxiosRequestConfig = {}
116+
): Promise<RequestArgs> => {
117+
const localVarPath = `/api/v1alpha1/security/auth/token/jwt/refresh`;
118+
// use dummy base URL string because the URL constructor only accepts absolute URLs.
119+
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
120+
let baseOptions;
121+
if (configuration) {
122+
baseOptions = configuration.baseOptions;
123+
}
124+
125+
const localVarRequestOptions = {
126+
method: "PUT",
127+
...baseOptions,
128+
...options,
129+
};
130+
const localVarHeaderParameter = {} as any;
131+
const localVarQueryParameter = {} as any;
132+
133+
// authentication BasicAuth required
134+
// http basic authentication required
135+
setBasicAuthToObject(localVarRequestOptions, configuration);
136+
137+
// authentication BearerAuth required
138+
// http bearer authentication required
139+
await setBearerAuthToObject(localVarHeaderParameter, configuration);
140+
141+
localVarHeaderParameter["Content-Type"] = "application/json";
142+
143+
setSearchParams(localVarUrlObj, localVarQueryParameter);
144+
let headersFromBaseOptions =
145+
baseOptions && baseOptions.headers ? baseOptions.headers : {};
146+
localVarRequestOptions.headers = {
147+
...localVarHeaderParameter,
148+
...headersFromBaseOptions,
149+
...options.headers,
150+
};
151+
localVarRequestOptions.data = serializeDataIfNeeded(
152+
body,
153+
localVarRequestOptions,
154+
configuration
155+
);
156+
100157
return {
101158
url: toPathString(localVarUrlObj),
102159
options: localVarRequestOptions,
@@ -123,7 +180,10 @@ export const V1alpha1SecurityApiFp = function (configuration?: Configuration) {
123180
jwtApplyParam?: JwtApplyParam,
124181
options?: AxiosRequestConfig
125182
): Promise<
126-
(axios?: AxiosInstance, basePath?: string) => AxiosPromise<string>
183+
(
184+
axios?: AxiosInstance,
185+
basePath?: string
186+
) => AxiosPromise<JwtApplyResponse>
127187
> {
128188
const localVarAxiosArgs = await localVarAxiosParamCreator.applyJwtToken(
129189
jwtApplyParam,
@@ -136,6 +196,29 @@ export const V1alpha1SecurityApiFp = function (configuration?: Configuration) {
136196
configuration
137197
);
138198
},
199+
/**
200+
* Refresh access token with refresh token.
201+
* @param {string} [body] Refresh token.
202+
* @param {*} [options] Override http request option.
203+
* @throws {RequiredError}
204+
*/
205+
async refreshToken(
206+
body?: string,
207+
options?: AxiosRequestConfig
208+
): Promise<
209+
(axios?: AxiosInstance, basePath?: string) => AxiosPromise<string>
210+
> {
211+
const localVarAxiosArgs = await localVarAxiosParamCreator.refreshToken(
212+
body,
213+
options
214+
);
215+
return createRequestFunction(
216+
localVarAxiosArgs,
217+
globalAxios,
218+
BASE_PATH,
219+
configuration
220+
);
221+
},
139222
};
140223
};
141224

@@ -159,11 +242,25 @@ export const V1alpha1SecurityApiFactory = function (
159242
applyJwtToken(
160243
requestParameters: V1alpha1SecurityApiApplyJwtTokenRequest = {},
161244
options?: AxiosRequestConfig
162-
): AxiosPromise<string> {
245+
): AxiosPromise<JwtApplyResponse> {
163246
return localVarFp
164247
.applyJwtToken(requestParameters.jwtApplyParam, options)
165248
.then((request) => request(axios, basePath));
166249
},
250+
/**
251+
* Refresh access token with refresh token.
252+
* @param {V1alpha1SecurityApiRefreshTokenRequest} requestParameters Request parameters.
253+
* @param {*} [options] Override http request option.
254+
* @throws {RequiredError}
255+
*/
256+
refreshToken(
257+
requestParameters: V1alpha1SecurityApiRefreshTokenRequest = {},
258+
options?: AxiosRequestConfig
259+
): AxiosPromise<string> {
260+
return localVarFp
261+
.refreshToken(requestParameters.body, options)
262+
.then((request) => request(axios, basePath));
263+
},
167264
};
168265
};
169266

@@ -181,6 +278,20 @@ export interface V1alpha1SecurityApiApplyJwtTokenRequest {
181278
readonly jwtApplyParam?: JwtApplyParam;
182279
}
183280

281+
/**
282+
* Request parameters for refreshToken operation in V1alpha1SecurityApi.
283+
* @export
284+
* @interface V1alpha1SecurityApiRefreshTokenRequest
285+
*/
286+
export interface V1alpha1SecurityApiRefreshTokenRequest {
287+
/**
288+
* Refresh token.
289+
* @type {string}
290+
* @memberof V1alpha1SecurityApiRefreshToken
291+
*/
292+
readonly body?: string;
293+
}
294+
184295
/**
185296
* V1alpha1SecurityApi - object-oriented interface
186297
* @export
@@ -203,4 +314,20 @@ export class V1alpha1SecurityApi extends BaseAPI {
203314
.applyJwtToken(requestParameters.jwtApplyParam, options)
204315
.then((request) => request(this.axios, this.basePath));
205316
}
317+
318+
/**
319+
* Refresh access token with refresh token.
320+
* @param {V1alpha1SecurityApiRefreshTokenRequest} requestParameters Request parameters.
321+
* @param {*} [options] Override http request option.
322+
* @throws {RequiredError}
323+
* @memberof V1alpha1SecurityApi
324+
*/
325+
public refreshToken(
326+
requestParameters: V1alpha1SecurityApiRefreshTokenRequest = {},
327+
options?: AxiosRequestConfig
328+
) {
329+
return V1alpha1SecurityApiFp(this.configuration)
330+
.refreshToken(requestParameters.body, options)
331+
.then((request) => request(this.axios, this.basePath));
332+
}
206333
}

0 commit comments

Comments
 (0)