|
@@ -0,0 +1,66 @@
|
|
|
+package com.jiayue.ssi.job;
|
|
|
+
|
|
|
+import com.jiayue.ssi.entity.SysUser;
|
|
|
+import com.jiayue.ssi.service.SysParameterService;
|
|
|
+import com.jiayue.ssi.service.SysUserService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+* 系统定时扫描未使用的账号
|
|
|
+*
|
|
|
+* @author xsl
|
|
|
+* @since 2023/04/06
|
|
|
+*/
|
|
|
+@Service
|
|
|
+@EnableScheduling
|
|
|
+@Slf4j
|
|
|
+public class AutoScanAccount {
|
|
|
+ @Autowired
|
|
|
+ SysParameterService sysParameterService;
|
|
|
+ @Autowired
|
|
|
+ SysUserService sysUserService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 每30分钟执行一次扫描
|
|
|
+ */
|
|
|
+ @Scheduled(cron = "0 0/10 * * * ?")
|
|
|
+ public void scanNotUsedAccount() throws Exception{
|
|
|
+ // 获取参数配置,未设置默认3个月
|
|
|
+ int autoScanAccount = Integer.parseInt(sysParameterService.queryByKey("autoScanAccount", "3"));
|
|
|
+ if (autoScanAccount>0){
|
|
|
+ log.info("自动扫描长期未使用的账号开始。。。");
|
|
|
+ // 进行扫描
|
|
|
+ List<SysUser> sysUsers = sysUserService.queryAllUsers();
|
|
|
+ for (SysUser sysUser:sysUsers){
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ if (sysUser.getLoginDate()==null){
|
|
|
+ calendar.setTime(sysUser.getLoginDate());
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ calendar.setTime(sysUser.getCreateTime());
|
|
|
+ }
|
|
|
+ calendar.add(Calendar.MONTH, autoScanAccount);
|
|
|
+ if (calendar.getTimeInMillis()<System.currentTimeMillis()){
|
|
|
+ // 上次登录后N月没有使用,锁定账号
|
|
|
+ sysUser.setLockTime(System.currentTimeMillis());
|
|
|
+ sysUser.setStatus("1");
|
|
|
+ Boolean bo = sysUserService.updateUser(sysUser);
|
|
|
+ if (!bo){
|
|
|
+ log.info(sysUser.getUsername()+",超过"+autoScanAccount+"个月未使用,锁定账号失败");
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ log.info(sysUser.getUsername()+",超过"+autoScanAccount+"个月未使用,锁定账号成功");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("自动扫描长期未使用的账号完成");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|