Commit 9145a8e9 authored by 赵啸非's avatar 赵啸非

设备添加站点编码

parent c19bd4df
......@@ -2,6 +2,7 @@ package com.mortals.xhx.busiz.security.filter;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.service.IAuthTokenService;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.service.IUser;
import com.mortals.framework.web.interceptor.BaseInterceptor;
import com.mortals.xhx.base.framework.annotation.ApiUserAuth;
......@@ -16,6 +17,10 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import static com.mortals.xhx.common.key.ErrorCode.TOKEN_AUTH_FAIL;
import static com.mortals.xhx.common.key.ErrorCode.TOKEN_AUTH_FAIL_CONTENT;
import static com.mortals.xhx.common.key.RedisKey.KEY_TOKEN_API_CACHE;
/**
* api接口权限验证
*
......@@ -27,6 +32,8 @@ public class ApiSubmitInterceptor extends BaseInterceptor {
@Autowired
private IAuthTokenService authTokenService;
@Autowired
private ICacheService cacheService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
......@@ -38,8 +45,8 @@ public class ApiSubmitInterceptor extends BaseInterceptor {
//校验
if (!this.verifyApiUser(request)) {
JSONObject ret = new JSONObject();
ret.put("code", ApiRespCodeEnum.TOKEN_FAILED.getValue());
ret.put("msg", ApiRespCodeEnum.TOKEN_FAILED.getLabel());
ret.put("code", TOKEN_AUTH_FAIL);
ret.put("msg", TOKEN_AUTH_FAIL_CONTENT);
super.writeJsonResponse(response, 200, JSONObject.toJSONString(ret));
return false;
}
......@@ -51,12 +58,16 @@ public class ApiSubmitInterceptor extends BaseInterceptor {
}
public boolean verifyApiUser(HttpServletRequest request) {
IUser apiUser = authTokenService.getLoginUser(request);
if (!ObjectUtils.isEmpty(apiUser) ) {
// authTokenService.verifyToken(apiUser);
return true;
}
String token = authTokenService.getToken(request);
if (cacheService.exists(KEY_TOKEN_API_CACHE + token)) return true;
return false;
// IUser apiUser = authTokenService.getLoginUser(request);
// if (!ObjectUtils.isEmpty(apiUser) ) {
// // authTokenService.verifyToken(apiUser);
// return true;
// }
// return false;
}
}
package com.mortals.xhx.busiz.web;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
......@@ -8,6 +10,7 @@ import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.IAuthTokenService;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.base.framework.annotation.ApiUserAuth;
import com.mortals.xhx.base.login.web.LoginForm;
......@@ -56,6 +59,7 @@ import java.util.stream.Collectors;
import static com.mortals.xhx.common.key.Constant.DES_STR;
import static com.mortals.xhx.common.key.Constant.ENCRYPT_STR;
import static com.mortals.xhx.common.key.ErrorCode.*;
import static com.mortals.xhx.common.key.RedisKey.KEY_TOKEN_API_CACHE;
/**
* 设备
......@@ -99,6 +103,8 @@ public class DeviceApiController {
private FirmService firmService;
@Autowired
private DeviceLogService deviceLogService;
@Autowired
private ICacheService cacheService;
/**
* 设备注册
......@@ -114,9 +120,6 @@ public class DeviceApiController {
DeviceResp deviceResp = new DeviceResp();
try {
DeviceEntity deviceEntity = checkDeviceExistAndCreate(req);
if (deviceEntity.getDeviceStatus() == DeviceStatusEnum.未激活.getValue()) {
throw new AppException(DEVICE_UNACTIVE, DEVICE_UNACTIVE_CONTENT);
}
DeviceQueueAuthInfo authInfo = new DeviceQueueAuthInfo();
authInfo.setHost(host);
authInfo.setPort(port);
......@@ -139,6 +142,10 @@ public class DeviceApiController {
throw new AppException(PLATFORM_IS_EMPTY, PLATFORM_IS_EMPTY_CONTENT);
}
if (deviceEntity.getDeviceStatus() == DeviceStatusEnum.未激活.getValue()) {
throw new AppException(DEVICE_UNACTIVE, DEVICE_UNACTIVE_CONTENT);
}
authInfo.setExchangeName(platformEntity.getPlatformSn() + Constant.EXCHANGE_SPLIT + productEntity.getProductCode());
authInfo.setUploadTopicFilter(Constant.UPLOAD_TOPIC + deviceEntity.getDeviceCode());
authInfo.setDownTopicFilter(Constant.DOWN_TOPIC + deviceEntity.getDeviceCode());
......@@ -146,7 +153,7 @@ public class DeviceApiController {
RegisterResp registerResp = new RegisterResp();
registerResp.setRabbmitInfo(authInfo);
ServerInfo serverInfo = new ServerInfo();
serverInfo.setHomeUrl(productEntity.getHomeUrl()==null?"":productEntity.getHomeUrl());
serverInfo.setHomeUrl(productEntity.getHomeUrl() == null ? "" : productEntity.getHomeUrl());
serverInfo.setServerUrl(platformEntity.getSendUrl());
registerResp.setServiceInfo(serverInfo);
DeviceInfo deviceInfo = new DeviceInfo();
......@@ -193,10 +200,15 @@ public class DeviceApiController {
try {
UserEntity userEntity = userService.doLogin(loginForm.getLoginName(), loginForm.getPassword(), "");
if (!ObjectUtils.isEmpty(userEntity)) {
this.authTokenService.setUser(userEntity);
userEntity.setExpireTime(DateUtils.addCurrDate(7).getTime());
userEntity.setToken(IdUtil.fastSimpleUUID());
String token = authTokenService.createToken(userEntity);
// this.authTokenService.setUser(userEntity);
// userEntity.setExpireTime(DateUtils.addCurrDate(7).getTime());
// userEntity.setToken(IdUtil.fastSimpleUUID());
// String token = authTokenService.createToken(userEntity);
String token = IdUtil.fastSimpleUUID();
//long expireTime = DateUtil.between(DateUtil.beginOfWeek(new Date()), DateUtil.endOfWeek(new Date()), DateUnit.SECOND);
cacheService.setnx(KEY_TOKEN_API_CACHE+token,"",604800);
rsp.setData(token);
}
} catch (AppException e) {
......@@ -219,7 +231,7 @@ public class DeviceApiController {
*
* @return
*/
@GetMapping("deviceInit")
@RequestMapping(value = "deviceInit", method = {RequestMethod.POST, RequestMethod.GET})
public String deviceInit() {
ApiResp<DeviceInitResp> rsp = new ApiResp<>();
rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
......@@ -292,7 +304,6 @@ public class DeviceApiController {
if (ObjectUtils.isEmpty(req.getDeviceCode())) {
throw new AppException(DEVICE_CODE_IS_EMPTY, DEVICE_CODE_IS_EMPTY_CONTENT);
}
if (ObjectUtils.isEmpty(req.getSiteId())) {
throw new AppException(SITEID_IS_EMPTY, SITEID_IS_EMPTY_CONTENT);
}
......@@ -467,9 +478,9 @@ public class DeviceApiController {
TbQueueMsgHeaders header = new DefaultTbQueueMsgHeaders();
header.put(MessageHeader.MESSAGETYPE, Constant.MESSAGETYPE_HEARTBEAT);
header.put(MessageHeader.DEVICECODE, deviceEntity.getDeviceCode());
for (int i = 0; i < 10; i++) {
for (int i = 0; i < 100; i++) {
deviceService.sendDeviceMessage(deviceEntity, info, header, JSON.toJSONString(req), null);
// Thread.sleep(50);
Thread.sleep(50);
}
//log.info(String.format("sendMsgResp:%s", JSON.toJSONString(sendDeviceMessageResp)));
}
......
......@@ -108,4 +108,7 @@ public interface ErrorCode {
public static final int DEVICE_NOT_EXIST = 1009;
public static final String DEVICE_NOT_EXIST_CONTENT = "当前设备不存在!";
public static final int TOKEN_AUTH_FAIL = 1010;
public static final String TOKEN_AUTH_FAIL_CONTENT = "token认证失败!";
}
......@@ -21,4 +21,6 @@ public class RedisKey {
public static final String KEY_PLATFORM_CACHE = "platformDict";
public static final String KEY_PRODUCT_CACHE = "productDict";
public static final String KEY_TOKEN_API_CACHE = "token:api:";
}
......@@ -164,7 +164,7 @@ public class DeviceMsgComsumerStartedService implements IApplicationStartedServi
boolean bool = false;
DeviceEntity deviceEntity = deviceService.getExtCache(deviceCode);
if (!ObjectUtils.isEmpty(deviceEntity)) {
cacheService.setnx(RedisKey.KEY_DEVICE_ONLINE_CACHE + deviceEntity.getDeviceCode(), "", GlobalSysInfo.getParamIntValue(Constant.HEARTBEAT_TIMEOUT, 180));
cacheService.setnx(RedisKey.KEY_DEVICE_ONLINE_CACHE + deviceEntity.getDeviceCode(), "", GlobalSysInfo.getParamIntValue(Constant.HEARTBEAT_TIMEOUT, 120));
if (deviceEntity.getDeviceStatus() == DeviceStatusEnum.离线.getValue()) {
bool = true;
}
......
......@@ -118,9 +118,9 @@ Authorization: {{authToken}}
Content-Type: application/json
{
"receiveMethod": 3,
"receiveMethod": 1,
"deviceName": "测试接口创建设备3",
"deviceCode": "a12345678",
"deviceCode": "a1234567899",
"siteId": 123,
"siteCode": "adfasfdasfdasf",
"siteName": "测试站点",
......
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