外卖项目day01-新增员工_代码完善-动态获取登录操作者(threadLocal)
This commit is contained in:
@@ -81,6 +81,8 @@ public class EmployeeController {
|
|||||||
@PostMapping()
|
@PostMapping()
|
||||||
public Result<String> save(@RequestBody EmployeeDTO employeeDTO) {
|
public Result<String> save(@RequestBody EmployeeDTO employeeDTO) {
|
||||||
log.info("新增员工:{}",employeeDTO);
|
log.info("新增员工:{}",employeeDTO);
|
||||||
|
//获取请求的线程ID
|
||||||
|
log.error("当前控制器中的线程id:{}",Thread.currentThread().getId());
|
||||||
//调用业务层新增员工的功能
|
//调用业务层新增员工的功能
|
||||||
employeeService.save(employeeDTO);
|
employeeService.save(employeeDTO);
|
||||||
return Result.success();
|
return Result.success();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.sky.interceptor;
|
package com.sky.interceptor;
|
||||||
|
|
||||||
import com.sky.constant.JwtClaimsConstant;
|
import com.sky.constant.JwtClaimsConstant;
|
||||||
|
import com.sky.context.BaseContext;
|
||||||
import com.sky.properties.JwtProperties;
|
import com.sky.properties.JwtProperties;
|
||||||
import com.sky.utils.JwtUtil;
|
import com.sky.utils.JwtUtil;
|
||||||
import io.jsonwebtoken.Claims;
|
import io.jsonwebtoken.Claims;
|
||||||
@@ -32,6 +33,10 @@ public class JwtTokenAdminInterceptor implements HandlerInterceptor {
|
|||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
//获取请求的线程ID
|
||||||
|
log.error("当前拦截器中的线程id:{}",Thread.currentThread().getId());
|
||||||
|
|
||||||
|
|
||||||
//判断当前拦截到的是Controller的方法还是其他资源
|
//判断当前拦截到的是Controller的方法还是其他资源
|
||||||
if (!(handler instanceof HandlerMethod)) {
|
if (!(handler instanceof HandlerMethod)) {
|
||||||
//当前拦截到的不是动态方法,直接放行
|
//当前拦截到的不是动态方法,直接放行
|
||||||
@@ -46,7 +51,9 @@ public class JwtTokenAdminInterceptor implements HandlerInterceptor {
|
|||||||
log.info("jwt校验:{}", token);
|
log.info("jwt校验:{}", token);
|
||||||
Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
|
Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
|
||||||
Long empId = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString());
|
Long empId = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString());
|
||||||
log.info("当前员工id:", empId);
|
log.info("当前员工id:{}", empId);
|
||||||
|
//将员工id保存到threadLocal,给三层架构使用
|
||||||
|
BaseContext.setCurrentId(empId);
|
||||||
//3、通过,放行
|
//3、通过,放行
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.sky.service.impl;
|
|||||||
import com.sky.constant.MessageConstant;
|
import com.sky.constant.MessageConstant;
|
||||||
import com.sky.constant.PasswordConstant;
|
import com.sky.constant.PasswordConstant;
|
||||||
import com.sky.constant.StatusConstant;
|
import com.sky.constant.StatusConstant;
|
||||||
|
import com.sky.context.BaseContext;
|
||||||
import com.sky.dto.EmployeeDTO;
|
import com.sky.dto.EmployeeDTO;
|
||||||
import com.sky.dto.EmployeeLoginDTO;
|
import com.sky.dto.EmployeeLoginDTO;
|
||||||
import com.sky.entity.Employee;
|
import com.sky.entity.Employee;
|
||||||
@@ -11,6 +12,7 @@ import com.sky.exception.AccountNotFoundException;
|
|||||||
import com.sky.exception.PasswordErrorException;
|
import com.sky.exception.PasswordErrorException;
|
||||||
import com.sky.mapper.EmployeeMapper;
|
import com.sky.mapper.EmployeeMapper;
|
||||||
import com.sky.service.EmployeeService;
|
import com.sky.service.EmployeeService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -19,6 +21,7 @@ import org.springframework.util.DigestUtils;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@Slf4j
|
||||||
public class EmployeeServiceImpl implements EmployeeService {
|
public class EmployeeServiceImpl implements EmployeeService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -66,6 +69,8 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void save(EmployeeDTO employeeDTO) {
|
public void save(EmployeeDTO employeeDTO) {
|
||||||
|
log.error("当前业务层中的线程id:{}",Thread.currentThread().getId());
|
||||||
|
|
||||||
//dto->Employee 相同属性值的复制
|
//dto->Employee 相同属性值的复制
|
||||||
Employee employee = new Employee();
|
Employee employee = new Employee();
|
||||||
//对象属性拷贝
|
//对象属性拷贝
|
||||||
@@ -77,9 +82,9 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
//补充剩下字段的值
|
//补充剩下字段的值
|
||||||
employee.setCreateTime(LocalDateTime.now());
|
employee.setCreateTime(LocalDateTime.now());
|
||||||
employee.setUpdateTime(LocalDateTime.now());
|
employee.setUpdateTime(LocalDateTime.now());
|
||||||
//todo 先写死操作者,后面再处理
|
//从threadLocal中获取当前的操作者
|
||||||
employee.setCreateUser(1L);
|
employee.setCreateUser(BaseContext.getCurrentId());
|
||||||
employee.setUpdateUser(1L);
|
employee.setUpdateUser(BaseContext.getCurrentId());
|
||||||
employeeMapper.insert(employee);
|
employeeMapper.insert(employee);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user