Commit 62ef311c authored by AfirSraftGarrier's avatar AfirSraftGarrier

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	license/src/main/java/iot/sixiang/license/config/CorsConfig.java
parents ed2ae4c9 5af86da3
package iot.sixiang.license.controller; package iot.sixiang.license.controller;
import iot.sixiang.license.jwt.LoginUser;
import iot.sixiang.license.jwt.UserUtils;
import iot.sixiang.license.model.ResResult; import iot.sixiang.license.model.ResResult;
import iot.sixiang.license.model.vo.AlarmVo; import iot.sixiang.license.model.vo.AlarmVo;
import iot.sixiang.license.service.AlarmService; import iot.sixiang.license.service.AlarmService;
...@@ -25,10 +27,11 @@ public class AlarmController { ...@@ -25,10 +27,11 @@ public class AlarmController {
private AlarmService alarmService; private AlarmService alarmService;
@GetMapping("list") @GetMapping("list")
// public ResResult getAlarmList(@RequestParam(value = "userId", defaultValue = "0") int userId) {
public ResResult getAlarmList() { public ResResult getAlarmList() {
int userId = 2147483647; String userId = UserUtils.getLoginUserId();
List<AlarmVo> alarmList = alarmService.getAlarmList(userId); int Id = Integer.valueOf(userId);
List<AlarmVo> alarmList = alarmService.getAlarmList(Id);
return ResResult.success().record(alarmList); return ResResult.success().record(alarmList);
} }
} }
......
...@@ -2,6 +2,7 @@ package iot.sixiang.license.controller; ...@@ -2,6 +2,7 @@ package iot.sixiang.license.controller;
import iot.sixiang.license.jwt.JwtUtil; import iot.sixiang.license.jwt.JwtUtil;
import iot.sixiang.license.jwt.LoginUser; import iot.sixiang.license.jwt.LoginUser;
import iot.sixiang.license.jwt.UserUtils;
import iot.sixiang.license.model.ResResult; import iot.sixiang.license.model.ResResult;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
...@@ -21,11 +22,11 @@ import java.util.Map; ...@@ -21,11 +22,11 @@ import java.util.Map;
public class LoginController { public class LoginController {
//模拟数据库 //模拟数据库
static Map<Integer, LoginUser> userMap = new HashMap<>(); static Map<String, LoginUser> userMap = new HashMap<>();
static { static {
LoginUser user1 = new LoginUser("root", "123456"); LoginUser user1 = new LoginUser("2147483647","root", "123456");
userMap.put(1, user1); userMap.put("2147483647", user1);
} }
/** /**
...@@ -34,13 +35,10 @@ public class LoginController { ...@@ -34,13 +35,10 @@ public class LoginController {
@GetMapping("login") @GetMapping("login")
public ResResult login(@RequestParam("userName") String userName, @RequestParam("password") String password) { public ResResult login(@RequestParam("userName") String userName, @RequestParam("password") String password) {
// @RequestBody JSONObject jsonObject
LoginUser user = new LoginUser();
user.setUserName(userName);
user.setPassword(password);
for (LoginUser dbUser : userMap.values()) { for (LoginUser dbUser : userMap.values()) {
if (dbUser.getUserName().equals(user.getUserName()) && dbUser.getPassword().equals(user.getPassword())) { if (dbUser.getUserName().equals(userName) && dbUser.getPassword().equals(password)) {
log.info("登录成功!生成token!"); log.info("登录成功!生成token!");
String token = JwtUtil.createToken(dbUser); String token = JwtUtil.createToken(dbUser);
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
......
package iot.sixiang.license.controller; package iot.sixiang.license.controller;
import iot.sixiang.license.resource.ResourceMnnager; import iot.sixiang.license.resource.ResourceManager;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -16,13 +16,13 @@ import java.io.IOException; ...@@ -16,13 +16,13 @@ import java.io.IOException;
public class ResourceContrller { public class ResourceContrller {
@Autowired @Autowired
ResourceMnnager resourceMnnager; ResourceManager resourceManager;
@GetMapping("/download") @GetMapping("/download")
public void downloadWorkHourRecordTemplate(HttpServletResponse response, @RequestParam(value = "userId") int userId) { public void downloadWorkHourRecordTemplate(HttpServletResponse response, @RequestParam(value = "userId") int userId) {
try { try {
resourceMnnager.downloadDeviceInfoExcle(response, userId); resourceManager.downloadDeviceInfoExcle(response, userId);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
package iot.sixiang.license.jwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Title: AuthenticationInterceptor
* Description: TODO
*
* @author m33
* @version V1.0
* @date 2022-06-13
*/
@Slf4j
@Component
public class AuthenticationInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
UserUtils.removeUser();
}
}
\ No newline at end of file
...@@ -71,12 +71,16 @@ public class JwtFilter implements Filter { ...@@ -71,12 +71,16 @@ public class JwtFilter implements Filter {
response.getWriter().write(resultStr); response.getWriter().write(resultStr);
return; return;
} }
String userId = userData.get("userId").asString();
String userName = userData.get("userName").asString(); String userName = userData.get("userName").asString();
String password = userData.get("password").asString(); String password = userData.get("password").asString();
//拦截器 拿到用户信息,放到request中 // //拦截器 拿到用户信息,放到request中
request.setAttribute("userName", userName); // request.setAttribute("userName", userName);
request.setAttribute("password", password); // request.setAttribute("password", password);
LoginUser loginUser = new LoginUser(userId, userName, password);
UserUtils.setLoginUser(loginUser);
filterChain.doFilter(request, response); filterChain.doFilter(request, response);
} }
} }
......
...@@ -35,6 +35,7 @@ public class JwtUtil { ...@@ -35,6 +35,7 @@ public class JwtUtil {
String token= JWT.create() String token= JWT.create()
.withHeader(map) //添加头部 .withHeader(map) //添加头部
//可以把数据存在claim中 //可以把数据存在claim中
.withClaim("userId",user.getUserId())
.withClaim("userName",user.getUserName()) .withClaim("userName",user.getUserName())
.withClaim("password",user.getPassword()) .withClaim("password",user.getPassword())
.withExpiresAt(expireDate) //超时设置,设置过期的日期 .withExpiresAt(expireDate) //超时设置,设置过期的日期
......
...@@ -4,14 +4,18 @@ import lombok.Data; ...@@ -4,14 +4,18 @@ import lombok.Data;
@Data @Data
public class LoginUser { public class LoginUser {
private String userId;
private String userName; private String userName;
private String password; private String password;
public LoginUser() { public LoginUser() {
} }
public LoginUser(String userName, String password) { public LoginUser(String userId,String userName, String password) {
this.userId = userId;
this.userName = userName; this.userName = userName;
this.password = password; this.password = password;
} }
} }
package iot.sixiang.license.jwt;
/**
* 存储/获取当前线程的用户信息工具类
*/
public abstract class UserUtils {
//线程变量,存放user实体类信息,即使是静态的与其他线程也是隔离的
private static ThreadLocal<LoginUser> userThreadLocal = new ThreadLocal<LoginUser>();
//从当前线程变量中获取用户信息
public static LoginUser getLoginUser() {
LoginUser user = userThreadLocal.get();
return user;
}
/**
* 获取当前登录用户的ID
* 未登录返回null
*
* @return
*/
public static String getLoginUserId() {
LoginUser user = userThreadLocal.get();
if (user != null && user.getUserId() != null) {
return user.getUserId();
}
return null;
}
//为当前的线程变量赋值上用户信息
public static void setLoginUser(LoginUser user) {
userThreadLocal.set(user);
}
//清除线程变量
public static void removeUser() {
userThreadLocal.remove();
}
}
...@@ -6,7 +6,6 @@ import iot.sixiang.license.service.ResourceService; ...@@ -6,7 +6,6 @@ import iot.sixiang.license.service.ResourceService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -17,7 +16,7 @@ import java.util.List; ...@@ -17,7 +16,7 @@ import java.util.List;
@Component @Component
@Slf4j @Slf4j
public class ResourceMnnager { public class ResourceManager {
@Autowired @Autowired
ResourceService resourceService; ResourceService resourceService;
...@@ -33,68 +32,83 @@ public class ResourceMnnager { ...@@ -33,68 +32,83 @@ public class ResourceMnnager {
// 修改后 // 修改后
String name = new String(filename.getBytes("utf-8"), "iso-8859-1"); String name = new String(filename.getBytes("utf-8"), "iso-8859-1");
response.setHeader("Content-Disposition", "attachment; filename = " + name); response.setHeader("Content-Disposition", "attachment; filename = " + name);
String sheetName = "用户设备信息表"; String sheetName = "用户设备信息表";
//第一步创建workbook //第一步创建workbook
wb = new HSSFWorkbook(); wb = new HSSFWorkbook();
//第二步创建sheet //第二步创建sheet
HSSFSheet sheet = wb.createSheet(); HSSFSheet sheet = wb.createSheet();
wb.setSheetName(0, sheetName);
sheet.setDefaultColumnWidth(20);
//第三步创建行row:添加表头0行 //设置表格格式
//4个参数依次为:开始行,结束行,开始列,结束列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));
HSSFRow row = sheet.createRow(0);
HSSFCell cell1 = row.createCell(0);
cell1.setCellValue("用户设备信息表");
HSSFCellStyle style = wb.createCellStyle(); HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); //居中 style.setAlignment(HorizontalAlignment.CENTER); //居中
HSSFRow row1 = sheet.createRow(1); //第三步创建行row:添加表头0行
HSSFRow row = sheet.createRow((short)0);
//第四步创建单元格 //第四步创建单元格
HSSFCell cell = row1.createCell(0); //第一个单元格 HSSFCell cell = row.createCell((short)0); //第一个单元格
cell.setCellValue("用户"); //设定值 cell.setCellValue("用户"); //设定值
cell.setCellStyle(style); //内容居中 cell.setCellStyle(style); //内容居中
cell = row1.createCell(1); //第二个单元格 cell = row.createCell((short)1); //第二个单元格
cell.setCellValue("密码"); cell.setCellValue("密码");
cell.setCellStyle(style); cell.setCellStyle(style);
cell = row1.createCell(2); //第三个单元格 cell = row.createCell((short)2); //第三个单元格
cell.setCellValue("公司名称"); cell.setCellValue("公司名称");
cell.setCellStyle(style); cell.setCellStyle(style);
cell = row1.createCell(3); //第四个单元格 cell = row.createCell((short)3); //第四个单元格
cell.setCellValue("应用名称"); cell.setCellValue("应用名称");
cell.setCellStyle(style); cell.setCellStyle(style);
cell = row1.createCell(4); //第五个单元格 cell = row.createCell((short)4); //第五个单元格
cell.setCellValue("appKey"); cell.setCellValue("appKey");
cell.setCellStyle(style); cell.setCellStyle(style);
cell = row1.createCell(5); //第六个单元格 cell = row.createCell((short)5); //第六个单元格
cell.setCellValue("SN"); cell.setCellValue("SN");
cell.setCellStyle(style); cell.setCellStyle(style);
//第五步插入数据 //第五步插入数据
List<ResourceVo> resourceList = resourceService.getResource(userId); List<ResourceVo> resourceList = resourceService.getResource(userId);
for (int i = 0; i < resourceList.size(); i++) { for (int i = 0; i < resourceList.size(); i++) {
ResourceVo resourceVo = resourceList.get(i); ResourceVo resourceVo = resourceList.get(i);
//创建行 //创建行
row = sheet.createRow(i + 2); row = sheet.createRow((short)(i + 1));
//创建单元格并且添加数据 //创建单元格并且添加数据
row.createCell(0).setCellValue(resourceVo.getUserName()); cell = row.createCell((short)0); //第一个单元格
row.createCell(1).setCellValue(resourceVo.getPassword()); cell.setCellValue(resourceVo.getUserName());
row.createCell(2).setCellValue(resourceVo.getCompany()); cell.setCellStyle(style);
row.createCell(3).setCellValue(resourceVo.getAppName());
row.createCell(4).setCellValue(resourceVo.getAppKey()); cell = row.createCell((short)1); // 第二个单元格
row.createCell(5).setCellValue(resourceVo.getSn()); cell.setCellValue(resourceVo.getPassword());
cell.setCellStyle(style);
cell = row.createCell((short)2); // 第三个单元格
cell.setCellValue(resourceVo.getCompany());
cell.setCellStyle(style);
cell = row.createCell((short)3); // 第四个单元格
cell.setCellValue(resourceVo.getAppName());
cell.setCellStyle(style);
cell = row.createCell((short)4); // 第五个单元格
cell.setCellValue(resourceVo.getAppKey());
cell.setCellStyle(style);
cell = row.createCell((short)5); // 第六个单元格
cell.setCellValue(resourceVo.getSn());
cell.setCellStyle(style);
} }
sheet.autoSizeColumn((short)0); //调整第一列宽度
sheet.autoSizeColumn((short)1); //调整第二列宽度
sheet.autoSizeColumn((short)2); //调整第三列宽度
sheet.autoSizeColumn((short)3); //调整第四列宽度
sheet.autoSizeColumn((short)4); //调整第五列宽度
sheet.autoSizeColumn((short)5); //调整第六列宽度
wb.write(os); wb.write(os);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
......
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