mirror of
https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network.git
synced 2026-02-04 15:05:54 +08:00
【fix】proxy代理支持令牌验证
This commit is contained in:
@@ -2,6 +2,7 @@ package org.framework.lazy.cloud.network.heartbeat.protocol.auth;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.protocol.properties.ProtocolProxyProperties;
|
||||
import org.framework.lazy.cloud.network.heartbeat.protocol.token.AuthenticationTokenContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -11,29 +12,17 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
@Component
|
||||
public class DefaultNettyPasswordAuth extends AbstractNettyPasswordAuth {
|
||||
|
||||
protected Map<String/*username*/, String/*password*/> authMap = new ConcurrentHashMap<>();
|
||||
|
||||
protected DefaultNettyPasswordAuth(ProtocolProxyProperties protocolProxyProperties) {
|
||||
super(protocolProxyProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doVerify(String username, String password) {
|
||||
boolean verify = authMap.containsKey(username) && authMap.get(username).equals(password);
|
||||
if(!verify){
|
||||
boolean verify = AuthenticationTokenContext.verify(username, password);
|
||||
if (!verify) {
|
||||
log.error("授权失败");
|
||||
}
|
||||
return verify;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增授权
|
||||
*
|
||||
* @param username 用户
|
||||
* @param password 密码
|
||||
*/
|
||||
public DefaultNettyPasswordAuth addAuth(String username, String password) {
|
||||
authMap.putIfAbsent(username, password);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.protocol.token;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 授权信息
|
||||
*/
|
||||
@Data
|
||||
public class AuthenticationToken {
|
||||
|
||||
/**
|
||||
* 令牌key
|
||||
*/
|
||||
@Schema(description = "令牌key", name = "appKey", example = "")
|
||||
private String appKey;
|
||||
|
||||
/**
|
||||
* 令牌密钥
|
||||
*/
|
||||
@Schema(description = "令牌密钥", name = "appSecret", example = "")
|
||||
private String appSecret;
|
||||
|
||||
|
||||
/**
|
||||
* 过期时间
|
||||
*/
|
||||
@Schema(description = "过期时间", name = "expireInTime", example = "")
|
||||
private LocalDateTime expireInTime;
|
||||
|
||||
/**
|
||||
* 被使用的客户端ID
|
||||
*/
|
||||
@Schema(description = "被使用的客户端ID", name = "usedByClientId", example = "")
|
||||
private String usedByClientId;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.protocol.token;
|
||||
|
||||
import org.wu.framework.core.utils.ObjectUtils;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class AuthenticationTokenContext {
|
||||
|
||||
|
||||
// key appid value AuthenticationToken
|
||||
private static final ConcurrentHashMap<String, AuthenticationToken> m = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
/**
|
||||
* 设置授权信息
|
||||
*
|
||||
* @param clientId 客户端ID
|
||||
* @param appKey 令牌
|
||||
* @param appSecret 迷药
|
||||
*/
|
||||
public static void setAuthenticationToken(String clientId, String appKey, String appSecret) {
|
||||
AuthenticationToken authenticationToken = new AuthenticationToken();
|
||||
authenticationToken.setUsedByClientId(clientId);
|
||||
authenticationToken.setAppKey(appKey);
|
||||
authenticationToken.setAppSecret(appSecret);
|
||||
AuthenticationTokenContext.setAuthenticationToken(authenticationToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置授权信息
|
||||
*
|
||||
* @param authenticationToken 授权信息
|
||||
*/
|
||||
public static void setAuthenticationToken(AuthenticationToken authenticationToken) {
|
||||
String key = authenticationToken.getAppKey();
|
||||
if (m.containsKey(key)) {
|
||||
m.put(key, authenticationToken);
|
||||
return;
|
||||
}
|
||||
m.put(key, authenticationToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取授权信息
|
||||
*
|
||||
* @param appKey 令牌key
|
||||
*/
|
||||
public static AuthenticationToken getAuthenticationToken(String appKey) {
|
||||
AuthenticationToken p = m.values()
|
||||
.stream()
|
||||
.filter(authenticationToken -> authenticationToken.getAppKey().equals(appKey))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除授权信息
|
||||
*
|
||||
* @param appKey 令牌key
|
||||
*/
|
||||
public static void removeAuthenticationToken(String appKey) {
|
||||
m.remove(appKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证令牌
|
||||
*
|
||||
* @param appKey 令牌key
|
||||
*/
|
||||
public static Boolean verify(String appKey, String appSecret) {
|
||||
AuthenticationToken p = m.values()
|
||||
.stream()
|
||||
.filter(authenticationToken -> authenticationToken.getAppKey().equals(appKey)
|
||||
&& authenticationToken.getAppSecret().equals(appSecret)
|
||||
)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
return ObjectUtils.isNotEmpty(p);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user