Commit 74c76e21 authored by 马33's avatar 马33

新增设备获取TOKEN【接口】

parent 9d8be15d
......@@ -64,6 +64,33 @@ public class AuthManager {
}
}
// 终端设备鉴权
public boolean authTerminalDevice(String appId, String sn, String sign) {
if (!allApply.containsKey(appId)) {
log.info("no valid appId...");
return false;
}
if (!deviceManager.getContainSn(sn)) {
log.info("no this sn...");
return false;
}
DeviceVo device = deviceManager.getDevice(sn);
int deviceId = device.getDeviceId();
if (deviceBlackMap.containsKey(deviceId)) {
log.info("in black...");
return false;
}
Apply apply = allApply.get(appId);
String appKey = apply.getAppKey();
String input = "app_id=" + appId + "&sn=" + sn;
String valSHA1 = HmacUtil.encrypt(input, appKey, HmacUtil.HMAC_SHA1).toUpperCase();
if (CommonUtil.toUpperCaseByEnglish(sign).equals(CommonUtil.toUpperCaseByEnglish(valSHA1))) {
return true;
} else {
log.info("sign no valid:" + input);
return false;
}
}
public boolean auth(String appId, String sn, String sign) {
if (!allApply.containsKey(appId)) {
......
package iot.sixiang.license.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import iot.sixiang.license.model.ResResult;
import iot.sixiang.license.model.dto.GetTerminalDeviceTokenDTO;
import iot.sixiang.license.service.TerminalDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/iot_license/terminal_device")
@Api(value = "终端设备模块", tags = {"终端设备模块"})
public class TerminalDeviceController {
@Autowired
TerminalDeviceService terminalDeviceService;
@GetMapping("/get_token")
@ApiOperation(value = "终端设备获取token", notes = "终端设备获取token")
public ResResult getToken(GetTerminalDeviceTokenDTO getTerminalDeviceTokenDTO) {
return terminalDeviceService.getToken(getTerminalDeviceTokenDTO);
}
}
......@@ -27,6 +27,7 @@ public class JwtFilter implements Filter {
private static final String url7 = "/swagger-resources";
private static final String url8 = "/webjars/";
private static final String url9 = "/log/get";
private static final String url10 = "/terminal_device/get_token";
@Override
public void init(FilterConfig filterConfig) {
......@@ -48,7 +49,7 @@ public class JwtFilter implements Filter {
boolean check = true;
String uri = request.getRequestURI();
if (uri.contains(url1) || uri.contains(url2) || uri.contains(url3) || uri.contains(url4) || uri.contains(url7) || uri.contains(url8) || uri.contains(url9)) {
if (uri.contains(url1) || uri.contains(url2) || uri.contains(url3) || uri.contains(url4) || uri.contains(url7) || uri.contains(url8) || uri.contains(url9) || uri.contains(url10)) {
if (uri.contains(url1) || uri.contains(url2)) {
UserUtils.setUri(uri);
}
......
......@@ -69,7 +69,7 @@ public class ResResult<T> {
* @return
*/
public static ResResult failed() {
return new ResResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg());
return new ResResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMsg());
}
......
package iot.sixiang.license.model.dto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class GetTerminalDeviceTokenDTO {
@ApiModelProperty("应用id")
private String appId;
@ApiModelProperty("设备编号")
private String sn;
@ApiModelProperty("签名")
private String sign;
}
package iot.sixiang.license.service;
import iot.sixiang.license.model.ResResult;
import iot.sixiang.license.model.dto.GetTerminalDeviceTokenDTO;
import iot.sixiang.license.model.vo.GetTerminalDeviceTokenVO;
public interface TerminalDeviceService {
ResResult getToken(GetTerminalDeviceTokenDTO getTerminalDeviceTokenDTO);
}
package iot.sixiang.license.service.impl;
import iot.sixiang.license.auth.AuthManager;
import iot.sixiang.license.device.DeviceManager;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.jwt.JwtUtil;
import iot.sixiang.license.jwt.LoginUser;
import iot.sixiang.license.model.ResResult;
import iot.sixiang.license.model.dto.GetTerminalDeviceTokenDTO;
import iot.sixiang.license.service.TerminalDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class TerminalDeviceServiceImpl implements TerminalDeviceService {
@Autowired
private AuthManager authManager;
@Autowired
private DeviceManager deviceManager;
@Override
public ResResult getToken(GetTerminalDeviceTokenDTO getTerminalDeviceTokenDTO) {
String appId = getTerminalDeviceTokenDTO.getAppId();
String sn = getTerminalDeviceTokenDTO.getSn();
String sign = getTerminalDeviceTokenDTO.getSign();
if (StringUtils.isEmpty(appId)) {
throw new IotLicenseException(403, "应用id不能为空");
}
if (StringUtils.isEmpty(sn)) {
throw new IotLicenseException(403, "终端编号不能为空");
}
if (StringUtils.isEmpty(sign)) {
throw new IotLicenseException(403, "签名不能为空");
}
boolean authResult = authManager.authTerminalDevice(appId, sn, sign);
if (authResult) {
LoginUser user = new LoginUser();
user.setUserId(appId);
user.setUserName(sn);
String token = JwtUtil.createToken(user);
return ResResult.success().record(token);
} else {
return ResResult.validate_failed();
}
}
}
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