/**
- 创建时间
/ @TableField(value = “CREATE_TIME”, fill = FieldFill.INSERT) @DateTimeFormat(pattern=“yyyy-MM-dd HH:mm:ss”) private Date createTime; /* - 创建账号
/ @TableField(value = “CREATE_USER”, fill = FieldFill.INSERT) private String createUser; /* - 更新时间
/ @TableField(value = “UPDATE_TIME”, fill = FieldFill.UPDATE) @DateTimeFormat(pattern=“yyyy-MM-dd HH:mm:ss”) private Date updateTime; /* - 更新账号
*/ @TableField(value = “UPDATE_USER”, fill = FieldFill.UPDATE) private String updateUser;
//第一种方式填充插入更新时间 //创建RabbitMetaObjectHandler类实现MetaObjectHandler接口 @Component public class RabbitMetaObjectHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { this.setFieldValByName(“createTime”, new Date(), metaObject); //获取当前登录用户1 String username = “system”; if (SecurityContextHolder.getContext().getAuthentication() != null) { username = SecurityContextHolder.getContext().getAuthentication().getName(); } Object lastUpdateNameId = metaObject.getValue(“lastUpdateNameId”); Object lastUpdateTime = metaObject.getValue(“lastUpdateTime”); //获取当前登录用户2 SysUser user = (SysUser)SecurityUtils.getSubject().getPrincipal(); if (null == lastUpdateNameId) { metaObject.setValue(“lastUpdateNameId”, user.getId()); } if (null == lastUpdateTime) { metaObject.setValue(“lastUpdateTime”, new Date()); } //获取当前登录用户3 HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); String user = request.getHeader(“authercode”); this.setFieldValByName(“createUser”, username, metaObject); }
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime", new Date(), metaObject);
String username = "system";
if (SecurityContextHolder.getContext().getAuthentication() != null) {
username = SecurityContextHolder.getContext().getAuthentication().getName();
}
this.setFieldValByName("updateUser", username, metaObject);
}
}
//第二种方式填充插入更新时间
/**
-
新增,更新时,自动更新字段 */ @Component public class MyMetaObjectHandler implements MetaObjectHandler { private static String CREATE_TIME = “createTime”; private static String UPDATE_TIME = “updateTime”; /**
- 插入使用
*/ @Override public void insertFill(MetaObject metaObject) { fillValue(metaObject, CREATE_TIME, () -> getDateValue(metaObject.getSetterType(CREATE_TIME))); fillValue(metaObject, UPDATE_TIME, () -> getDateValue(metaObject.getSetterType(UPDATE_TIME))); } @Override public void updateFill(MetaObject metaObject) { fillValue(metaObject, “et.”+UPDATE_TIME, () -> getDateValue(metaObject.getSetterType(“et.”+UPDATE_TIME))); } private void fillValue(MetaObject metaObject, String fieldName, Supplier valueSupplier) { if (!metaObject.hasGetter(fieldName)) { return; } Object sidObj = metaObject.getValue(fieldName); if (sidObj == null && metaObject.hasSetter(fieldName) && valueSupplier != null) { setFieldValByName(fieldName, valueSupplier.get(), metaObject); } } private Object getDateValue(Class<?> setterType) { if (Date.class.equals(setterType)) { return new Date(); } else if (LocalDateTime.class.equals(setterType)) { return LocalDateTime.now(); } else if (Long.class.equals(setterType)){ return System.currentTimeMillis(); } return null; } }
//第三种方式填充插入更新时间 /**
-
线程本地持有者:存储当前请求线程的登录用户 */ public class ThreadLocalHolder{ private final static ThreadLocal ThreadLocalHolder = new ThreadLocal<>(); /**
- 重写ThreadLocal的三个方法:set、get、remove
*/ public static void set(OnlineUser user) { ThreadLocalHolder.set(user); } public static OnlineUser get() { return ThreadLocalHolder.get(); } public static void remove() { ThreadLocalHolder.remove(); } /**
- 获取登录用户的常用信息:id、姓名
*/ public static Long getId() { return ThreadLocalHolder.get().getId(); } public static String getName() { return ThreadLocalHolder.get().getName(); } }
/**
-
登录验证 */ @Controller public class SysLoginController extends BaseController { @RequestMapping(value = “/login”, method = RequestMethod.POST) @ResponseBody public Result login(HttpServletRequest request, UserVO userVO) { User userSession = (User) request.getSession().getAttribute(“user”); //用户未登录 if(userSession == null){ //校验:名字是否空、用户是否存在、密码是否空、密码是否正确 if (密码正确){ //执行登陆后台的逻辑 request.getSession().setAttribute(“user”,user); return Result.ok(“登录成功”); }else{ return Result.fail(“密码不正确,请重新输入”); } }else{ return Result.ok(“登录成功”); } } @ApiOperation(“退出登录”) @RequestMapping(value = “logout”, method = RequestMethod.POST) @ResponseBody public Result logoutApi(HttpServletRequest request) { //销毁session request.getSession().invalidate(); return Result.ok(“退出登录成功”); } } /** -
session拦截器 */ @Component public class SessionInterceptorConfig implements HandlerInterceptor { private static Logger log = LoggerFactory.getLogger(SessionInterceptorConfig.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { User user = (User) request.getSession().getAttribute(“user”); //如果用户不为空,则通过 if (user != null) { //将在线用户存入线程本地ThreadLocal中 OnlineUser onlineUser = new OnlineUser(user.getId(), user.getUserName()); ThreadLocalHolder.set(onlineUser); return true; } else { log.info(">>>session过期, 跳至登录页"); request.getRequestDispatcher("/login").forward(request, response); return false; } } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { //将线程本地ThreadLocal中的在线用户移除(需要手动移除,否则会产生内存泄露) ThreadLocalHolder.remove(); } }
|