Search in sources :

Example 1 with TransportAuthenticationException

use of io.transport.core.exception.TransportAuthenticationException in project transporter by wang4ever.

the class AbstractChannelMessageHandler method login.

/**
 * 登录连接认证处理.
 *
 * @param msg
 */
private void login(ConnectMessage msg) {
    // 是浏览器(使用的WebSocket协议连接), 则直接验证token是否有效.
    if (TransportProcessors.isWSChannel(this.client)) {
        String token = String.valueOf(msg.getDeviceInfo().getDeviceId());
        String appId = this.jedisService.get(token);
        if (!StringUtils.equals(appId, msg.getAppId()))
            throw new TransportAuthenticationException("Token authentication failed, token=" + token + ", appId(req)=" + msg.getAppId() + ", appId=" + appId);
    } else // Provider(Android或后台服务端),则使用的自定义协议连接
    {
        String appId = String.valueOf(msg.getAppId());
        String secret = this.jedisService.get(appId);
        if (!StringUtils.equals(secret, msg.getAppSecret()))
            throw new TransportAuthenticationException("AppId authentication failed. appId=" + appId + ", appSecret=" + secret);
    }
}
Also used : TransportAuthenticationException(io.transport.core.exception.TransportAuthenticationException)

Example 2 with TransportAuthenticationException

use of io.transport.core.exception.TransportAuthenticationException in project transporter by wang4ever.

the class TransportMessageHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (logger.isDebugEnabled())
        logger.debug("Read消息: {}", JSON.toJSONString(msg));
    // 1.1 转发给具体业务方法
    if (msg == null)
        logger.warn("On channelRead(msg) is 'msg' null.");
    else {
        SubmitTaskExecutors.getLimitExecutor().submit(() -> {
            // 结果码
            RetCode retCode = RetCode.OK;
            // 处理消息类型
            String type = null;
            // 异常信息
            String err = null;
            try {
                this.dispatch(ctx, msg);
            } catch (Exception e) {
                err = ExceptionUtils.getRootCauseMessage(e).split("\\:")[1];
                // 1.1 认证异常.
                if (e instanceof TransportAuthenticationException) {
                    // 认证失败消息
                    retCode = RetCode.AUTH_FAIL;
                    type = String.valueOf(MsgType.CONNECT_IN.getActionId());
                    logger.warn("认证失败: {}", err);
                } else {
                    // 系统异常结果码
                    retCode = RetCode.SYS_ERR;
                    logger.error("处理失败.{}", err);
                }
            } finally {
                // 1.3 Echo系统错误消息
                ChannelFuture cf = this.echoWrite(ctx, new ResultRespMessage(retCode, type, err));
                if (retCode == RetCode.AUTH_FAIL) {
                    // 认证失败则关闭通道
                    cf.addListener((ChannelFuture f) -> {
                        if (f.isSuccess())
                            // 返回消息完成后再关闭通道
                            super.closeClient();
                    });
                }
            }
        });
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ResultRespMessage(io.transport.core.protocol.message.internal.ResultRespMessage) RetCode(io.transport.core.protocol.message.internal.ResultRespMessage.RetCode) TransportException(io.transport.core.exception.TransportException) TransportAuthenticationException(io.transport.core.exception.TransportAuthenticationException) TransportAuthenticationException(io.transport.core.exception.TransportAuthenticationException)

Aggregations

TransportAuthenticationException (io.transport.core.exception.TransportAuthenticationException)2 ChannelFuture (io.netty.channel.ChannelFuture)1 TransportException (io.transport.core.exception.TransportException)1 ResultRespMessage (io.transport.core.protocol.message.internal.ResultRespMessage)1 RetCode (io.transport.core.protocol.message.internal.ResultRespMessage.RetCode)1