Skip to content

Commit 197795a

Browse files
authored
Fix #12395, use request context replace session depend. (#12398)
1 parent 298f36f commit 197795a

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

plugin-default-impl/nacos-default-auth-plugin/src/main/java/com/alibaba/nacos/plugin/auth/impl/controller/UserController.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.alibaba.nacos.common.model.RestResultUtils;
2424
import com.alibaba.nacos.common.utils.JacksonUtils;
2525
import com.alibaba.nacos.common.utils.StringUtils;
26+
import com.alibaba.nacos.core.context.RequestContextHolder;
2627
import com.alibaba.nacos.persistence.model.Page;
2728
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
2829
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
@@ -102,7 +103,11 @@ public class UserController {
102103
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "users", action = ActionTypes.WRITE)
103104
@PostMapping
104105
public Object createUser(@RequestParam String username, @RequestParam String password) {
105-
106+
if (AuthConstants.DEFAULT_USER.equals(username)) {
107+
return RestResultUtils.failed(HttpStatus.CONFLICT.value(),
108+
"User `nacos` is default admin user. Please use `/nacos/v1/auth/users/admin` API to init `nacos` users. "
109+
+ "Detail see `https://nacos.io/docs/latest/manual/admin/auth/#31-%E8%AE%BE%E7%BD%AE%E7%AE%A1%E7%90%86%E5%91%98%E5%AF%86%E7%A0%81`");
110+
}
106111
User user = userDetailsService.getUserFromDatabase(username);
107112
if (user != null) {
108113
throw new IllegalArgumentException("user '" + username + "' already exist!");
@@ -202,8 +207,7 @@ private boolean hasPermission(String username, HttpServletRequest request)
202207
if (!authConfigs.isAuthEnabled()) {
203208
return true;
204209
}
205-
IdentityContext identityContext = (IdentityContext) request.getSession()
206-
.getAttribute(com.alibaba.nacos.plugin.auth.constant.Constants.Identity.IDENTITY_CONTEXT);
210+
IdentityContext identityContext = RequestContextHolder.getContext().getAuthContext().getIdentityContext();
207211
if (identityContext == null) {
208212
throw new HttpSessionRequiredException("session expired!");
209213
}
@@ -324,7 +328,6 @@ public RestResult<String> updatePassword(@RequestParam(value = "oldPassword") St
324328
}
325329
}
326330

327-
328331
/**
329332
* Fuzzy matching username.
330333
*

plugin-default-impl/nacos-default-auth-plugin/src/test/java/com/alibaba/nacos/plugin/auth/impl/controller/UserControllerTest.java

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.alibaba.nacos.auth.config.AuthConfigs;
2020
import com.alibaba.nacos.common.model.RestResult;
21+
import com.alibaba.nacos.core.context.RequestContextHolder;
2122
import com.alibaba.nacos.persistence.model.Page;
2223
import com.alibaba.nacos.plugin.auth.api.IdentityContext;
2324
import com.alibaba.nacos.plugin.auth.exception.AccessException;
@@ -33,6 +34,7 @@
3334
import com.alibaba.nacos.sys.env.EnvUtil;
3435
import com.fasterxml.jackson.databind.JsonNode;
3536
import com.fasterxml.jackson.databind.node.ObjectNode;
37+
import org.junit.jupiter.api.AfterEach;
3638
import org.junit.jupiter.api.BeforeEach;
3739
import org.junit.jupiter.api.Test;
3840
import org.junit.jupiter.api.extension.ExtendWith;
@@ -105,6 +107,12 @@ void setUp() throws Exception {
105107
AuthConstants.DEFAULT_TOKEN_EXPIRE_SECONDS.toString());
106108

107109
EnvUtil.setEnvironment(mockEnvironment);
110+
RequestContextHolder.getContext().getAuthContext().setIdentityContext(new IdentityContext());
111+
}
112+
113+
@AfterEach
114+
public void tearDown() {
115+
RequestContextHolder.removeContext();
108116
}
109117

110118
@Test
@@ -123,20 +131,26 @@ void testLoginWithAuthedUser() throws AccessException, IOException {
123131

124132
@Test
125133
void testCreateUser1() {
126-
when(userDetailsService.getUserFromDatabase("nacos")).thenReturn(null);
127-
RestResult<String> result = (RestResult<String>) userController.createUser("nacos", "test");
134+
when(userDetailsService.getUserFromDatabase("test")).thenReturn(null);
135+
RestResult<String> result = (RestResult<String>) userController.createUser("test", "test");
128136
assertEquals(200, result.getCode());
129137

130138
}
131139

132140
@Test
133141
void testCreateUser2() {
134-
when(userDetailsService.getUserFromDatabase("nacos")).thenReturn(new User());
142+
when(userDetailsService.getUserFromDatabase("test")).thenReturn(new User());
135143
assertThrows(IllegalArgumentException.class, () -> {
136-
userController.createUser("nacos", "test");
144+
userController.createUser("test", "test");
137145
});
138146
}
139147

148+
@Test
149+
void testCreateUserNamedNacos() {
150+
RestResult<String> result = (RestResult<String>) userController.createUser("nacos", "test");
151+
assertEquals(409, result.getCode());
152+
}
153+
140154
@Test
141155
void testCreateAdminUser1() {
142156
when(authConfigs.getNacosAuthSystemType()).thenReturn(AuthSystemTypes.NACOS.name());
@@ -221,7 +235,7 @@ void testUpdateUser2() {
221235

222236
@Test
223237
void testUpdateUser3() throws IOException {
224-
238+
RequestContextHolder.getContext().getAuthContext().setIdentityContext(null);
225239
when(authConfigs.isAuthEnabled()).thenReturn(true);
226240
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
227241
MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
@@ -234,15 +248,11 @@ void testUpdateUser3() throws IOException {
234248

235249
@Test
236250
void testUpdateUser4() throws IOException {
237-
251+
RequestContextHolder.getContext().getAuthContext().getIdentityContext()
252+
.setParameter(AuthConstants.NACOS_USER_KEY, user);
238253
when(authConfigs.isAuthEnabled()).thenReturn(true);
239254
when(userDetailsService.getUserFromDatabase(anyString())).thenReturn(new User());
240255
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
241-
IdentityContext identityContext = new IdentityContext();
242-
identityContext.setParameter(AuthConstants.NACOS_USER_KEY, user);
243-
mockHttpServletRequest.getSession()
244-
.setAttribute(com.alibaba.nacos.plugin.auth.constant.Constants.Identity.IDENTITY_CONTEXT,
245-
identityContext);
246256
MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
247257
RestResult<String> result = (RestResult<String>) userController.updateUser("nacos", "test",
248258
mockHttpServletResponse, mockHttpServletRequest);
@@ -252,17 +262,13 @@ void testUpdateUser4() throws IOException {
252262

253263
@Test
254264
void testUpdateUser5() throws IOException, AccessException {
255-
265+
RequestContextHolder.getContext().getAuthContext().getIdentityContext()
266+
.setParameter(AuthConstants.NACOS_USER_KEY, null);
256267
when(authConfigs.isAuthEnabled()).thenReturn(true);
257268
when(userDetailsService.getUserFromDatabase(anyString())).thenReturn(new User());
258269
when(authenticationManager.authenticate(any(MockHttpServletRequest.class))).thenReturn(user);
259270

260271
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
261-
IdentityContext identityContext = new IdentityContext();
262-
identityContext.setParameter(AuthConstants.NACOS_USER_KEY, null);
263-
mockHttpServletRequest.getSession()
264-
.setAttribute(com.alibaba.nacos.plugin.auth.constant.Constants.Identity.IDENTITY_CONTEXT,
265-
identityContext);
266272
MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
267273
RestResult<String> result = (RestResult<String>) userController.updateUser("nacos", "test",
268274
mockHttpServletResponse, mockHttpServletRequest);
@@ -272,16 +278,12 @@ void testUpdateUser5() throws IOException, AccessException {
272278

273279
@Test
274280
void testUpdateUser6() throws IOException, AccessException {
275-
281+
RequestContextHolder.getContext().getAuthContext().getIdentityContext()
282+
.setParameter(AuthConstants.NACOS_USER_KEY, null);
276283
when(authConfigs.isAuthEnabled()).thenReturn(true);
277284
when(authenticationManager.authenticate(any(MockHttpServletRequest.class))).thenReturn(null);
278285

279286
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
280-
IdentityContext identityContext = new IdentityContext();
281-
identityContext.setParameter(AuthConstants.NACOS_USER_KEY, null);
282-
mockHttpServletRequest.getSession()
283-
.setAttribute(com.alibaba.nacos.plugin.auth.constant.Constants.Identity.IDENTITY_CONTEXT,
284-
identityContext);
285287
MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
286288
Object result = userController.updateUser("nacos", "test", mockHttpServletResponse, mockHttpServletRequest);
287289

@@ -292,17 +294,13 @@ void testUpdateUser6() throws IOException, AccessException {
292294

293295
@Test
294296
void testUpdateUser7() throws IOException, AccessException {
295-
297+
RequestContextHolder.getContext().getAuthContext().getIdentityContext()
298+
.setParameter(AuthConstants.NACOS_USER_KEY, null);
296299
when(authConfigs.isAuthEnabled()).thenReturn(true);
297300
when(authenticationManager.authenticate(any(MockHttpServletRequest.class))).thenThrow(
298301
new AccessException("test"));
299302

300303
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
301-
IdentityContext identityContext = new IdentityContext();
302-
identityContext.setParameter(AuthConstants.NACOS_USER_KEY, null);
303-
mockHttpServletRequest.getSession()
304-
.setAttribute(com.alibaba.nacos.plugin.auth.constant.Constants.Identity.IDENTITY_CONTEXT,
305-
identityContext);
306304
MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
307305
Object result = userController.updateUser("nacos", "test", mockHttpServletResponse, mockHttpServletRequest);
308306

0 commit comments

Comments
 (0)