diff --git a/config/src/main/java/com/alibaba/nacos/config/server/utils/RequestUtil.java b/config/src/main/java/com/alibaba/nacos/config/server/utils/RequestUtil.java index fd566906a..0e34c86d0 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/utils/RequestUtil.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/utils/RequestUtil.java @@ -18,6 +18,9 @@ package com.alibaba.nacos.config.server.utils; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.common.utils.StringUtils; +import com.alibaba.nacos.core.context.RequestContextHolder; +import com.alibaba.nacos.core.utils.WebUtils; +import com.alibaba.nacos.plugin.auth.api.IdentityContext; import javax.servlet.http.HttpServletRequest; @@ -28,30 +31,24 @@ import javax.servlet.http.HttpServletRequest; */ public class RequestUtil { - private static final String X_REAL_IP = "X-Real-IP"; - - private static final String X_FORWARDED_FOR = "X-Forwarded-For"; - - private static final String X_FORWARDED_FOR_SPLIT_SYMBOL = ","; - public static final String CLIENT_APPNAME_HEADER = "Client-AppName"; /** - * get real client ip - * - *
first use X-Forwarded-For header https://zh.wikipedia.org/wiki/X-Forwarded-For next nginx X-Real-IP last - * {@link HttpServletRequest#getRemoteAddr()} + * Get real client ip from context first, if no value, use + * {@link com.alibaba.nacos.core.utils.WebUtils#getRemoteIp(HttpServletRequest)}. * * @param request {@link HttpServletRequest} * @return remote ip address. */ public static String getRemoteIp(HttpServletRequest request) { - String xForwardedFor = request.getHeader(X_FORWARDED_FOR); - if (!StringUtils.isBlank(xForwardedFor)) { - return xForwardedFor.split(X_FORWARDED_FOR_SPLIT_SYMBOL)[0].trim(); + String remoteIp = RequestContextHolder.getContext().getBasicContext().getAddressContext().getSourceIp(); + if (StringUtils.isBlank(remoteIp)) { + remoteIp = RequestContextHolder.getContext().getBasicContext().getAddressContext().getRemoteIp(); } - String nginxHeader = request.getHeader(X_REAL_IP); - return StringUtils.isBlank(nginxHeader) ? request.getRemoteAddr() : nginxHeader; + if (StringUtils.isBlank(remoteIp)) { + remoteIp = WebUtils.getRemoteIp(request); + } + return remoteIp; } /** @@ -61,7 +58,12 @@ public class RequestUtil { * @return may be return null */ public static String getAppName(HttpServletRequest request) { - return request.getHeader(CLIENT_APPNAME_HEADER); + String result = RequestContextHolder.getContext().getBasicContext().getApp(); + return isUnknownApp(result) ? request.getHeader(CLIENT_APPNAME_HEADER) : result; + } + + private static boolean isUnknownApp(String appName) { + return StringUtils.isBlank(appName) || StringUtils.equalsIgnoreCase("unknown", appName); } /** @@ -71,8 +73,12 @@ public class RequestUtil { * @return may be return null */ public static String getSrcUserName(HttpServletRequest request) { - String result = (String) request.getSession() - .getAttribute(com.alibaba.nacos.plugin.auth.constant.Constants.Identity.IDENTITY_ID); + IdentityContext identityContext = RequestContextHolder.getContext().getAuthContext().getIdentityContext(); + String result = StringUtils.EMPTY; + if (null != identityContext) { + result = (String) identityContext.getParameter( + com.alibaba.nacos.plugin.auth.constant.Constants.Identity.IDENTITY_ID); + } // If auth is disabled, get username from parameters by agreed key return StringUtils.isBlank(result) ? request.getParameter(Constants.USERNAME) : result; } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/utils/RequestUtilTest.java b/config/src/test/java/com/alibaba/nacos/config/server/utils/RequestUtilTest.java index eb25966ab..d272e2210 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/utils/RequestUtilTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/utils/RequestUtilTest.java @@ -17,11 +17,13 @@ package com.alibaba.nacos.config.server.utils; import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.core.context.RequestContextHolder; +import com.alibaba.nacos.plugin.auth.api.IdentityContext; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.eq; @@ -32,8 +34,13 @@ class RequestUtilTest { private static final String X_FORWARDED_FOR = "X-Forwarded-For"; + @AfterEach + void tearDown() { + RequestContextHolder.removeContext(); + } + @Test - void testGetRemoteIp() { + void testGetRemoteIpFromRequest() { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getRemoteAddr()).thenReturn("127.0.0.1"); @@ -56,29 +63,33 @@ class RequestUtilTest { } @Test - void testGetAppName() { + void testGetAppNameFromContext() { + RequestContextHolder.getContext().getBasicContext().setApp("contextApp"); + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + Mockito.when(request.getHeader(eq(RequestUtil.CLIENT_APPNAME_HEADER))).thenReturn("test"); + assertEquals("contextApp", RequestUtil.getAppName(request)); + } + + @Test + void testGetAppNameFromRequest() { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getHeader(eq(RequestUtil.CLIENT_APPNAME_HEADER))).thenReturn("test"); assertEquals("test", RequestUtil.getAppName(request)); } @Test - void testGetSrcUserNameV1() { + void testGetSrcUserNameFromContext() { + IdentityContext identityContext = new IdentityContext(); + identityContext.setParameter(com.alibaba.nacos.plugin.auth.constant.Constants.Identity.IDENTITY_ID, "test"); + RequestContextHolder.getContext().getAuthContext().setIdentityContext(identityContext); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - HttpSession session = Mockito.mock(HttpSession.class); - Mockito.when(request.getSession()).thenReturn(session); - Mockito.when(session.getAttribute(eq(com.alibaba.nacos.plugin.auth.constant.Constants.Identity.IDENTITY_ID))).thenReturn("test"); assertEquals("test", RequestUtil.getSrcUserName(request)); } @Test - void testGetSrcUserNameV2() { + void testGetSrcUserNameFromRequest() { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - HttpSession session = Mockito.mock(HttpSession.class); - Mockito.when(request.getSession()).thenReturn(session); - Mockito.when(session.getAttribute(eq(com.alibaba.nacos.plugin.auth.constant.Constants.Identity.IDENTITY_ID))).thenReturn(null); Mockito.when(request.getParameter(eq(Constants.USERNAME))).thenReturn("parameterName"); assertEquals("parameterName", RequestUtil.getSrcUserName(request)); } - } diff --git a/core/src/main/java/com/alibaba/nacos/core/auth/AuthFilter.java b/core/src/main/java/com/alibaba/nacos/core/auth/AuthFilter.java index 55c00f985..5bf98b5f1 100644 --- a/core/src/main/java/com/alibaba/nacos/core/auth/AuthFilter.java +++ b/core/src/main/java/com/alibaba/nacos/core/auth/AuthFilter.java @@ -22,6 +22,8 @@ import com.alibaba.nacos.auth.config.AuthConfigs; import com.alibaba.nacos.common.utils.ExceptionUtil; import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.core.code.ControllerMethodsCache; +import com.alibaba.nacos.core.context.RequestContext; +import com.alibaba.nacos.core.context.RequestContextHolder; import com.alibaba.nacos.core.utils.Loggers; import com.alibaba.nacos.core.utils.WebUtils; import com.alibaba.nacos.plugin.auth.api.IdentityContext; @@ -120,11 +122,16 @@ public class AuthFilter implements Filter { Resource resource = protocolAuthService.parseResource(req, secured); IdentityContext identityContext = protocolAuthService.parseIdentity(req); boolean result = protocolAuthService.validateIdentity(identityContext, resource); + RequestContext requestContext = RequestContextHolder.getContext(); + requestContext.getAuthContext().setIdentityContext(identityContext); + requestContext.getAuthContext().setResource(resource); + if (null == requestContext.getAuthContext().getAuthResult()) { + requestContext.getAuthContext().setAuthResult(result); + } if (!result) { // TODO Get reason of failure throw new AccessException("Validate Identity failed."); } - injectIdentityId(req, identityContext); String action = secured.action().toString(); result = protocolAuthService.validateAuthority(identityContext, new Permission(resource, action)); if (!result) { @@ -146,21 +153,4 @@ public class AuthFilter implements Filter { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Server failed, " + e.getMessage()); } } - - /** - * Set identity id to request session, make sure some actual logic can get identity information. - * - *
May be replaced with whole identityContext. - * - * @param request http request - * @param identityContext identity context - */ - private void injectIdentityId(HttpServletRequest request, IdentityContext identityContext) { - String identityId = identityContext.getParameter( - com.alibaba.nacos.plugin.auth.constant.Constants.Identity.IDENTITY_ID, StringUtils.EMPTY); - request.getSession() - .setAttribute(com.alibaba.nacos.plugin.auth.constant.Constants.Identity.IDENTITY_ID, identityId); - request.getSession().setAttribute(com.alibaba.nacos.plugin.auth.constant.Constants.Identity.IDENTITY_CONTEXT, - identityContext); - } } diff --git a/core/src/main/java/com/alibaba/nacos/core/auth/RemoteRequestAuthFilter.java b/core/src/main/java/com/alibaba/nacos/core/auth/RemoteRequestAuthFilter.java index ad9a250b0..21b000cc9 100644 --- a/core/src/main/java/com/alibaba/nacos/core/auth/RemoteRequestAuthFilter.java +++ b/core/src/main/java/com/alibaba/nacos/core/auth/RemoteRequestAuthFilter.java @@ -24,6 +24,8 @@ import com.alibaba.nacos.auth.GrpcProtocolAuthService; import com.alibaba.nacos.auth.annotation.Secured; import com.alibaba.nacos.auth.config.AuthConfigs; import com.alibaba.nacos.common.utils.ExceptionUtil; +import com.alibaba.nacos.core.context.RequestContext; +import com.alibaba.nacos.core.context.RequestContextHolder; import com.alibaba.nacos.core.remote.AbstractRequestFilter; import com.alibaba.nacos.core.utils.Loggers; import com.alibaba.nacos.plugin.auth.api.IdentityContext; @@ -75,6 +77,12 @@ public class RemoteRequestAuthFilter extends AbstractRequestFilter { Resource resource = protocolAuthService.parseResource(request, secured); IdentityContext identityContext = protocolAuthService.parseIdentity(request); boolean result = protocolAuthService.validateIdentity(identityContext, resource); + RequestContext requestContext = RequestContextHolder.getContext(); + requestContext.getAuthContext().setIdentityContext(identityContext); + requestContext.getAuthContext().setResource(resource); + if (null == requestContext.getAuthContext().getAuthResult()) { + requestContext.getAuthContext().setAuthResult(result); + } if (!result) { // TODO Get reason of failure throw new AccessException("Validate Identity failed."); diff --git a/core/src/main/java/com/alibaba/nacos/core/context/RequestContext.java b/core/src/main/java/com/alibaba/nacos/core/context/RequestContext.java new file mode 100644 index 000000000..24b0aea49 --- /dev/null +++ b/core/src/main/java/com/alibaba/nacos/core/context/RequestContext.java @@ -0,0 +1,96 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.core.context; + +import com.alibaba.nacos.core.context.addition.AuthContext; +import com.alibaba.nacos.core.context.addition.BasicContext; +import com.alibaba.nacos.core.context.addition.EngineContext; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +/** + * Nacos request context. + * + * @author xiweng.yy + */ +public class RequestContext { + + /** + * Optional, the request id. + *
TODO with more auth result by auth plugin. + */ + private Object authResult; + + public IdentityContext getIdentityContext() { + return identityContext; + } + + public void setIdentityContext(IdentityContext identityContext) { + this.identityContext = identityContext; + } + + public Resource getResource() { + return resource; + } + + public void setResource(Resource resource) { + this.resource = resource; + } + + public Object getAuthResult() { + return authResult; + } + + public void setAuthResult(Object authResult) { + this.authResult = authResult; + } +} diff --git a/core/src/main/java/com/alibaba/nacos/core/context/addition/BasicContext.java b/core/src/main/java/com/alibaba/nacos/core/context/addition/BasicContext.java new file mode 100644 index 000000000..4c712f83f --- /dev/null +++ b/core/src/main/java/com/alibaba/nacos/core/context/addition/BasicContext.java @@ -0,0 +1,114 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.core.context.addition; + +import com.alibaba.nacos.api.common.Constants; + +/** + * Nacos request basic information context. + * + * @author xiweng.yy + */ +public class BasicContext { + + private static final String DEFAULT_APP = "unknown"; + + public static final String HTTP_PROTOCOL = "HTTP"; + + public static final String GRPC_PROTOCOL = "GRPC"; + + private final AddressContext addressContext; + + /** + * Request user agent, such as Nacos-Java-client:v2.4.0 + */ + private String userAgent; + + /** + * Request protocol type, HTTP or GRPC and so on. + */ + private String requestProtocol; + + /** + * Request target. + *
first use X-Forwarded-For header https://zh.wikipedia.org/wiki/X-Forwarded-For next nginx X-Real-IP last
+ * {@link HttpServletRequest#getRemoteAddr()}
+ *
+ * @param request {@link HttpServletRequest}
+ * @return remote ip address.
+ */
+ public static String getRemoteIp(HttpServletRequest request) {
+ String xForwardedFor = request.getHeader(X_FORWARDED_FOR);
+ if (!StringUtils.isBlank(xForwardedFor)) {
+ return xForwardedFor.split(X_FORWARDED_FOR_SPLIT_SYMBOL)[0].trim();
+ }
+ String nginxHeader = request.getHeader(X_REAL_IP);
+ return StringUtils.isBlank(nginxHeader) ? request.getRemoteAddr() : nginxHeader;
+ }
}
diff --git a/core/src/test/java/com/alibaba/nacos/core/auth/AuthFilterTest.java b/core/src/test/java/com/alibaba/nacos/core/auth/AuthFilterTest.java
index db7e3a72c..5baa64bf2 100644
--- a/core/src/test/java/com/alibaba/nacos/core/auth/AuthFilterTest.java
+++ b/core/src/test/java/com/alibaba/nacos/core/auth/AuthFilterTest.java
@@ -21,7 +21,9 @@ import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.core.code.ControllerMethodsCache;
+import com.alibaba.nacos.core.context.RequestContextHolder;
import com.alibaba.nacos.sys.env.Constants;
+import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
@@ -58,6 +60,11 @@ class AuthFilterTest {
@Mock
private ControllerMethodsCache methodsCache;
+ @AfterEach
+ void tearDown() {
+ RequestContextHolder.removeContext();
+ }
+
@Test
void testDoFilter() {
try {
diff --git a/core/src/test/java/com/alibaba/nacos/core/auth/RemoteRequestAuthFilterTest.java b/core/src/test/java/com/alibaba/nacos/core/auth/RemoteRequestAuthFilterTest.java
index c0256f071..61f5963fc 100644
--- a/core/src/test/java/com/alibaba/nacos/core/auth/RemoteRequestAuthFilterTest.java
+++ b/core/src/test/java/com/alibaba/nacos/core/auth/RemoteRequestAuthFilterTest.java
@@ -24,7 +24,9 @@ import com.alibaba.nacos.api.remote.request.RequestMeta;
import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.auth.config.AuthConfigs;
+import com.alibaba.nacos.core.context.RequestContextHolder;
import com.alibaba.nacos.core.remote.RequestHandler;
+import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
@@ -50,6 +52,11 @@ class RemoteRequestAuthFilterTest {
@Mock
private AuthConfigs authConfigs;
+ @AfterEach
+ void tearDown() {
+ RequestContextHolder.removeContext();
+ }
+
@Test
void testFilter() {
Mockito.when(authConfigs.isAuthEnabled()).thenReturn(true);
diff --git a/core/src/test/java/com/alibaba/nacos/core/context/RequestContextHolderTest.java b/core/src/test/java/com/alibaba/nacos/core/context/RequestContextHolderTest.java
new file mode 100644
index 000000000..38074cc94
--- /dev/null
+++ b/core/src/test/java/com/alibaba/nacos/core/context/RequestContextHolderTest.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 1999-2023 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.nacos.core.context;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class RequestContextHolderTest {
+
+ @AfterEach
+ void tearDown() {
+ RequestContextHolder.removeContext();
+ }
+
+ @Test
+ void testGetContext() {
+ long timestamp = System.currentTimeMillis();
+ RequestContext requestContext = RequestContextHolder.getContext();
+ assertNotNull(requestContext);
+ assertNotNull(requestContext.getRequestId());
+ assertTrue(requestContext.getRequestTimestamp() >= timestamp);
+ assertNotNull(requestContext.getBasicContext());
+ assertNotNull(requestContext.getEngineContext());
+ assertNotNull(requestContext.getAuthContext());
+ }
+}
\ No newline at end of file
diff --git a/core/src/test/java/com/alibaba/nacos/core/context/RequestContextTest.java b/core/src/test/java/com/alibaba/nacos/core/context/RequestContextTest.java
new file mode 100644
index 000000000..4392f7f65
--- /dev/null
+++ b/core/src/test/java/com/alibaba/nacos/core/context/RequestContextTest.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 1999-2023 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.nacos.core.context;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.UUID;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class RequestContextTest {
+
+ long requestTimestamp;
+
+ RequestContext requestContext;
+
+ @BeforeEach
+ void setUp() {
+ requestTimestamp = System.currentTimeMillis();
+ requestContext = new RequestContext(requestTimestamp);
+ }
+
+ @Test
+ public void testGetRequestId() {
+ String requestId = requestContext.getRequestId();
+ assertNotNull(requestId);
+ assertNotNull(UUID.fromString(requestId));
+ requestContext.setRequestId("testRequestId");
+ assertEquals("testRequestId", requestContext.getRequestId());
+ }
+
+ @Test
+ public void testGetRequestTimestamp() {
+ assertEquals(requestTimestamp, requestContext.getRequestTimestamp());
+ }
+
+ @Test
+ public void testSetExtensionContext() {
+ assertNull(requestContext.getExtensionContext("testKey"));
+ requestContext.addExtensionContext("testKey", "testValue");
+ assertEquals("testValue", requestContext.getExtensionContext("testKey"));
+ }
+}
\ No newline at end of file
diff --git a/core/src/test/java/com/alibaba/nacos/core/context/addition/AddressContextTest.java b/core/src/test/java/com/alibaba/nacos/core/context/addition/AddressContextTest.java
new file mode 100644
index 000000000..ce123f3e0
--- /dev/null
+++ b/core/src/test/java/com/alibaba/nacos/core/context/addition/AddressContextTest.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 1999-2023 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.nacos.core.context.addition;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class AddressContextTest {
+
+ AddressContext addressContext;
+
+ @BeforeEach
+ void setUp() {
+ addressContext = new AddressContext();
+ }
+
+ @Test
+ void testSetSourceIp() {
+ assertNull(addressContext.getSourceIp());
+ addressContext.setSourceIp("127.0.0.1");
+ assertEquals("127.0.0.1", addressContext.getSourceIp());
+ }
+
+ @Test
+ void testSetSourcePort() {
+ assertEquals(0, addressContext.getSourcePort());
+ addressContext.setSourcePort(8080);
+ assertEquals(8080, addressContext.getSourcePort());
+ }
+
+ @Test
+ void testSetRemoteIp() {
+ assertNull(addressContext.getRemoteIp());
+ addressContext.setRemoteIp("127.0.0.1");
+ assertEquals("127.0.0.1", addressContext.getRemoteIp());
+ }
+
+ @Test
+ void testSetRemotePort() {
+ assertEquals(0, addressContext.getRemotePort());
+ addressContext.setRemotePort(8080);
+ assertEquals(8080, addressContext.getRemotePort());
+ }
+
+ @Test
+ void testSetHost() {
+ assertNull(addressContext.getHost());
+ addressContext.setHost("127.0.0.1");
+ assertEquals("127.0.0.1", addressContext.getHost());
+ }
+}
\ No newline at end of file
diff --git a/core/src/test/java/com/alibaba/nacos/core/context/addition/AuthContextTest.java b/core/src/test/java/com/alibaba/nacos/core/context/addition/AuthContextTest.java
new file mode 100644
index 000000000..972749994
--- /dev/null
+++ b/core/src/test/java/com/alibaba/nacos/core/context/addition/AuthContextTest.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 1999-2023 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.nacos.core.context.addition;
+
+import com.alibaba.nacos.plugin.auth.api.IdentityContext;
+import com.alibaba.nacos.plugin.auth.api.Resource;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Properties;
+
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class AuthContextTest {
+
+ AuthContext authContext;
+
+ @BeforeEach
+ void setUp() {
+ authContext = new AuthContext();
+ }
+
+ @Test
+ void testSetIdentityContext() {
+ IdentityContext identityContext = new IdentityContext();
+ assertNull(authContext.getIdentityContext());
+ authContext.setIdentityContext(identityContext);
+ assertSame(identityContext, authContext.getIdentityContext());
+ }
+
+ @Test
+ void testSetResource() {
+ Resource resource = new Resource("", "", "", "", new Properties());
+ assertNull(authContext.getResource());
+ authContext.setResource(resource);
+ assertSame(resource, authContext.getResource());
+ }
+
+ @Test
+ void testSetAuthResult() {
+ assertNull(authContext.getAuthResult());
+ authContext.setAuthResult(true);
+ assertTrue((boolean) authContext.getAuthResult());
+ }
+}
\ No newline at end of file
diff --git a/core/src/test/java/com/alibaba/nacos/core/context/addition/BasicContextTest.java b/core/src/test/java/com/alibaba/nacos/core/context/addition/BasicContextTest.java
new file mode 100644
index 000000000..90430f900
--- /dev/null
+++ b/core/src/test/java/com/alibaba/nacos/core/context/addition/BasicContextTest.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright 1999-2023 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.nacos.core.context.addition;
+
+import com.alibaba.nacos.api.common.Constants;
+import com.alibaba.nacos.api.naming.remote.request.InstanceRequest;
+import com.alibaba.nacos.common.utils.VersionUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class BasicContextTest {
+
+ BasicContext basicContext;
+
+ @BeforeEach
+ void setUp() {
+ basicContext = new BasicContext();
+ }
+
+ @Test
+ void testGetAddressContext() {
+ assertNotNull(basicContext.getAddressContext());
+ }
+
+ @Test
+ void testSetUserAgent() {
+ assertNull(basicContext.getUserAgent());
+ basicContext.setUserAgent(VersionUtils.getFullClientVersion());
+ assertEquals(VersionUtils.getFullClientVersion(), basicContext.getUserAgent());
+ }
+
+ @Test
+ void testSetRequestProtocol() {
+ assertNull(basicContext.getRequestProtocol());
+ basicContext.setRequestProtocol(BasicContext.HTTP_PROTOCOL);
+ assertEquals(BasicContext.HTTP_PROTOCOL, basicContext.getRequestProtocol());
+ basicContext.setRequestProtocol(BasicContext.GRPC_PROTOCOL);
+ assertEquals(BasicContext.GRPC_PROTOCOL, basicContext.getRequestProtocol());
+ }
+
+ @Test
+ void testSetRequestTarget() {
+ assertNull(basicContext.getRequestTarget());
+ basicContext.setRequestTarget("POST /v2/ns/instance");
+ assertEquals("POST /v2/ns/instance", basicContext.getRequestTarget());
+ basicContext.setRequestTarget(InstanceRequest.class.getSimpleName());
+ assertEquals(InstanceRequest.class.getSimpleName(), basicContext.getRequestTarget());
+ }
+
+ @Test
+ void testSetApp() {
+ assertEquals("unknown", basicContext.getApp());
+ basicContext.setApp("testApp");
+ assertEquals("testApp", basicContext.getApp());
+ }
+
+ @Test
+ void testSetEncoding() {
+ assertEquals(Constants.ENCODE, basicContext.getEncoding());
+ basicContext.setEncoding("GBK");
+ assertEquals("GBK", basicContext.getEncoding());
+ }
+}
\ No newline at end of file
diff --git a/core/src/test/java/com/alibaba/nacos/core/context/addition/EngineContextTest.java b/core/src/test/java/com/alibaba/nacos/core/context/addition/EngineContextTest.java
new file mode 100644
index 000000000..b1a1e27c8
--- /dev/null
+++ b/core/src/test/java/com/alibaba/nacos/core/context/addition/EngineContextTest.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 1999-2023 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.nacos.core.context.addition;
+
+import com.alibaba.nacos.common.utils.VersionUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class EngineContextTest {
+
+ EngineContext engineContext;
+
+ @BeforeEach
+ void setUp() {
+ engineContext = new EngineContext();
+ }
+
+ @Test
+ void testSetVersion() {
+ assertEquals(VersionUtils.version, engineContext.getVersion());
+ engineContext.setVersion("testVersion");
+ assertEquals("testVersion", engineContext.getVersion());
+ }
+
+ @Test
+ void testSetContext() {
+ assertNull(engineContext.getContext("test"));
+ assertEquals("default", engineContext.getContext("test", "default"));
+ engineContext.setContext("test", "testValue");
+ assertEquals("testValue", engineContext.getContext("test"));
+ assertEquals("testValue", engineContext.getContext("test", "default"));
+ }
+}
\ No newline at end of file
diff --git a/core/src/test/java/com/alibaba/nacos/core/context/remote/HttpRequestContextConfigTest.java b/core/src/test/java/com/alibaba/nacos/core/context/remote/HttpRequestContextConfigTest.java
new file mode 100644
index 000000000..998778215
--- /dev/null
+++ b/core/src/test/java/com/alibaba/nacos/core/context/remote/HttpRequestContextConfigTest.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 1999-2023 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.nacos.core.context.remote;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+class HttpRequestContextConfigTest {
+
+ @Test
+ void testRequestContextFilterRegistration() {
+ HttpRequestContextConfig contextConfig = new HttpRequestContextConfig();
+ HttpRequestContextFilter filter = contextConfig.nacosRequestContextFilter();
+ FilterRegistrationBean