Skip to content

Commit 96ed9fa

Browse files
author
rchen9
committed
fix:response code
1 parent 77bc985 commit 96ed9fa

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

arex-storage-web-api/src/main/java/com/arextest/storage/aspect/AppAuthAspectExecutor.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import com.arextest.common.annotation.AppAuth;
44
import com.arextest.common.context.ArexContext;
55
import com.arextest.common.jwt.JWTService;
6-
import com.arextest.common.model.response.ResponseCode;
7-
import com.arextest.common.utils.ResponseUtils;
6+
import com.arextest.common.utils.ResponseUtils_New;
87
import com.arextest.config.model.dao.config.SystemConfigurationCollection.KeySummary;
98
import com.arextest.config.model.dto.application.ApplicationConfiguration;
109
import com.arextest.config.model.dto.system.SystemConfiguration;
1110
import com.arextest.config.repository.impl.ApplicationConfigurationRepositoryImpl;
1211
import com.arextest.config.repository.impl.SystemConfigurationRepositoryImpl;
12+
import com.arextest.storage.model.ArexStorageResponseCode;
1313
import com.arextest.storage.service.config.ApplicationService;
1414
import java.util.List;
1515
import java.util.Optional;
@@ -61,18 +61,12 @@ public Object doAround(ProceedingJoinPoint point, AppAuth auth) throws Throwable
6161
// do aspect by appId
6262
if (context.getAppId() == null) {
6363
LOGGER.error("header has no appId");
64-
return reject(point, auth, NO_APPID);
64+
return reject(point, auth, NO_APPID, ArexStorageResponseCode.APP_AUTH_NO_APP_ID);
6565
}
6666

6767
// do aspect by owner exist
6868
OwnerExistResult ownerExistResult = getOwnerExistResult();
69-
if (ownerExistResult.getExist()) {
70-
context.setPassAuth(true);
71-
return point.proceed();
72-
} else {
73-
context.setPassAuth(false);
74-
return reject(point, auth, ownerExistResult.getRemark());
75-
}
69+
return processOwnerExistVerify(ownerExistResult, context, point, auth);
7670

7771
} finally {
7872
ArexContext.removeContext();
@@ -110,23 +104,39 @@ protected OwnerExistResult getOwnerExistResult() {
110104
List<ApplicationConfiguration> applications = applicationConfigurationRepository.listBy(
111105
context.getAppId());
112106
if (CollectionUtils.isEmpty(applications)) {
113-
LOGGER.error("error appId");
114-
return new OwnerExistResult(false, ERROR_APPID);
107+
LOGGER.error("error appId, appId: {}", context.getAppId());
108+
return new OwnerExistResult(false, ERROR_APPID,
109+
ArexStorageResponseCode.APP_AUTH_ERROR_APP_ID);
115110
}
116111
ApplicationConfiguration application = applications.get(0);
117112
owners = application.getOwners();
118113
}
119114
if (CollectionUtils.isEmpty(owners) || owners.contains(userName)) {
120-
return new OwnerExistResult(true, null);
115+
return new OwnerExistResult(true, null, null);
121116
} else {
122-
return new OwnerExistResult(false, NO_PERMISSION);
117+
return new OwnerExistResult(false, NO_PERMISSION,
118+
ArexStorageResponseCode.APP_AUTH_NO_PERMISSION);
123119
}
124120
}
125121

126-
private Object reject(ProceedingJoinPoint point, AppAuth auth, String remark) throws Throwable {
122+
private Object processOwnerExistVerify(OwnerExistResult ownerExistResult, ArexContext context,
123+
ProceedingJoinPoint point, AppAuth auth)
124+
throws Throwable {
125+
if (ownerExistResult.getExist()) {
126+
context.setPassAuth(true);
127+
return point.proceed();
128+
} else {
129+
context.setPassAuth(false);
130+
return reject(point, auth, ownerExistResult.getRemark(), ownerExistResult.getResponseCode());
131+
}
132+
}
133+
134+
135+
private Object reject(ProceedingJoinPoint point, AppAuth auth, String remark, int responseCode)
136+
throws Throwable {
127137
switch (auth.rejectStrategy()) {
128138
case FAIL_RESPONSE:
129-
return ResponseUtils.errorResponse(remark, ResponseCode.AUTHENTICATION_FAILED);
139+
return ResponseUtils_New.errorResponse(remark, responseCode);
130140
case DOWNGRADE:
131141
ArexContext.getContext().setPassAuth(false);
132142
return point.proceed();
@@ -135,6 +145,7 @@ private Object reject(ProceedingJoinPoint point, AppAuth auth, String remark) th
135145
}
136146
}
137147

148+
138149
private void init() {
139150
authSwitch = Optional.ofNullable(
140151
systemConfigurationRepository.getSystemConfigByKey(KeySummary.AUTH_SWITCH))
@@ -149,5 +160,6 @@ public static class OwnerExistResult {
149160

150161
private Boolean exist;
151162
private String remark;
163+
private Integer responseCode;
152164
}
153165
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.arextest.storage.model;
2+
3+
import com.arextest.common.model.response.ResponseCode_New;
4+
5+
public class ArexStorageResponseCode extends ResponseCode_New {
6+
7+
8+
9+
10+
// app auth error codes start with 105xxx, shared with arex-saas-api
11+
// com.arextest.web.common.exception.ArexApiResponseCode
12+
public static final int APP_AUTH_NO_APP_ID = 105001;
13+
public static final int APP_AUTH_ERROR_APP_ID = 105002;
14+
public static final int APP_AUTH_NO_PERMISSION = 105003;
15+
16+
}

0 commit comments

Comments
 (0)