Commit 26852d39 authored by zengtianlai3's avatar zengtianlai3

修改鉴权

parent e2312047
package iot.sixiang.license.auth; package iot.sixiang.license.auth;
import iot.sixiang.license.entity.Server; import iot.sixiang.license.entity.License;
import iot.sixiang.license.service.ServerService; import iot.sixiang.license.entity.User;
import iot.sixiang.license.service.LicenseService;
import iot.sixiang.license.service.UserService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.util.*; import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component @Component
@Slf4j @Slf4j
public class AuthManager { public class AuthManager {
@Autowired @Autowired
private ServerService serverService; private UserService userService;
@Autowired
private LicenseService licenseService;
private Map<String, Server> allServers = null; private Map<String, User> allUsers = null;
private Map<String, License> allLicense = null;
public AuthManager() { public AuthManager() {
allServers = new HashMap<String, Server>(); allUsers = new HashMap<String, User>();
allLicense = new HashMap<String, License>();
} }
@PostConstruct @PostConstruct
public void init() { public void init() {
List<Server> servers = serverService.getServerList(0, 20); initUsers();
for (Server server : servers) { initLicense();
String serverIp = server.getServerIp();
allServers.put(serverIp, server);
} }
private void initUsers() {
List<User> users = userService.getUserList(0, 20);
for (User user : users) {
String userId = user.getUserId();
allUsers.put(userId, user);
} }
public Server getBalanceServer(){
int count = allServers.size();
if(count==0){
return null;
}else{
Random random = new Random();
int index = random.nextInt(count);
List<Server> servers = new ArrayList<>(allServers.values());
return servers.get(index);
} }
private void initLicense() {
List<License> licenses = licenseService.getLicenseList(0, 20);
for (License license : licenses) {
String appId = license.getAppId();
allLicense.put(appId, license);
}
} }
public boolean auth(String userId, String password, String appId, String appKey) {
if (!allUsers.containsKey(userId)) {
return false;
} else {
User user = allUsers.get(userId);
if (user.getPassword() != password) {
return false;
}
}
if (!allLicense.containsKey(appId)) {
return false;
} else {
License license = allLicense.get(appId);
if (license.getAppKey() != appKey) {
return false;
}
}
return true;
}
} }
package iot.sixiang.license.device; package iot.sixiang.license.device;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import iot.sixiang.license.auth.AuthManager;
import iot.sixiang.license.consts.Consts; import iot.sixiang.license.consts.Consts;
import iot.sixiang.license.event.CreateForwarClientEvent; import iot.sixiang.license.event.CreateForwarClientEvent;
import iot.sixiang.license.event.DeviceClientInactiveEvent; import iot.sixiang.license.event.DeviceClientInactiveEvent;
...@@ -51,8 +54,8 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -51,8 +54,8 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
*/ */
byte cmd = protocol.getCmd(); byte cmd = protocol.getCmd();
int cmdInt = cmd & 0xFF; int cmdInt = cmd & 0xFF;
log.info("real cmd:"+cmdInt); log.info("real cmd:" + cmdInt);
log.info("收到的消息:"+HexUtil.bytes2hex(protocol.getContent())); log.info("收到的消息:" + HexUtil.bytes2hex(protocol.getContent()));
boolean license = false; boolean license = false;
// cmdInt = Consts.CMD_LICENSE; // cmdInt = Consts.CMD_LICENSE;
...@@ -153,13 +156,24 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -153,13 +156,24 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
private boolean handlerLicense(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) { private boolean handlerLicense(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) {
String jsonLicense = new String(protocol.getContent(), 0, protocol.getContent().length); String jsonLicense = new String(protocol.getContent(), 0, protocol.getContent().length);
JSONObject jsonObject = JSON.parseObject(jsonLicense);
String appId = "123456"; String userId = jsonObject.getString("userId");
String appKey = "123456"; String password = jsonObject.getString("password");
String appId = jsonObject.getString("appId");
String appKey = jsonObject.getString("appKey");
// String userId = "12345";
// String password = "1234";
// String appId = "12222";
// String appKey = "1234455";
String token = "123456"; String token = "123456";
String channelId = channel.id().asLongText();
boolean license = true;
AuthManager authManager = SpringUtil.getBean(AuthManager.class);
boolean license = authManager.auth(userId, password, appId, appKey);
String channelId = channel.id().asLongText();
if (license) { if (license) {
SessionContext session = new SessionContext(); SessionContext session = new SessionContext();
session.setRemoteIp(remoteIp); session.setRemoteIp(remoteIp);
......
package iot.sixiang.license.event; package iot.sixiang.license.event;
import iot.sixiang.license.auth.AuthManager; import iot.sixiang.license.balance.BalanceManager;
import iot.sixiang.license.entity.Server; import iot.sixiang.license.entity.Server;
import iot.sixiang.license.forward.ForwardManager; import iot.sixiang.license.forward.ForwardManager;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -18,7 +18,7 @@ public class CreateForwarClientEventHandler { ...@@ -18,7 +18,7 @@ public class CreateForwarClientEventHandler {
@Autowired @Autowired
EventPublisher eventPublisher; EventPublisher eventPublisher;
@Autowired @Autowired
AuthManager authManager; BalanceManager balanceManager;
public CreateForwarClientEventHandler() { public CreateForwarClientEventHandler() {
...@@ -30,7 +30,7 @@ public class CreateForwarClientEventHandler { ...@@ -30,7 +30,7 @@ public class CreateForwarClientEventHandler {
String appId = event.getAppId(); String appId = event.getAppId();
Server balanceServer = authManager.getBalanceServer(); Server balanceServer = balanceManager.getBalanceServer();
if (balanceServer != null) { if (balanceServer != null) {
String serverIp = balanceServer.getServerIp(); String serverIp = balanceServer.getServerIp();
Integer port = balanceServer.getPort(); Integer port = balanceServer.getPort();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment