All Downloads are FREE. Search and download functionalities are using the official Maven repository.

link.jfire.socket.socketserver.handler.impl.AuthHandler Maven / Gradle / Ivy

Go to download

Jfire - socket is a server-side framework based on AIO. Users only need a simple implementation of a business logic processing interface can be the business data processing. The framework provides the client and server at the same time. Have strong connection capacity. Single server provides tens of thousands of connections.

The newest version!
package link.jfire.socket.socketserver.handler.impl;

import java.nio.charset.Charset;
import javax.annotation.Resource;
import link.jfire.baseutil.collection.ByteCache;
import link.jfire.baseutil.encrypt.RSAUtil;
import link.jfire.baseutil.simplelog.ConsoleLogFactory;
import link.jfire.baseutil.simplelog.Logger;
import link.jfire.socket.socketserver.bus.BusinessCommand;
import link.jfire.socket.socketserver.bus.Message;
import link.jfire.socket.socketserver.handler.MessageHandler;

@Resource
public class AuthHandler implements MessageHandler
{
    private Charset charset = Charset.forName("utf8");
    private byte[]  privateKey;
    private Logger  logger  = ConsoleLogFactory.getLogger();
    
    @Override
    public byte interestedDataPacketType()
    {
        return BusinessCommand.AUTH;
    }
    
    @Override
    public void handler(Message message)
    {
        logger.trace("收到验证消息,开始进行验证");
        ByteCache data = message.getBusinessData();
        RSAUtil rsaUtil = new RSAUtil();
        rsaUtil.setPrivateKey(privateKey);
        // 在正确的情况下,客户端应该发送jfire+当前时间毫秒数的字符串
        byte[] authMsgBytes = rsaUtil.decrypt(data.toArray());
        String authMsg = new String(authMsgBytes, charset);
        long clientTime = Long.valueOf(authMsg.substring(5));
        long result = System.currentTimeMillis() - clientTime;
        if (result >= -220000 && result <= 220000)
        {
            data.clear().putArray(rsaUtil.sign(authMsgBytes));
            logger.debug("客户端{}验证成功", message.getChannelInfo().getAddress());
        }
        else
        {
            message.getChannelInfo().close();
            logger.debug("客户端{}验证失败", message.getChannelInfo().getAddress());
        }
    }
    
    public void setPrivateKey(byte[] privateKey)
    {
        this.privateKey = privateKey;
        RSAUtil rsaUtil = new RSAUtil();
        rsaUtil.setPrivateKey(privateKey);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy