抽象类

package com.infosec.warning.scheduler;

import com.infosec.warning.abs.NetAuthWarning;
import com.infosec.warning.abs.NetAuthWarningNotice;
import com.infosec.warning.entity.WarningRuleConfig;
import com.infosec.warning.service.IWarningRuleConfigService;

public abstract class AbstractNetAuthSchedulerWarning implements NetAuthWarning{

    protected NetAuthWarningNotice netAuthNotice;
    
    protected IWarningRuleConfigService iWarningRuleConfigService;
    
    public AbstractNetAuthSchedulerWarning(IWarningRuleConfigService iWarningRuleConfigService) {
        super();
        this.iWarningRuleConfigService = iWarningRuleConfigService;
    }

    /**
     * 通过子类实现具体的预警逻辑
     */
    public abstract void startWarning();
    
    /**
     * 是否开启预警通知
     * @return
     */
    public boolean isOpenWarning() {
        WarningRuleConfig warningRuleConfig = iWarningRuleConfigService.findOneById(1);
        return warningRuleConfig.getEnabled();
    };
    
    @Override
    public void warning() {
        
        if(isOpenWarning()) {
            startWarning();
        }
        
    }
    
    public void setNetAuthNotice(NetAuthWarningNotice netAuthNotice) {
        this.netAuthNotice = netAuthNotice;
    }

    public NetAuthWarningNotice getNetAuthNotice() {
        return netAuthNotice;
    }

    public IWarningRuleConfigService getiWarningRuleConfigService() {
        return iWarningRuleConfigService;
    }

}


package com.infosec.warning.abs;

public interface NetAuthWarning {
    /**
     * 预警方法
     */
    void warning();
}

package com.infosec.warning.scheduler.job;

import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;

import com.infosec.warning.scheduler.AbstractNetAuthSchedulerWarning;
import com.infosec.warning.scheduler.job.detail.WeakPasswordWarningJobDetail;
import com.infosec.warning.service.IWarningRuleConfigService;

public class NetAuthWeakPasswordWarning extends AbstractNetAuthSchedulerWarning{
    public NetAuthWeakPasswordWarning(IWarningRuleConfigService iWarningRuleConfigService) {
        super(iWarningRuleConfigService);
    }
    /**
     *
     *  @Description    启动弱密码用户预警检测调度器  --补充备注
     *     @author            wyh
     *     @date            2020年10月14日上午9:38:55
     */
    @Override
    public void startWarning() {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            JobDataMap jobDataMap = new JobDataMap();
            jobDataMap.put("warning", this);
            JobDetail weakPwdJobDetail = JobBuilder.newJob(WeakPasswordWarningJobDetail.class)
                    .usingJobData(jobDataMap)
                    .withIdentity("weakPwdJob", "weakPwdGroup")
                    .build();
            Trigger oldtrigger = scheduler.getTrigger(TriggerKey.triggerKey("weakPwdTrigger", "weakPwdTriggerGroup"));
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("weakPwdTrigger", "weakPwdTriggerGroup")
                    .startNow()
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMinutes(iWarningRuleConfigService.findOneById(1).getWeakPasswordUserTime()).repeatForever())
                    .build();
            if(null != oldtrigger) {
                scheduler.rescheduleJob(TriggerKey.triggerKey("weakPwdTrigger", "weakPwdTriggerGroup"), trigger);
            }else {
                scheduler.scheduleJob(weakPwdJobDetail, trigger);
                //启动定时任务
                scheduler.start();
            };
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}

    public void warningAndNoticeWeakPasswordWithSchedulerEvent() {
        NetAuthWeakPasswordWarning warning = new NetAuthWeakPasswordWarning(iWarningRuleConfigService);
        NetAuthWarningNotice netAuthWarningNotice=new SimpleNetAuthWarningNotice(iWarningNoticeModeConfigService.findOneById(1), NetAuthWarningNoticeEventType.WEAKPASSWORDNOTICE.getEvent());
        warning.setNetAuthNotice(netAuthWarningNotice);
        warning.warning();
    }



package com.infosec.warning.scheduler.job.detail;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.infosec.user.service.user.IUserExtensionService;
import com.infosec.warning.abs.NetAuthWarningNoticeEventType;
import com.infosec.warning.entity.WarningNoticeEventLog;
import com.infosec.warning.entity.WarningRuleConfig;
import com.infosec.warning.scheduler.job.NetAuthWeakPasswordWarning;
import com.infosec.warning.service.IWarningNoticeEventLogService;
import com.netauth.utils.component.LocalSpringService;
/**
 *
 *
 * <p>
 *     弱密码用户检测预警记录执行job--补充备注
 * </p>
 *
 * <p>
 *     版权所有:神州融信信息技术有限公司(c) 2019
 * </p>
 * @date 2020年10月14日
 * @author wyh
 *
 */
public class WeakPasswordWarningJobDetail implements Job{
    Logger logger = LoggerFactory.getLogger(WeakPasswordWarningJobDetail.class);
    
    private IUserExtensionService userExtensionService = LocalSpringService.getSpringService(IUserExtensionService.class);
    
    private NetAuthWeakPasswordWarning warning;
    
    private IWarningNoticeEventLogService iWarningNoticeEventLogService = LocalSpringService.getSpringService(IWarningNoticeEventLogService.class);
    
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        warning = (NetAuthWeakPasswordWarning) context.getJobDetail().getJobDataMap().get("warning");
        if(!warning.isOpenWarning()) {
            try {
                //如果关闭监控,则关闭当前任务
                context.getScheduler().shutdown();
                logger.debug("当前任务已关闭");
            } catch (SchedulerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //执行弱密码检测任务
        try {
            long value = userExtensionService.findWeakPasswordUserCount();
            WarningRuleConfig ruleConfig = warning.getiWarningRuleConfigService().findOneById(1);
            Integer threshold = ruleConfig.getWeakPasswordUserThreshold();
            WarningNoticeEventLog log = new WarningNoticeEventLog();
            log.setCreateTime(new Date());
            log.setEventType(NetAuthWarningNoticeEventType.WEAKPASSWORDNOTICE.getEvent());
            log.setValue(Integer.valueOf(value+""));
            log.setEventName(NetAuthWarningNoticeEventType.WEAKPASSWORDNOTICE.getName());
            iWarningNoticeEventLogService.saveOrUpdate(log);
            if(value>=threshold) {
                warning.getNetAuthNotice().notice("预警通知:

	当前系统中存在弱密码用户已经超过安全预警设置的阈值,请尽快到系统进行处理。当前弱密码用户总数为:"+value, Integer.parseInt(value+""), null, null, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("弱密码用户检测失败");
        }
    }

}




原文地址:https://www.cnblogs.com/cuijinlong/p/15044183.html