Search in sources :

Example 1 with Request

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;
}
Also used : AccessException(com.alibaba.nacos.auth.exception.AccessException) ExpiredJwtException(io.jsonwebtoken.ExpiredJwtException) NacosUser(com.alibaba.nacos.security.nacos.users.NacosUser) RoleInfo(com.alibaba.nacos.config.server.auth.RoleInfo) Authentication(org.springframework.security.core.Authentication) Request(com.alibaba.nacos.api.remote.request.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) AccessException(com.alibaba.nacos.auth.exception.AccessException) ExpiredJwtException(io.jsonwebtoken.ExpiredJwtException) AuthenticationException(org.springframework.security.core.AuthenticationException)

Example 2 with Request

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;
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) Request(com.alibaba.nacos.api.remote.request.Request) Reflections(org.reflections.Reflections)

Example 3 with Request

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());
}
Also used : ServerListChangedEvent(com.alibaba.nacos.client.naming.event.ServerListChangedEvent) DefaultRequestFuture(com.alibaba.nacos.api.remote.DefaultRequestFuture) NamingGrpcRedoService(com.alibaba.nacos.client.naming.remote.gprc.redo.NamingGrpcRedoService) RequestCallBack(com.alibaba.nacos.api.remote.RequestCallBack) Connection(com.alibaba.nacos.common.remote.client.Connection) Request(com.alibaba.nacos.api.remote.request.Request) InstanceRequest(com.alibaba.nacos.api.naming.remote.request.InstanceRequest) SubscribeServiceRequest(com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest) QueryServiceResponse(com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse) Response(com.alibaba.nacos.api.remote.response.Response) SubscribeServiceResponse(com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse) ServiceListResponse(com.alibaba.nacos.api.naming.remote.response.ServiceListResponse) InstanceResponse(com.alibaba.nacos.api.naming.remote.response.InstanceResponse) Field(java.lang.reflect.Field) RpcClient(com.alibaba.nacos.common.remote.client.RpcClient) Test(org.junit.Test)

Example 4 with Request

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);
}
Also used : Request(com.alibaba.nacos.api.remote.request.Request) NacosException(com.alibaba.nacos.api.exception.NacosException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) Test(org.junit.Test)

Example 5 with Request

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());
    }
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) HealthCheckRequest(com.alibaba.nacos.api.remote.request.HealthCheckRequest) Request(com.alibaba.nacos.api.remote.request.Request) HealthCheckRequest(com.alibaba.nacos.api.remote.request.HealthCheckRequest) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.Test)

Aggregations

Request (com.alibaba.nacos.api.remote.request.Request)21 Test (org.junit.Test)13 Response (com.alibaba.nacos.api.remote.response.Response)8 Secured (com.alibaba.nacos.auth.annotation.Secured)8 Resource (com.alibaba.nacos.plugin.auth.api.Resource)8 NacosException (com.alibaba.nacos.api.exception.NacosException)6 ConfigBatchListenRequest (com.alibaba.nacos.api.config.remote.request.ConfigBatchListenRequest)5 AbstractNamingRequest (com.alibaba.nacos.api.naming.remote.request.AbstractNamingRequest)5 NotifySubscriberRequest (com.alibaba.nacos.api.naming.remote.request.NotifySubscriberRequest)5 ConfigPublishRequest (com.alibaba.nacos.api.config.remote.request.ConfigPublishRequest)4 ErrorResponse (com.alibaba.nacos.api.remote.response.ErrorResponse)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 RequestCallBack (com.alibaba.nacos.api.remote.RequestCallBack)3 List (java.util.List)3 Payload (com.alibaba.nacos.api.grpc.auto.Payload)2 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 InstanceRequest (com.alibaba.nacos.api.naming.remote.request.InstanceRequest)2 SubscribeServiceRequest (com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest)2 InstanceResponse (com.alibaba.nacos.api.naming.remote.response.InstanceResponse)2 QueryServiceResponse (com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse)2