use of com.alibaba.nacos.api.remote.request.Request in project XHuiCloud by sindaZeng.
the class NacosAuthManager method loginRemote.
@Override
public User loginRemote(Object request) throws AccessException {
Request req = (Request) request;
String token = resolveToken(req);
if (StringUtils.isBlank(token)) {
throw new AccessException("user not found!");
}
try {
tokenManager.validateToken(token);
} catch (ExpiredJwtException e) {
throw new AccessException("token expired!");
} catch (Exception e) {
throw new AccessException("token invalid!");
}
Authentication authentication = tokenManager.getAuthentication(token);
SecurityContextHolder.getContext().setAuthentication(authentication);
String username = authentication.getName();
NacosUser user = new NacosUser();
user.setUserName(username);
user.setToken(token);
List<RoleInfo> roleInfoList = roleService.getRoles(username);
if (roleInfoList != null) {
for (RoleInfo roleInfo : roleInfoList) {
if (roleInfo.getRole().equals(NacosRoleServiceImpl.GLOBAL_ADMIN_ROLE)) {
user.setGlobalAdmin(true);
break;
}
}
}
return user;
}
use of com.alibaba.nacos.api.remote.request.Request in project nacos by alibaba.
the class PayloadRegistry method scan.
private static synchronized void scan() {
if (initialized) {
return;
}
List<String> requestScanPackage = Arrays.asList("com.alibaba.nacos.api.naming.remote.request", "com.alibaba.nacos.api.config.remote.request", "com.alibaba.nacos.api.remote.request", "com.alibaba.nacos.naming.cluster.remote.request");
for (String pkg : requestScanPackage) {
Reflections reflections = new Reflections(pkg);
Set<Class<? extends Request>> subTypesRequest = reflections.getSubTypesOf(Request.class);
for (Class<?> clazz : subTypesRequest) {
register(clazz.getSimpleName(), clazz);
}
}
List<String> responseScanPackage = Arrays.asList("com.alibaba.nacos.api.naming.remote.response", "com.alibaba.nacos.api.config.remote.response", "com.alibaba.nacos.api.remote.response", "com.alibaba.nacos.naming.cluster.remote.response");
for (String pkg : responseScanPackage) {
Reflections reflections = new Reflections(pkg);
Set<Class<? extends Response>> subTypesOfResponse = reflections.getSubTypesOf(Response.class);
for (Class<?> clazz : subTypesOfResponse) {
register(clazz.getSimpleName(), clazz);
}
}
initialized = true;
}
use of com.alibaba.nacos.api.remote.request.Request in project nacos by alibaba.
the class NamingGrpcClientProxyTest method testServerListChanged.
@Test
public void testServerListChanged() throws Exception {
RpcClient rpc = new RpcClient("testServerListHasChanged", factory) {
@Override
public ConnectionType getConnectionType() {
return ConnectionType.GRPC;
}
@Override
public int rpcPortOffset() {
return 0;
}
@Override
public Connection connectToServer(ServerInfo serverInfo) throws Exception {
return new Connection(serverInfo) {
@Override
public Response request(Request request, long timeoutMills) throws NacosException {
Response response = new Response() {
};
response.setRequestId(request.getRequestId());
return response;
}
@Override
public RequestFuture requestFuture(Request request) throws NacosException {
return new DefaultRequestFuture("test", request.getRequestId());
}
@Override
public void asyncRequest(Request request, RequestCallBack requestCallBack) throws NacosException {
}
@Override
public void close() {
}
};
}
};
Field rpcClient = NamingGrpcClientProxy.class.getDeclaredField("rpcClient");
rpcClient.setAccessible(true);
rpcClient.set(client, rpc);
rpc.serverListFactory(factory);
rpc.registerServerRequestHandler(new NamingPushRequestHandler(holder));
Field listenerField = NamingGrpcClientProxy.class.getDeclaredField("redoService");
listenerField.setAccessible(true);
NamingGrpcRedoService listener = (NamingGrpcRedoService) listenerField.get(client);
rpc.registerConnectionListener(listener);
rpc.start();
int retry = 10;
while (!rpc.isRunning()) {
TimeUnit.SECONDS.sleep(1);
if (--retry < 0) {
Assert.fail("rpc is not running");
}
}
Assert.assertEquals(ORIGIN_SERVER, rpc.getCurrentServer().getServerIp());
String newServer = "www.aliyun.com";
when(factory.genNextServer()).thenReturn(newServer);
when(factory.getServerList()).thenReturn(Stream.of(newServer, "anotherServer").collect(Collectors.toList()));
NotifyCenter.publishEvent(new ServerListChangedEvent());
retry = 10;
while (ORIGIN_SERVER.equals(rpc.getCurrentServer().getServerIp())) {
TimeUnit.SECONDS.sleep(1);
if (--retry < 0) {
Assert.fail("failed to auth switch server");
}
}
Assert.assertEquals(newServer, rpc.getCurrentServer().getServerIp());
}
use of com.alibaba.nacos.api.remote.request.Request in project nacos by alibaba.
the class RpcClientTest method testRequestWhenResponseUnregisterThenSwitchServer.
@Test
public void testRequestWhenResponseUnregisterThenSwitchServer() throws NacosException {
rpcClient.rpcClientStatus.set(RpcClientStatus.RUNNING);
rpcClient.currentConnection = connection;
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setErrorCode(NacosException.UN_REGISTER);
doReturn(errorResponse).when(connection).request(any(), anyLong());
Exception exception = null;
try {
rpcClient.request(mock(Request.class), 10000);
} catch (Exception e) {
exception = e;
}
Assert.assertEquals(RpcClientStatus.UNHEALTHY, rpcClient.rpcClientStatus.get());
verify(rpcClient).switchServerAsync();
Assert.assertNotNull(exception);
}
use of com.alibaba.nacos.api.remote.request.Request in project nacos by alibaba.
the class RemoteRequestAuthFilterTest method testFilter.
@Test
public void testFilter() {
Mockito.when(authConfigs.isAuthEnabled()).thenReturn(true);
Request healthCheckRequest = new HealthCheckRequest();
try {
Response healthCheckResponse = remoteRequestAuthFilter.filter(healthCheckRequest, new RequestMeta(), MockRequestHandler.class);
Assert.assertNull(healthCheckResponse);
} catch (NacosException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
Aggregations