System Overview
With the advancement of internet technology, it has become an integral part of various societal sectors, enabling efficient information dissemination and management through digital platforms. In addresing challenges such as disorganized health data, high error rates, poor security, and labor-intensive processes in employee health management, a daily health declaration system offers a structured solution.
This system, developed in Eclipse using Java and MySQL for data storage, facilitates the management of employee health records. It allows administrators to oversee employee registration, manage provinces and cities, and handle health records including updates, queries, and deletions. The system provides secure, scalable, and cost-effective solutions for health information management, reducing manual effort and improving operational efficiency.
Technology Stack
Source code available at the end of this document
Programming Language: Java
Framework: SSM (Spring + Spring MVC + MyBatis)
Frontend: Vue/HTML
Persistence Layer: MyBatis
Server: Tomcat 7/8/9/10
Database: MySQL 5.7+
Build Tool: Maven
IDE: IDEA/Eclipse
System Functionality
Administrator Features
Employee Management
Administrators can perform CRUD operations on employee profiles. This includes adding new employees, updating personal details like ID, phone, email, and profile pictures, and deleting employee records.
Health Record Management
The system enables administrators to view, modify, and remove employee health records. These records include province, city, address, and temperature data.
Province Management
Administrators can add, edit, or delete province entries in the system.
City Management
City entries can be managed similarly, with options to create, view, update, or delete city information.
Password Update
Administrators have the ability to change their login credentials, which requires re-authentication after the change.
Employee Features
Registration
Employees can register for accounts by providing basic details such as name, email, password, and employee ID.
Submit Health Information
Employees can report daily health status, including body temperature, condition, location details, and residential address.
Health Record Management
Employees can access, update, and review their own health records.
Code Implementation
CORS Handling
package com.interceptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import com.annotation.IgnoreAuth;
import com.entity.EIException;
import com.entity.TokenEntity;
import com.service.TokenService;
import com.utils.R;
/**
* Authentication Token Validation
*/
@Component
public class AuthInterceptor implements HandlerInterceptor {
public static final String AUTH_HEADER = "Token";
@Autowired
private TokenService tokenService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// Enable Cross-Origin Resource Sharing
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,request-source,Token, Origin,imgType, Content-Type, cache-control,postman-token,Cookie, Accept,authorization");
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
IgnoreAuth annotation;
if (handler instanceof HandlerMethod) {
annotation = ((HandlerMethod) handler).getMethodAnnotation(IgnoreAuth.class);
} else {
return true;
}
// Retrieve token from header
String token = request.getHeader(AUTH_HEADER);
/**
* Skip validation for methods marked with IgnoreAuth
*/
if(annotation!=null) {
return true;
}
TokenEntity tokenEntity = null;
if(StringUtils.isNotBlank(token)) {
tokenEntity = tokenService.getTokenEntity(token);
}
if(tokenEntity != null) {
request.getSession().setAttribute("userId", tokenEntity.getUserid());
request.getSession().setAttribute("role", tokenEntity.getRole());
request.getSession().setAttribute("tableName", tokenEntity.getTablename());
request.getSession().setAttribute("username", tokenEntity.getUsername());
return true;
}
PrintWriter writer = null;
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
try {
writer = response.getWriter();
writer.print(JSONObject.toJSONString(R.error(401, "Please log in first")));
} finally {
if(writer != null){
writer.close();
}
}
return false;
}
}
Login Implementation
package com.controller;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.annotation.IgnoreAuth;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.entity.TokenEntity;
import com.entity.UserEntity;
import com.service.TokenService;
import com.service.UserService;
import com.utils.CommonUtil;
import com.utils.MD5Util;
import com.utils.MPUtil;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.ValidatorUtils;
/**
* Authentication Controller
*/
@RequestMapping("users")
@RestController
public class UserController{
@Autowired
private UserService userService;
@Autowired
private TokenService tokenService;
/**
* User Login
*/
@IgnoreAuth
@PostMapping(value = "/login")
public R login(String username, String password, String captcha, HttpServletRequest request) {
UserEntity user = userService.selectOne(new EntityWrapper().eq("username", username));
if(user==null || !user.getPassword().equals(password)) {
return R.error("Invalid username or password");
}
String token = tokenService.generateToken(user.getId(),username, "users", user.getRole());
return R.ok().put("token", token);
}
/**
* User Registration
*/
@IgnoreAuth
@PostMapping(value = "/register")
public R register(@RequestBody UserEntity user){
if(userService.selectOne(new EntityWrapper().eq("username", user.getUsername())) !=null) {
return R.error("User already exists");
}
userService.insert(user);
return R.ok();
}
/**
* Logout
*/
@GetMapping(value = "logout")
public R logout(HttpServletRequest request) {
request.getSession().invalidate();
return R.ok("Logout successful");
}
/**
* Reset Password
*/
@IgnoreAuth
@RequestMapping(value = "/resetPass")
public R resetPass(String username, HttpServletRequest request){
UserEntity user = userService.selectOne(new EntityWrapper().eq("username", username));
if(user==null) {
return R.error("Account does not exist");
}
user.setPassword("123456");
userService.update(user,null);
return R.ok("Password reset to: 123456");
}
/**
* User List Pagination
*/
@RequestMapping("/page")
public R page(@RequestParam Map params,UserEntity user){
EntityWrapper ew = new EntityWrapper();
PageUtils page = userService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.allLike(ew, user), params), params));
return R.ok().put("data", page);
}
/**
* User List
*/
@RequestMapping("/list")
public R list( UserEntity user){
EntityWrapper ew = new EntityWrapper();
ew.allEq(MPUtil.allEQMapPre( user, "user"));
return R.ok().put("data", userService.selectListView(ew));
}
/**
* Get User Info
*/
@RequestMapping("/info/{id}")
public R info(@PathVariable("id") String id){
UserEntity user = userService.selectById(id);
return R.ok().put("data", user);
}
/**
* Get Current Session User
*/
@RequestMapping("/session")
public R getCurrUser(HttpServletRequest request){
Long id = (Long)request.getSession().getAttribute("userId");
UserEntity user = userService.selectById(id);
return R.ok().put("data", user);
}
/**
* Save New User
*/
@PostMapping("/save")
public R save(@RequestBody UserEntity user){
if(userService.selectOne(new EntityWrapper().eq("username", user.getUsername())) !=null) {
return R.error("User already exists");
}
userService.insert(user);
return R.ok();
}
/**
* Update User Profile
*/
@RequestMapping("/update")
public R update(@RequestBody UserEntity user){
UserEntity u = userService.selectOne(new EntityWrapper().eq("username", user.getUsername()));
if(u!=null && u.getId()!=user.getId() && u.getUsername().equals(user.getUsername())) {
return R.error("Username already taken.");
}
userService.updateById(user);
return R.ok();
}
/**
* Delete Users
*/
@RequestMapping("/delete")
public R delete(@RequestBody Long[] ids){
userService.deleteBatchIds(Arrays.asList(ids));
return R.ok();
}
}