| | |
| | | package com.whyc.listener; |
| | | |
| | | import com.whyc.constant.YamlProperties; |
| | | import com.whyc.task.AccountLockStrategyTask; |
| | | import com.whyc.task.AccountScanTask; |
| | | import com.whyc.task.PreventTryTask; |
| | | import org.apache.commons.lang.time.DateUtils; |
| | | |
| | | import javax.servlet.ServletContextEvent; |
| | |
| | | |
| | | @Override |
| | | public void contextInitialized(ServletContextEvent servletContextEvent) { |
| | | // Calendar instance = Calendar.getInstance(); |
| | | // instance.set(Calendar.HOUR_OF_DAY,23); |
| | | // instance.set(Calendar.MINUTE,59); |
| | | // instance.set(Calendar.SECOND,59); |
| | | // Date firstDate = instance.getTime(); |
| | | // timer = new Timer("Task定时任务",true); |
| | | // /*//每月刷新数据库表中天气支持城市-聚合平台,弃用 |
| | | // timer.schedule(new WeatherTask(),firstDate,ONE_MONTH);*/ |
| | | // |
| | | // //每日清空Application中的weather |
| | | // timer.schedule(new ClearApplicationWeatherTask(),firstDate,ONE_DAY); |
| | | // |
| | | // //告警工单功能是否开启 |
| | | // if(PropertiesUtil.props.getProperty("alarm.task.switch").toUpperCase().equals("ON")) { |
| | | // //刷新告警记录生成工单,频率为1秒1次 |
| | | // timer.schedule(new AlarmTask(), 1000, 1000); |
| | | // |
| | | // //告警工单自动派单,频率为1秒钟1次 |
| | | // timer.schedule(new AlarmDispatchTask(), 1000, 1000); |
| | | // } |
| | | // |
| | | // //配置文件控制告警短信发送功能是否开启 |
| | | // //告警短信发送,频率为10秒钟1次 |
| | | // if(PropertiesUtil.props.getProperty("message.switch").toUpperCase().equals("ON")){ |
| | | // timer.schedule(new AlarmMessageTask2(),1000*10,1000*10); |
| | | // } |
| | | // //账号自动扫描策略,频率为1天 |
| | | // timer.schedule(new AccountScanTask(),1000,ONE_DAY); |
| | | // //账号登录失败锁定相关策略,频率为2秒 |
| | | // timer.schedule(new AccountLockStrategyTask(servletContextEvent.getServletContext()),1000,2000); |
| | | // //防重放功能随机码清除,频率为2秒 |
| | | // timer.schedule(new PreventTryTask(servletContextEvent.getServletContext()),1000,2000); |
| | | /*Calendar instance = Calendar.getInstance(); |
| | | instance.set(Calendar.HOUR_OF_DAY,23); |
| | | instance.set(Calendar.MINUTE,59); |
| | | instance.set(Calendar.SECOND,59); |
| | | Date firstDate = instance.getTime();*/ |
| | | timer = new Timer("Task定时任务",true); |
| | | /*//每月刷新数据库表中天气支持城市-聚合平台,弃用 |
| | | timer.schedule(new WeatherTask(),firstDate,ONE_MONTH);*/ |
| | | |
| | | //每日清空Application中的weather |
| | | //timer.schedule(new ClearApplicationWeatherTask(),firstDate,ONE_DAY); |
| | | |
| | | //告警工单功能是否开启 |
| | | /*if(YamlProperties.alarmTaskSwitch.toUpperCase().equals("ON")) { |
| | | //刷新告警记录生成工单,频率为1秒1次 |
| | | timer.schedule(new AlarmTask(), 1000, 1000); |
| | | |
| | | //告警工单自动派单,频率为1秒钟1次 |
| | | timer.schedule(new AlarmDispatchTask(), 1000, 1000); |
| | | }*/ |
| | | |
| | | //配置文件控制告警短信发送功能是否开启 |
| | | //告警短信发送,频率为10秒钟1次 |
| | | /*if(YamlProperties.messageSwitch.toUpperCase().equals("ON")){ |
| | | timer.schedule(new AlarmMessageTask2(),1000*10,1000*10); |
| | | }*/ |
| | | //账号自动扫描策略,频率为1天 |
| | | timer.schedule(new AccountScanTask(),1000,ONE_DAY); |
| | | //账号登录失败锁定相关策略,频率为2秒 |
| | | timer.schedule(new AccountLockStrategyTask(servletContextEvent.getServletContext()),1000,2000); |
| | | //防重放功能随机码清除,频率为2秒 |
| | | timer.schedule(new PreventTryTask(servletContextEvent.getServletContext()),1000,2000); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | |
| | | public void updateExpiredAccount() { |
| | | UpdateWrapper<UserInf> wrapper = Wrappers.update(); |
| | | wrapper.set("status",UserConstant.ACCOUNT_STATUS_CANCEL).eq("type",UserConstant.ACCOUNT_TYPE_TEMP).gt("expiration_time",new Date()); |
| | | userMapper.update(null,wrapper); |
| | | } |
| | | |
| | | public void unLock() { |
| | | UpdateWrapper<UserInf> wrapper = Wrappers.update(); |
| | | wrapper.set("status", UserConstant.ACCOUNT_STATUS_ACTIVE.getValue()).set("lock_time",null) |
| | | .eq("status",UserConstant.ACCOUNT_STATUS_LOCK_FAIL.getValue()) |
| | | .last(" and date_add(lock_time,interval 20 MINUTE)<now() and uId is not null"); |
| | | userMapper.update(null,wrapper); |
| | | } |
| | | |
| | | public void updateAccountStatus(int status) { |
| | | UpdateWrapper<UserInf> wrapper = Wrappers.update(); |
| | | wrapper.set("status", status) |
| | | .eq("status",UserConstant.ACCOUNT_STATUS_ACTIVE.getValue()) |
| | | .last(" and date_add(last_login_time,interval 3 MONTH)<now() and uId is not null"); |
| | | userMapper.update(null,wrapper); |
| | | } |
| | | } |
New file |
| | |
| | | package com.whyc.task; |
| | | |
| | | import com.whyc.pojo.PageParam; |
| | | import com.whyc.service.PageParamService; |
| | | import com.whyc.service.UserService; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.ServletContext; |
| | | import java.util.Enumeration; |
| | | import java.util.List; |
| | | import java.util.TimerTask; |
| | | |
| | | /** |
| | | * 账号解锁:默认20分钟 |
| | | * 账号登录失败时间超过24小时,移除 |
| | | * TODO 尚未测试 |
| | | */ |
| | | public class AccountLockStrategyTask extends TimerTask { |
| | | |
| | | private ServletContext servletContext; |
| | | |
| | | @Resource |
| | | private UserService userService; |
| | | |
| | | @Resource |
| | | private PageParamService pageParamService; |
| | | |
| | | public AccountLockStrategyTask(ServletContext servletContext) { |
| | | this.servletContext = servletContext; |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | //临时账号检测是否过期,如果过期,更改状态为注销 |
| | | userService.updateExpiredAccount(); |
| | | |
| | | //查询账号解锁策略 |
| | | PageParam accountUnLock = (PageParam) ((List)pageParamService.findByCategoryId(9).getData()).get(1); |
| | | if(accountUnLock.getStatus()==2){ //自动解锁策略 |
| | | //查询账号锁定时间,超过20分钟解锁 |
| | | userService.unLock(); |
| | | } |
| | | |
| | | //登录失败时间超过24小时记录,内存中application移除 |
| | | Enumeration<String> attributeNames = servletContext.getAttributeNames(); |
| | | //24小时前的时间戳 |
| | | long oneDayBefore = System.currentTimeMillis()-24*60*60*1000; |
| | | |
| | | while (attributeNames.hasMoreElements()){ |
| | | String attributeName = attributeNames.nextElement(); |
| | | if(attributeName.contains("_login_fail_times_")){ |
| | | if(oneDayBefore>Long.parseLong(attributeName.split("_login_fail_times_")[1])) { |
| | | servletContext.removeAttribute(attributeName); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | package com.whyc.task; |
| | | |
| | | import com.whyc.pojo.PageParam; |
| | | import com.whyc.service.PageParamService; |
| | | import com.whyc.service.UserService; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.List; |
| | | import java.util.TimerTask; |
| | | |
| | | public class AccountScanTask extends TimerTask { |
| | | |
| | | @Resource |
| | | private UserService userService; |
| | | |
| | | @Resource |
| | | private PageParamService pageParamService; |
| | | |
| | | @Override |
| | | public void run() { |
| | | //查询账号扫描策略 |
| | | PageParam accountScan = (PageParam) ((List)pageParamService.findByCategoryId(8)).get(0); |
| | | //查询激活账号的最后登录时间,3个月内未登录的自动休眠或者锁定 |
| | | int status; |
| | | if(accountScan.getStatus()==1){ |
| | | status = 3; |
| | | }else{ |
| | | status = 2; |
| | | } |
| | | userService.updateAccountStatus(status); |
| | | } |
| | | } |
New file |
| | |
| | | package com.whyc.task; |
| | | |
| | | import javax.servlet.ServletContext; |
| | | import java.util.Enumeration; |
| | | import java.util.TimerTask; |
| | | |
| | | /** |
| | | * 防重放功能随机码清除 |
| | | */ |
| | | public class PreventTryTask extends TimerTask { |
| | | |
| | | private ServletContext servletContext; |
| | | |
| | | public PreventTryTask(ServletContext servletContext) { |
| | | this.servletContext = servletContext; |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | |
| | | //60秒前的记录,内存中application移除 |
| | | Enumeration<String> attributeNames = servletContext.getAttributeNames(); |
| | | long oneMinuteBefore = System.currentTimeMillis()-70*1000; |
| | | |
| | | while (attributeNames.hasMoreElements()){ |
| | | String attributeName = attributeNames.nextElement(); |
| | | if(attributeName.contains("randomStr_")){ |
| | | if(oneMinuteBefore>Long.parseLong((String) servletContext.getAttribute(attributeName.split("randomStr_")[1]))) { |
| | | servletContext.removeAttribute(attributeName); |
| | | servletContext.removeAttribute(attributeName.split("randomStr_")[1]); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |