mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-04-09 09:37:32 +00:00
feat:ws握手同时认证
This commit is contained in:
@@ -26,7 +26,7 @@ public class JwtUtils {
|
|||||||
/**
|
/**
|
||||||
* token秘钥,请勿泄露,请勿随便修改
|
* token秘钥,请勿泄露,请勿随便修改
|
||||||
*/
|
*/
|
||||||
@Value("jwt.secret")
|
@Value("${mallchat.jwt.secret}")
|
||||||
private String secret;
|
private String secret;
|
||||||
|
|
||||||
private static final String UID_CLAIM = "uid";
|
private static final String UID_CLAIM = "uid";
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package com.abin.mallchat.common.common.algorithm.ac;
|
||||||
|
|
||||||
|
|
||||||
|
import com.auth0.jwt.JWT;
|
||||||
|
import com.auth0.jwt.algorithms.Algorithm;
|
||||||
|
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||||
|
import com.auth0.jwt.interfaces.JWTVerifier;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class CreateTokenTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void create(){
|
||||||
|
String token = JWT.create()
|
||||||
|
.withClaim("uid", 123L) // 只存一个uid信息,其他的自己去redis查
|
||||||
|
.withClaim("createTime", new Date())
|
||||||
|
.sign(Algorithm.HMAC256("dsfsdfsdfsdfsd")); // signature
|
||||||
|
log.info("生成的token为 {}",token);
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
JWTVerifier verifier = JWT.require(Algorithm.HMAC256("dsfsdfsdfsdfsd")).build();
|
||||||
|
DecodedJWT jwt = verifier.verify(token);
|
||||||
|
log.info(jwt.getClaims().toString());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("decode error,token:{}", token, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void verifyToken(){
|
||||||
|
String token = JWT.create()
|
||||||
|
.withClaim("uid", 1) // 只存一个uid信息,其他的自己去redis查
|
||||||
|
.withClaim("createTime", new Date())
|
||||||
|
.sign(Algorithm.HMAC256("dsfsdfsdfsdfsd")); // signature
|
||||||
|
log.info("生成的token为{}",token);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,6 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
public class HttpHeadersHandler extends ChannelInboundHandlerAdapter {
|
public class HttpHeadersHandler extends ChannelInboundHandlerAdapter {
|
||||||
private AttributeKey<String> key = AttributeKey.valueOf("Id");
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
@@ -22,6 +21,8 @@ public class HttpHeadersHandler extends ChannelInboundHandlerAdapter {
|
|||||||
ip = address.getAddress().getHostAddress();
|
ip = address.getAddress().getHostAddress();
|
||||||
}
|
}
|
||||||
NettyUtil.setAttr(ctx.channel(), NettyUtil.IP, ip);
|
NettyUtil.setAttr(ctx.channel(), NettyUtil.IP, ip);
|
||||||
|
String token = headers.get("token");
|
||||||
|
NettyUtil.setAttr(ctx.channel(), NettyUtil.TOKEN, token);
|
||||||
}
|
}
|
||||||
ctx.fireChannelRead(msg);
|
ctx.fireChannelRead(msg);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import io.netty.util.AttributeKey;
|
|||||||
|
|
||||||
public class NettyUtil {
|
public class NettyUtil {
|
||||||
|
|
||||||
|
public static AttributeKey<String> TOKEN = AttributeKey.valueOf("token");
|
||||||
public static AttributeKey<String> IP = AttributeKey.valueOf("ip");
|
public static AttributeKey<String> IP = AttributeKey.valueOf("ip");
|
||||||
public static AttributeKey<Long> UID = AttributeKey.valueOf("uid");
|
public static AttributeKey<Long> UID = AttributeKey.valueOf("uid");
|
||||||
|
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler<Tex
|
|||||||
}
|
}
|
||||||
} else if (evt == WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) {
|
} else if (evt == WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) {
|
||||||
getService().connect(ctx.channel());
|
getService().connect(ctx.channel());
|
||||||
|
getService().authorize(ctx.channel(), new WSAuthorize(NettyUtil.getAttr(ctx.channel(), NettyUtil.TOKEN)));
|
||||||
}
|
}
|
||||||
super.userEventTriggered(ctx, evt);
|
super.userEventTriggered(ctx, evt);
|
||||||
}
|
}
|
||||||
@@ -92,10 +93,7 @@ public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler<Tex
|
|||||||
log.info("请求二维码 = " + msg.text());
|
log.info("请求二维码 = " + msg.text());
|
||||||
break;
|
break;
|
||||||
case HEARTBEAT:
|
case HEARTBEAT:
|
||||||
break;
|
log.info("心跳");
|
||||||
case AUTHORIZE:
|
|
||||||
getService().authorize(ctx.channel(), JSONUtil.toBean(wsBaseReq.getData(), WSAuthorize.class));
|
|
||||||
log.info("主动认证 = " + msg.text());
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
log.info("未知类型");
|
log.info("未知类型");
|
||||||
|
|||||||
Reference in New Issue
Block a user